JDBC数据库编程:ResultSet接口

掌握ResultSet接口

使用ResultSet接口进行查询

ResultSet接口

在JDBC操作中,数据库所有查询记录将使用ResultSet进行接收,并使用ResultSet显示内容。

常用方法:

bool next() 将指针移动到下一行;

getString("columnName") 返回指定列内容。 

在操作中,使用statement中的executequery()方法进行数据库的查询操作,此方法的返回值就是ResultSet接口。

package 类集;
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.SQLException ;
import java.sql.Statement ;
import java.sql.ResultSet ;
public class ResultSetDemo01{
    // 定义MySQL的数据库驱动程序
    public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
    // 定义MySQL数据库的连接地址
    public static final String DBURL = "jdbc:mysql://localhost:3306/sys" ;
    // MySQL数据库的连接用户名
    public static final String DBUSER = "root" ;
    // MySQL数据库的连接密码
    public static final String DBPASS = "aaaaaa" ;
    public static void main(String args[]) throws Exception {
        Connection conn = null ;        // 数据库连接
        Statement stmt = null ;        // 数据库的操作对象
        ResultSet rs = null ;        // 保存查询结果
        String sql = "SELECT name,age FROM student" ;
        Class.forName(DBDRIVER) ;    // 加载驱动程序
        conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
        stmt = conn.createStatement() ;
        rs = stmt.executeQuery(sql) ;
        while(rs.next()){    // 依次取出数据
            String name = rs.getString("name") ;    // 取出name列的内容
            int age = rs.getInt("age") ;    // 取出age列的内容
            System.out.print("姓名:" + name + ";") ;
            System.out.print("年龄:" + age + ";") ;
            System.out.println("-----------------------") ;
        }
        rs.close() ;    ResultSet也要关闭
        stmt.close() ;
        conn.close() ;            // 数据库关闭
    }
};

输出结果:

Sun Apr 09 21:10:15 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
姓名:大红;年龄:35;-----------------------
姓名:大红;年龄:35;-----------------------
姓名:大红;年龄:35;-----------------------

以上操作,对于列名过长的取值不是很方便,

此时可以采取顺序的编号取得。

package 类集;
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.SQLException ;
import java.sql.Statement ;
import java.sql.ResultSet ;
public class ResultSetDemo01{
    // 定义MySQL的数据库驱动程序
    public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
    // 定义MySQL数据库的连接地址
    public static final String DBURL = "jdbc:mysql://localhost:3306/sys" ;
    // MySQL数据库的连接用户名
    public static final String DBUSER = "root" ;
    // MySQL数据库的连接密码
    public static final String DBPASS = "aaaaaa" ;
    public static void main(String args[]) throws Exception {
        Connection conn = null ;        // 数据库连接
        Statement stmt = null ;        // 数据库的操作对象
        ResultSet rs = null ;        // 保存查询结果
        String sql = "SELECT name,age FROM student" ;
        Class.forName(DBDRIVER) ;    // 加载驱动程序
        conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
        stmt = conn.createStatement() ;
      rs = stmt.executeQuery(sql) ;
        while(rs.next()){    // 依次取出数据
            String name = rs.getString(1) ;    // 取出name列的内容
            int age = rs.getInt(2) ;    // 取出age列的内容
            System.out.print("姓名:" + name + ";") ;
            System.out.print("年龄:" + age + ";") ;
            System.out.println("-----------------------") ;
        }
        rs.close() ;
        stmt.close() ;
        conn.close() ;            // 数据库关闭
    }
};

输出结果:

Sun Apr 09 21:19:51 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
姓名:大红;年龄:35;-----------------------
姓名:大红;年龄:35;-----------------------
姓名:大红;年龄:35;-----------------------

综合来说,使用顺序编号更方便

问题:直接使用select * from student不是更简单吗?

但是,在开发中不能直接使用*的。因为查询的时候,不能明确的表示要取的内容是什么,所以开发中,要明确写出需要查询的列。

原文地址:https://www.cnblogs.com/alsf/p/6686475.html