zoukankan      html  css  js  c++  java
  • JDBC开发模式

    一】代码模块———Demo.java 

           public class Demo {
                    private static Connection connection;
                    private static Statement statement;
                    private static ResultSet rs;
                
                    public static void main(String[] args) {
                
                        connection = JDBCUtils.getConnection();
                
                        try {
                            statement = connection.createStatement();
                            rs = statement.executeQuery(SqlMapping.QEURY_ALL);
                
                            while (rs.next()) {
                                System.out.println(rs.getInt("id") + " : "
                                        + rs.getString("name") + " : " + rs.getString("gender")
                                        + " : " + rs.getString("salary"));
                            }
                
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                
                        JDBCUtils.closeStream(rs, statement, connection);
                
                    }
    
                


                
     二】工具模块———JdbcUtil.java   

                        public class JDBCUtils {
            
                        private static Properties properties;
                        //得到数据库驱动
                        static {
                            InputStream inputStream  = JDBCUtils.class.getClassLoader().getResourceAsStream("com/suse/jdbc/db.properties");
                            properties = new Properties();
                            try {
                                properties.load(inputStream);
                                Class.forName(properties.getProperty("driver"));
                            } catch (IOException e) {
                                e.printStackTrace();
                            } catch (ClassNotFoundException e) {
                                e.printStackTrace();
                            }
                        }
                        
                        //得到数据库连接
                        public static Connection getConnection(){
                                Connection connection  = null;
                                try {
                                    connection = DriverManager.getConnection(properties.getProperty("url"), properties);
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
                                return connection;
                        }
                        
                        
                        //关闭流
                        public static void closeStream( ResultSet rs, Statement stmt, Connection conn) {
                            
                            if (null != rs) {
                                try {
                                    rs.close();
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
                            }
                            if (null != stmt) {
                                try {
                                    stmt.close();
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
                            }
                            if (null != conn) {
                                try {
                                    conn.close();
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
                            }
                        }    
                    }
                

    三】配置文件模块———db.properties

            driver = com.mysql.jdbc.Driver
                url  = jdbc:mysql://127.0.0.1:3306/mydb
                user = root
                password = root
  • 相关阅读:
    服务器变量 $_SERVER 详解
    PHP 函数功能参考
    ecshop后台0day漏洞原理+利用方法 XSS+Getshll
    CSRF漏洞原理说明与利用方法
    Drupal 远程命令执行漏洞(CVE-2018-7600)
    SSH登陆验证绕过漏洞(cve-2018-10933)
    单元二:建立和维护数据表
    单元一:认识数据库系统
    【 模块1 认识计算机 】1.2 认识微型计算机
    【 模块1 认识计算机 】 1.1走进计算机世界
  • 原文地址:https://www.cnblogs.com/SkyGood/p/4005337.html
Copyright © 2011-2022 走看看