美文网首页
MySQL 基础12 JBCD中工具类的抽取

MySQL 基础12 JBCD中工具类的抽取

作者: 小熊先生很不开心 | 来源:发表于2018-03-20 08:38 被阅读8次

    1.1 工具类的抽取

      因为传统JDBC的开发,注册驱动,获得连接,释放资源这些代码都是重复编写的。所以可以将重复的代码提取到一个类中来完成。

    • 工具类
    /**
     * JDBC的工具类
     * @author jt
     *
     */
    public class JDBCUtils {
        private static final String driverClassName;
        private static final String url;
        private static final String username;
        private static final String password;
        
        static{
            driverClassName="com.mysql.jdbc.Driver";
            url="jdbc:mysql:///web_test3";
            username="root";
            password="abc";
        }
    
        /**
         * 注册驱动的方法
         */
        public static void loadDriver(){
            try {
                Class.forName(driverClassName);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        
        /**
         * 获得连接的方法
         */
        public static Connection getConnection(){
            Connection conn = null;
            try{
                // 将驱动一并注册:
                loadDriver();
                // 获得连接
                conn = DriverManager.getConnection(url,username, password);
            }catch(Exception e){
                e.printStackTrace();
            }
            return conn;
        }
        
        /**
         * 释放资源的方法
         */
        public static void release(Statement stmt,Connection conn){
            if(stmt != null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                
                stmt = null;
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                conn = null;
            }
        }
        
        public static void release(ResultSet rs,Statement stmt,Connection conn){
            // 资源释放:
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                
                rs = null;
            }
            if(stmt != null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                
                stmt = null;
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                conn = null;
            }
        }
    }
    
    
    • 测试类
    @Test
        /**
         * 查询操作:使用工具类
         */
        public void demo1(){
            Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;
            try{
                // 获得连接:
                conn = JDBCUtils.getConnection();
                // 创建执行SQL语句的对象:
                stmt = conn.createStatement();
                // 编写SQL:
                String sql = "select * from user";
                // 执行查询:
                rs = stmt.executeQuery(sql);
                // 遍历结果集:
                while(rs.next()){
                    System.out.println(rs.getInt("id")+" "+rs.getString("username")+" "+rs.getString("password"));
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                // 释放资源:
                JDBCUtils.release(rs, stmt, conn);
            }
        }
    
    

    相关文章

      网友评论

          本文标题:MySQL 基础12 JBCD中工具类的抽取

          本文链接:https://www.haomeiwen.com/subject/nlorqftx.html