📜  JDBC-结果集

📅  最后修改于: 2020-11-13 04:34:13             🧑  作者: Mango


从数据库查询中读取数据的SQL语句返回结果集中的数据。 SELECT语句是从数据库中选择行并在结果集中查看它们的标准方法。 java.sql.ResultSet接口表示数据库查询的结果集。

ResultSet对象维护一个游标,该游标指向结果集中的当前行。术语“结果集”是指ResultSet对象中包含的行和列数据。

ResultSet接口的方法可以分为三类-

  • 导航方法:用于在附近移动光标。

  • 获取方法:用于查看光标所指向的当前行的列中的数据。

  • 更新方法:用于更新当前行各列中的数据。然后,更新也可以在基础数据库中更新。

光标可根据ResultSet的属性移动。这些属性是在创建生成ResultSet的相应Statement时指定的。

JDBC提供以下连接方法来创建具有所需ResultSet的语句-

  • createStatement(int RSType,int RSConcurrency);

  • prepareStatement(String SQL,int RSType,int RSConcurrency);

  • prepareCall(String sql,int RSType,int RSConcurrency);

第一个参数指示ResultSet对象的类型,第二个参数是两个ResultSet常量之一,用于指定结果集是只读还是可更新。

结果集类型

可能的RSType在下面给出。如果您未指定任何ResultSet类型,则将自动获得TYPE_FORWARD_ONLY。

Type Description
ResultSet.TYPE_FORWARD_ONLY The cursor can only move forward in the result set.
ResultSet.TYPE_SCROLL_INSENSITIVE The cursor can scroll forward and backward, and the result set is not sensitive to changes made by others to the database that occur after the result set was created.
ResultSet.TYPE_SCROLL_SENSITIVE. The cursor can scroll forward and backward, and the result set is sensitive to changes made by others to the database that occur after the result set was created.

ResultSet的并发

可能的RSConcurrency在下面给出。如果您未指定任何并发类型,则将自动获得CONCUR_READ_ONLY。

Concurrency Description
ResultSet.CONCUR_READ_ONLY Creates a read-only result set. This is the default
ResultSet.CONCUR_UPDATABLE Creates an updateable result set.

到目前为止,我们编写的所有示例都可以编写如下,该示例将一个Statement对象初始化为创建只读的ResultSet对象-

try {
   Statement stmt = conn.createStatement(
                           ResultSet.TYPE_FORWARD_ONLY,
                           ResultSet.CONCUR_READ_ONLY);
}
catch(Exception ex) {
   ....
}
finally {
   ....
}

导航结果集

ResultSet接口中有几种涉及移动光标的方法,包括-

S.N. Methods & Description
1 public void beforeFirst() throws SQLException

Moves the cursor just before the first row.

2 public void afterLast() throws SQLException

Moves the cursor just after the last row.

3 public boolean first() throws SQLException

Moves the cursor to the first row.

4 public void last() throws SQLException

Moves the cursor to the last row.

5 public boolean absolute(int row) throws SQLException

Moves the cursor to the specified row.

6 public boolean relative(int row) throws SQLException

Moves the cursor the given number of rows forward or backward, from where it is currently
pointing.

7 public boolean previous() throws SQLException

Moves the cursor to the previous row. This method returns false if the previous row is off the
result set.

8 public boolean next() throws SQLException

Moves the cursor to the next row. This method returns false if there are no more rows in the
result set.

9 public int getRow() throws SQLException

Returns the row number that the cursor is pointing to.

10 public void moveToInsertRow() throws SQLException

Moves the cursor to a special row in the result set that can be used to insert a new row
into the database. The current cursor location is remembered.

11 public void moveToCurrentRow() throws SQLException

Moves the cursor back to the current row if the cursor is currently at the insert row;
otherwise, this method does nothing

为了更好地理解,让我们研究导航示例代码

查看结果集

ResultSet接口包含许多用于获取当前行数据的方法。

每个可能的数据类型都有一个get方法,每个get方法都有两个版本-

  • 带有列名的列。

  • 带有列索引的索引。

例如,如果您要查看的列包含一个int,则需要使用ResultSet的getInt()方法之一-

S.N. Methods & Description
1 public int getInt(String columnName) throws SQLException

Returns the int in the current row in the column named columnName.

2 public int getInt(int columnIndex) throws SQLException

Returns the int in the current row in the specified column index. The column index
starts at 1, meaning the first column of a row is 1, the second column of a row is 2, and so on.

同样,在ResultSet接口中,对于八种Java基本类型中的每一种以及常见类型(如java.lang.String,java.lang.Object和java.net.URL)都有get方法。

还有一些获取SQL数据类型java.sql.Date,java.sql.Time,java.sql.TimeStamp,java.sql.Clob和java.sql.Blob的方法。查看文档以获取有关使用这些SQL数据类型的更多信息。

为了更好地理解,让我们研究Viewing-Example Code

更新结果集

ResultSet接口包含用于更新结果集数据的更新方法的集合。

与get方法一样,每种数据类型都有两种更新方法-

  • 带有列名的列。

  • 带有列索引的索引。

例如,要更新结果集当前行的String列,可以使用以下updateString()方法之一-

S.N. Methods & Description
1 public void updateString(int columnIndex, String s) throws SQLException

Changes the String in the specified column to the value of s.

2 public void updateString(String columnName, String s) throws SQLException

Similar to the previous method, except that the column is
specified by its name instead of its index.

在java.sql包中,有八种原始数据类型以及String,Object,URL和SQL数据类型的更新方法。

更新结果集中的一行会更改ResultSet对象中当前行的列,但不会更改基础数据库中的行。要更新对数据库中行的更改,您需要调用以下方法之一。

S.N. Methods & Description
1 public void updateRow()

Updates the current row by updating the corresponding row in the database.

2 public void deleteRow()

Deletes the current row from the database

3 public void refreshRow()

Refreshes the data in the result set to reflect any recent changes in the database.

4 public void cancelRowUpdates()

Cancels any updates made on the current row.

5 public void insertRow()

Inserts a row into the database. This method can only be invoked when the cursor is pointing to the insert row.

为了更好地理解,让我们研究更新示例代码