Java ResultSet interface is a part of the java.sql package. It is one of the core components of the JDBC Framework. ResultSet Object is used to access query results retrieved from the relational databases.
Java ResultSet接口是java.sql包的一部分。 它是JDBC框架的核心组件之一。 ResultSet对象用于访问从关系数据库检索的查询结果。
ResultSet maintains cursor/pointer which points to a single row of the query results. Using navigational and getter methods provided by ResultSet, we can iterate and access database records one by one. ResultSet can also be used to update data.
ResultSet维护指向查询结果的单行的光标/指针。 使用ResultSet提供的导航和获取方法,我们可以一个一个地迭代和访问数据库记录。 ResultSet也可以用于更新数据。
Java ResultSet层次结构 (Java ResultSet Hierarchy)
The above diagram shows the place of ResultSet in the JDBC Framework. ResultSet can be obtained by executing SQL Query using Statement, PreparedStatement or CallableStatement.
上图显示了ResultSet在JDBC Framework中的位置。 通过使用Statement , PreparedStatement或CallableStatement执行SQL查询可以获得ResultSet 。
AutoCloseable, Wrapper are super interfaces of ResultSet. Now we will see how to work with ResultSet in our Java programs.
AutoCloseable , Wrapper是ResultSet的超级接口。 现在,我们将看到如何在Java程序中使用ResultSet。
ResultSet示例 (ResultSet Example)
We will be using MySQL for our example purpose. Use below DB script to create a database and table along with some records.
我们将使用MySQL作为示例。 使用下面的DB脚本来创建数据库和表以及一些记录。
create database empdb;
use empdb;
create table tblemployee (empid integer primary key, firstname varchar(32), lastname varchar(32), dob date);
insert into tblemployee values (1, 'Mike', 'Davis',' 1998-11-11');
insert into tblemployee values (2, 'Josh', 'Martin', '1988-10-22');
insert into tblemployee values (3, 'Ricky', 'Smith', '1999-05-11');
Let’s have look at the below example program to fetch the records from the table and print them on the console. Please make sure you have the MySQL JDBC driver in the project classpath.
让我们看下面的示例程序,从表中获取记录并将其打印在控制台上。 请确保在项目类路径中有MySQL JDBC驱动程序。
package com.journaldev.examples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
/**
* Java Resultset Example of Retrieving records.
*
* @author pankaj
*
*/
public class ResultSetDemo {
public static void main(String[] args) {
String query = "select empid, firstname, lastname, dob from tblemployee";
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/empdb", "root", "root");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
Integer empId = rs.getInt(1);
String firstName = rs.getString(2);
String lastName = rs.getString(3);
Date dob = rs.getDate(4);
System.out.println("empId:" + empId);
System.out.println("firstName:" + firstName);
System.out.println("lastName:" + lastName);
System.out.println("dob:" + dob);
System.out.println("");
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
stmt.close();
conn.close();
} catch (Exception e) {}
}
}
}
Output:
输出:
empId:1
firstName:Mike
lastName:Davis
dob:1998-11-11
empId:2
firstName:Josh
lastName:Martin
dob:1988-10-22
empId:3
firstName:Ricky
lastName:Smith
dob:1999-05-11
Explanation:
说明 :
- ResultSet is obtained by calling the executeQuery method on Statement instance. Initially, the cursor of ResultSet points to the position before the first ro