JDBC的工具类的抽取

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

                }

        }
上一篇:sqlite3:深入理解sqlite3_stmt 机制


下一篇:Java_JDBC mysql数据库连接