JDBC的CRUD操做之查詢操做

1.1.1 查詢多條記錄mysql

@Test

        /**

         * 查詢多條記錄

         */

        public void demo4(){

                Connection conn = null;

                Statement stmt = null;

                ResultSet rs = null;

                try{

                        // 註冊驅動

                        Class.forName("com.mysql.jdbc.Driver");

                        // 得到鏈接

                        conn = DriverManager.getConnection("jdbc:mysql:///web_test3", "root", "abc");

                        // 執行操做

                        // 建立執行SQL語句的對象:

                        stmt = conn.createStatement();

                        // 編寫SQL:

                        String sql = "select * from user";

                        // 執行SQL:

                        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{

                        // 資源釋放:

                        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 查詢一條記錄web

@Test

        /**

         * 查詢一條記錄

         */

        public void demo5(){

                Connection conn = null;

                Statement stmt = null;

                ResultSet rs = null;

                try{

                        // 註冊驅動

                        Class.forName("com.mysql.jdbc.Driver");

                        // 得到鏈接

                        conn = DriverManager.getConnection("jdbc:mysql:///web_test3", "root", "abc");

                        // 執行SQL

                        // 建立執行SQL語句對象:

                        stmt = conn.createStatement();

                        // 編寫SQL:

                        String sql = "select * from user where id = 4";

                        rs = stmt.executeQuery(sql);

                        // 判斷就能夠:

                        if(rs.next()){

                                System.out.println(rs.getInt("id")+" "+rs.getString("username")+" "+rs.getString("password"));

                        }

                }catch(Exception e){

                        e.printStackTrace();

                }finally{

                        // 資源釋放:

                        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;

                        }

                }

        }
相關文章
相關標籤/搜索