JDBC的CRUD操作之查询操作

1.1.1 查询多条记录

@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 查询一条记录

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

                        }

                }

        }

猜你喜欢

转载自blog.51cto.com/14473726/2447872
今日推荐