1. 程式人生 > >JDBC的工具類的抽取

JDBC的工具類的抽取

dem trac found eas ger lex next() drive tac

1.1.1 抽取一個JDBC的工具類
因為傳統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;
                }
        }
}

1.1.2 測試工具類

@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);
                }
        }

JDBC的工具類的抽取