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

                }

        }

좋은 웹페이지 즐겨찾기