• <二>JDBC_通过ResultSet执行查询操作


    一、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 testResultSet() throws Exception{
      
        Connection conn=null;
        Statement st=null;
        ResultSet rs=null;
      
        try {
           /*
            * 1、获取Connection连接
            * 2、获取Statement
            * 3、SQL语句
            * 4、执行查询,得到ResultSet
            * 5、处理ResultSet
            * 6、关闭数据库连接
            *
            * */
           conn=JDBCTools.getConnection();
           st=conn.createStatement();
           String sql="select id,name,email,birth from customers where id=1";
           rs=st.executeQuery(sql);
           if (rs.next()) {
              int id=rs.getInt(1);
              String name=rs.getString(2);
              String email=rs.getString(3);
              Date birth=rs.getDate(4);
        
              System.out.println(id+"  "+name+"  "+email+"  "+birth);
           }
       
       
        } catch (Exception e) {
           e.printStackTrace();
        }finally{
           JDBCTools.release(rs, st, conn);
        }
      
     }

  • 相关阅读:
    递增一个指针
    ubuntu 系统 sudo apt-get update遇到问题sub-process returned an error code
    熟悉HDFS过程中遇到的问题
    大二暑假第八周进度报告
    大二暑假第七周进度报告
    oracle“ORA-00904”错误:标识符无效
    大学暑假第六周进度报告
    大二暑假第五周进度报告
    使用Navicat for Oracle新建表空间、用户及权限赋予
    大学暑假第四周进度报告
  • 原文地址:https://www.cnblogs.com/iamkk/p/6058253.html
Copyright © 2020-2023  润新知