JDBC第三天_通过 ResultSet 执行查询操作

在学习了数据库连接和对数据库的insert、update、delete后,最重要的就是做查询了,因为查询是有返回值得,返回的数据库中查询所得的表内容
在Statement中insert、update、delete方法是没有返回值的,用的是 sta.executeUpdate(sql)方法
而查询我们要用到ResultSet 和 statement.executeQuery(sql)方法,返回的结果集我们放在ResultSet的对象 中
下面是针对查询的实例:方法也是用到了以前写的工具类,工具类中有 连接数据库方法,有关闭数据库和Statement方法,这次添加了关闭ResultSet方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
      *
      * ResultSet:
      *    结果集. 封装了使用 JDBC 进行查询的结果.
      * 1. 调用 Statement 对象的 executeQuery(sql) 可以得到结果集.
      * 2. ResultSet 返回的实际上就是一张数据表. 有一个指针指向数据表的第一行的前面.
      *     可以调用 next() 方法检测下一行是否有效. 若有效该方法返回 true, 且指针下移. 相当于
      *    Iterator 对象的 hasNext() 和 next() 方法的结合体
      * 3. 当指针对位到一行时, 可以通过调用 getXxx(index) 或 getXxx(columnName)
      *    获取每一列的值. 例如: getInt(1), getString("name")
      * 4. ResultSet 当然也需要进行关闭.
      */
@Test
     public  void  ResTest(){
         //连接数据库
         Connection conn =  null ;
         //创建Statement对象 和ResultSet
         Statement statement =  null ;
         ResultSet res =  null ;
         //准备sql语句
         String  sql= "select * from article" ;
         try {
             conn = JDBCTolls.getConnection();
             statement = conn.createStatement();
             //执行Statement对象的excuteQuery()方法
             res = statement.executeQuery(sql);
             while (res.next()){
                 int  id = res.getInt( "id" );
                 String  title = res.getString( "title" );
                 int  ptime = res.getInt( "ptime" );
                 String  content = res.getString( "content" );
                 System.out.println(id+ "-" +title+ "-" +ptime+ "-" +content);
             }
         } catch (Exception e){
             e.printStackTrace();
         } finally {
             //关闭Statement
             //关闭数据库 调用工具类中的关闭方法
             JDBCTolls.release(statement, conn,res);
         }
     }
 
 
工具类JDBCTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//关闭Statement sta,Connection conn,ResultSet res
public  static  void  release(Statement sta,Connection conn,ResultSet res){
         if (res !=  null ){
             try  {
                 res.close();
             catch  (SQLException e) {
                 e.printStackTrace();
             }
         }
         if (sta!= null )
             try  {
                 sta.close();
             catch  (SQLException e) {
                 e.printStackTrace();
             } finally {
                 if (conn != null )
                     try  {
                         conn.close();
                     catch  (SQLException e) {
                         e.printStackTrace();
                     }
             }
     }

//连接数据库方法 返回Connection对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public  static  Connection getConnection(){
     //声明用到的变量 url 驱动类型 用户名和密码
             String driverClass = null ;
             String url = null ;
             String user =  null ;
             String password = null ;
             Connection conn =  null ;
             try {
                 //通过读入文件得到信息
                 @SuppressWarnings ( "static-access" )
                 InputStream in=    JDBCTolls. class .getClassLoader().getSystemResourceAsStream( "jdbc.properties" );
                 Properties properties =  new  Properties();
                 properties.load(in);
                 // 1. 准备获取连接的 4 个字符串: user, password, jdbcUrl, driverClass
                 driverClass=properties.getProperty( "driver" );
                 url = properties.getProperty( "url" );
                 user = properties.getProperty( "user" );
                 password = properties.getProperty( "password" );
                 // 2. 加载驱动: Class.forName(driverClass)
                 Class.forName(driverClass);
                 // 3. 调用
                 // DriverManager.getConnection(jdbcUrl, user, password)
                 // 获取数据库连接
                 conn = DriverManager.getConnection(url, user, password);
                 System.out.println(conn);
             } catch (ClassNotFoundException e){
                 e.printStackTrace();
             } catch (Exception e){
                 e.printStackTrace();
             }
     return  conn;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
  * update insert delete操作通用方法,注意这里如果没有返回值的话,关闭是可以写在finally中的,如果有返回值则不能,否则会出错
  * @param sql 
  */
public  static  void  update(String sql){
     Connection conn =  null ;
     Statement statement =  null ;
     try {
         conn = JDBCTools.getconnection();
         statement = conn.createStatement();
         statement.executeUpdate(sql);
     } catch (Exception e){
         e.printStackTrace();
     } finally {
         JDBCTools.release(statement, conn);
     }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
  * select 查询操作,返回ResultSet结果集
  * @param sql
  * @return
  */
public  static  ResultSet getRes(String sql){
     ResultSet res =  null ;
     Connection conn =  null ;
     Statement statement =  null ;
     try {
         conn = JDBCTools.getconnection();
         statement = conn.createStatement();
         res = statement.executeQuery(sql);
         if (res !=  null ){
             return  res;
         }
     } catch (Exception e){
         e.printStackTrace();
     }
     return  null ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
      * 测试select方法
      */
     @Test
     public  void  test4(){
         ResultSet res =  null ;
         String sql =  "select * from article" ;
         try  {
             //调用工具类,返回ResultSet
             res = JDBCTools.getRes(sql);
             while (res.next()){
                 System.out.println(res.getInt( "id" )+res.getString( 2 )+res.getString( 3 )+res.getString( 4 ));
             }
         catch  (SQLException e) {
             e.printStackTrace();
         }
         finally {
             JDBCTools.releaseDB(res,  null null );
         }
     }


你可能感兴趣的:(JDBC第三天_通过 ResultSet 执行查询操作)