Java ResultSet接口是 java.sql 包的一部分,它是 JDBC 框架的核心组件之一。
ResultSet 保持指针/指针,指向查询结果的单一行. 使用由 ResultSet 提供的导航和接收方法,我们可以一次性地重复并访问数据库记录。
Java 结果层次结构
上面的图表显示了 ResultSet 在 JDBC 框架中的位置。 ResultSet 可以通过使用 Statement、 PreparedStatement 或 CallableStatement 执行 SQL 查询来获取。
AutoCloseable, Wrapper是ResultatSet的超级界面,现在我们将看到如何在我们的Java程序中使用ResultatSet。
结果示例
我们将使用MySQL为我们的示例目的. 使用下面的DB脚本来创建一个数据库和表以及一些记录。
1create database empdb;
2
3use empdb;
4
5create table tblemployee (empid integer primary key, firstname varchar(32), lastname varchar(32), dob date);
6
7insert into tblemployee values (1, 'Mike', 'Davis',' 1998-11-11');
8insert into tblemployee values (2, 'Josh', 'Martin', '1988-10-22');
9insert into tblemployee values (3, 'Ricky', 'Smith', '1999-05-11');
让我们看看下面的示例程序,从表中提取记录,并在控制台上打印它们。
1package com.journaldev.examples;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.ResultSet;
6import java.sql.Statement;
7import java.util.Date;
8
9/**
10 * Java Resultset Example of Retrieving records.
11 *
12 * @author pankaj
13 *
14 */
15
16public class ResultSetDemo {
17
18 public static void main(String[] args) {
19 String query = "select empid, firstname, lastname, dob from tblemployee";
20 Connection conn = null;
21 Statement stmt = null;
22 try {
23 Class.forName("com.mysql.jdbc.Driver");
24 conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/empdb", "root", "root");
25 stmt = conn.createStatement();
26 ResultSet rs = stmt.executeQuery(query);
27 while (rs.next()) {
28 Integer empId = rs.getInt(1);
29 String firstName = rs.getString(2);
30 String lastName = rs.getString(3);
31 Date dob = rs.getDate(4);
32 System.out.println("empId:" + empId);
33 System.out.println("firstName:" + firstName);
34 System.out.println("lastName:" + lastName);
35 System.out.println("dob:" + dob);
36 System.out.println("");
37 }
38 rs.close();
39 } catch (Exception e) {
40 e.printStackTrace();
41 } finally {
42 try {
43 stmt.close();
44 conn.close();
45 } catch (Exception e) {}
46 }
47 }
48}
出发点:**
1empId:1
2firstName:Mike
3lastName:Davis
4dob:1998-11-11
5
6empId:2
7firstName:Josh
8lastName:Martin
9dob:1988-10-22
10
11empId:3
12firstName:Ricky
13lastName:Smith
14dob:1999-05-11
解釋 *:
ResultSet 是通过在 Statement 实例上调用 executeQuery 方法来获得的。 最初,ResultSet 的路由器指向第一个行之前的位置
ResultSet 旁边的方法将路由器移动到下一个行. 如果有进一步的行,它会返回 true
否则它会返回 false
我们可以从 ResultSet 获取数据,使用其提供的接口方法. 例如, get(), getString(), getDate()
所有接口方法有两种变量。 1st 变量将列索引作为参数, 2nd 变量接受列名称为 Parameter
结果类型 & 竞争
在创建 Statement 实例时,我们可以指定 ResultSet 的类型和同步性,例如 PreparedStatement或 CallableStatement。
_statement.createStatement(int resultSetType,int resultSetConcurrency)
结果类型
只有前进(结果设置_TYPE_FORWARD_ONLY)
此类型的 ResultSet 实例只能向前移动,从第一行到最后一行. ResultSet 可以通过调用 next() 方法向前移动一个行. 我们可以在创建 Statement 实例、 PreparedStatement 或 CallableStatement 时获得此类型的 ResultSet。
1Statement stmt = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
2ResultSet rs = stmt.executeQuery("select * from tbluser");
**2) 滚动不敏感(ResultSet.TYPE_SCROLL_INSENSITIVE)
Scroll Insensitive ResultSet 可以向前和向后滚动,也可以通过呼叫绝对() 方法滚动到绝对位置,但它对数据的变化不敏感,只有当查询被执行并获得 ResultSet 时才会包含数据。
1Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
2ResultSet rs = stmt.executeQuery("select * from tbluser");
**3) 滚动敏感(ResultSet._TYPE_SCROLL_SENSITIVE)
Scroll Sensitive ResultSet 可以向前和向后滚动,也可以通过呼叫绝对() 方法滚动到绝对位置,但它对数据的变化很敏感。
1Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
2ResultSet rs = stmt.executeQuery("select * from tbluser");
竞争对手 竞争对手
1) 只读(ResultSet.CONCUR_READ_ONLY)
它是默认的同步模型. 我们只能在 ResultSet 实例上执行只读操作. 不允许更新操作。
2) 可更新(ResultSet.CONCUR_UPDATABLE)
在这种情况下,我们可以在 ResultSet 实例上执行更新操作。
结果方法
我们可以将 ResultSet 方法分为以下类别。
- 导航方法
- Getter/Reader 方法
- Setter/Updater 方法
- Miscellaneous 方法 - close() 和 GetMetaData()
1、导航方法
- boolean绝对(int行) throws SQLException**:** 此方法将ResultatSet引导器移动到指定的行并在操作成功时返回 true
- void afterLast() throws SQLException**:** 此方法将ResultatSet引导器移动到最后一行之后的位置
- void前First() throws SQLException**:** 此方法将ResultatSet引导器移动到第一个行前的位置
- boolean first()throws** SQLException**:本方法将ResultatSet引导器移动到第一个行
1package com.journaldev.examples;
2import java.sql.*;
3
4/**
5 * Java Resultset Example using navigational methods.
6 *
7 * @author pankaj
8 *
9 */
10public class ResultSetDemo {
11
12 public static void main(String[] args) {
13 String query = "select empid, firstname, lastname, dob from tblemployee";
14 Connection conn = null;
15 Statement stmt = null;
16 try {
17 Class.forName("com.mysql.jdbc.Driver");
18 conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/empdb", "root", "root");
19 stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
20 ResultSet rs = stmt.executeQuery(query);
21 System.out.println("All the rows of table=>");
22 while (rs.next()) {
23 // Go to next row by calling next() method
24 displayData(rs);
25 }
26 System.out.println("Now go directly to 2nd row=>");
27 rs.absolute(2); // Go directly to 2nd row
28 displayData(rs);
29 System.out.println("Now go to Previous row=>");
30 rs.previous();
31 // Go to 1st row which is previous of 2nd row
32 displayData(rs);
33 rs.close();
34 } catch (Exception e) {
35 e.printStackTrace();
36 } finally {
37 try {
38 stmt.close();
39 conn.close();
40 } catch (Exception e) {
41 }
42 }
43 }
44
45 public static void displayData(ResultSet rs) throws SQLException {
46 System.out.println("empId:" + rs.getInt(1));
47 System.out.println("firstName:" + rs.getString(2));
48 System.out.println("lastName:" + rs.getString(3));
49 System.out.println("dob:" + rs.getDate(4));
50 System.out.println("");
51 }
52}
出发点:**
1All the rows of table=>
2empId:1
3firstName:Mike
4lastName:Davis
5dob:1998-11-11
6
7empId:2
8firstName:Josh
9lastName:Martin
10dob:1988-10-22
11
12empId:3
13firstName:Ricky
14lastName:Smith
15dob:1999-05-11
16
17Now go directly to 2nd row=>
18empId:2
19firstName:Josh
20lastName:Martin
21dob:1988-10-22
22
23Now go to Previous row=>
24empId:1
25firstName:Mike
26lastName:Davis
27dob:1998-11-11
结果组 Getter/Reader 方法s
int 得到Int(int 列Index)* 扔出** SQL例外: 这种方法返回指定列的Index值为int
- 长得到Long(int列Index) 扔出 SQL例外: 这种方法返回指定列的值Index为长
- 字符串获取String( **int ** 列索引) ** 扔出 ** SQL 例外 : 这种方法返回指定列的Index值为 String
- java.sql。 日期 获取 Date (int 列 Index) ** 扔出 ** SQL 例外 : 这种方法返回指定列的Index值为 java.sql。 日期
- ** int ** 得到 Int( 串列标签) ** 扔 ** SQL 例外 : 这种方法返回指定列名的值为 **int **.
- ** 长** 得到 Long( 斜列标签) ** 扔 ** SQL 例外 : 这种方法返回指定列名的值为长
- 字符串获得 String( 字符列标签) ** 扔出 ** SQL 例外 : 此方法返回指定列名的值为 String.
- java. sql 。 日期 : 这种方法返回指定列名的值为 java.sql. 日期
- 结果 设定包含返回其他原始数据类型如布尔、浮和双等的获取方法。 它还有从数据库中获取数组和二进制数据的方法. (_) (英语)
结果组 ** 设置 / 更新方法**
- ** 撤销** 最新情况 int(int 列 Idex,int x) ** 扔出** SQL 例外 : 这种方法更新了当前行中指定列的值为int值. (_) ( )* ** void** 更新 Long(int 栏Index,** long** x) ** 扔出** SQL例外: 此方法更新当前行中指定列的长值 。
- 无效更新String( 插入列) 索引, 字符串 x) 丢出 SQL 例外 : 此方法更新当前行指定列的字符串值 。 (_) ( )* ** void** 更新 Date(int 专栏Index, java.sql.Date x) ** sQL例外: 这种方法以 java. sql 更新当前行指定列的值 。 日期值.
- 无效更新 Int( 串列标签, int x) 抛出 SQL 例外 : 此方法更新了当前行指定列标签的正文值 。
- 无效更新 长( L) 字符串列 标签,长x)抛出 SQL 例外 : 此方法更新当前行指定列标签的长值 。
- 无效更新 String( string 列) 标签, 字符串 x) 扔出 SQL 例外 : 这种方法以字符串值更新当前行指定列标签的值. (_) ( )* ** 撤销** 日期( 串列标签, java. sql. 日期 x) ** 扔出 ** SQL 例外 : 这种方法以 java. sql 更新当前行指定列标签的值 。 日期值. ( (英语)
** 注意:** Setter/Updater 方法不会直接更新数据库值。 ** 数据库值将在调用 insertRow 或 updateRow 方法后插入/更新。
1package com.journaldev.examples;
2import java.sql.*;
3
4/**
5 * Java Resultset Example using updater methods.
6 *
7 * @author pankaj
8 *
9 */
10
11public class ResultSetUpdateDemo {
12
13 public static void main(String[] args) {
14 String query = "select empid, firstname, lastname, dob from tblemployee";
15 Connection conn = null;
16 Statement stmt = null;
17 try {
18 Class.forName("com.mysql.jdbc.Driver");
19 conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/empdb", "root", "root");
20 stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
21 ResultSet rs = stmt.executeQuery(query);
22 System.out.println("Now go directly to 2nd row for Update");
23 if (rs.absolute(2)) {
24 // Go directly to 2nd row
25 System.out.println("Existing Name:" + rs.getString("firstName"));
26 rs.updateString("firstname", "Tyson");
27 rs.updateRow();
28 }
29 rs.beforeFirst(); // go to start
30 System.out.println("All the rows of table=>");
31 while (rs.next()) {
32 // Go to next row by calling next() method
33 displayData(rs);
34 }
35 rs.close();
36 } catch (Exception e) {
37 e.printStackTrace();
38 } finally {
39 try {
40 stmt.close();
41 conn.close();
42 } catch (Exception e) {
43 }
44 }
45 }
46
47 public static void displayData(ResultSet rs) throws SQLException {
48 System.out.println("empId:" + rs.getInt(1));
49 System.out.println("firstName:" + rs.getString(2));
50 System.out.println("lastName:" + rs.getString(3));
51 System.out.println("dob:" + rs.getDate(4));
52 System.out.println("");
53 }
54}
出发点:**
1Now go directly to 2nd row for Update
2Existing Name:Josh
3All the rows of table=>
4empId:1
5firstName:Mike
6lastName:Davis
7dob:1998-11-11
8
9empId:2
10firstName:Tyson
11lastName:Martin
12dob:1988-10-22
13
14empId:3
15firstName:Ricky
16lastName:Smith
17dob:1999-05-11
四、混合方法
- void close() throws SQLException**:** 此方法释放了与 ResultSet 实例相关的资源. 必须调用,否则将导致资源泄露.
- ResultSetMetaData getMetaData() throws SQLException: 此方法返回了 ResultSetMetaData 实例. 它提供了查询输出列类型和属性信息
** 参考**: Java doc