Java Mysql 简明教程

Java & MySQL - ResultSet

从数据库查询中读取数据的 SQL 语句,在结果集中返回数据。SELECT 语句是选择数据库中的行并将其显示在结果集中的一般方法。java.sql.ResultSet 接口表示数据库查询的结果集。

The SQL statements that read data from a database query, return the data in a result set. The SELECT statement is the standard way to select rows from a database and view them in a result set. The java.sql.ResultSet interface represents the result set of a database query.

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

A ResultSet object maintains a cursor that points to the current row in the result set. The term "result set" refers to the row and column data contained in a ResultSet object.

ResultSet 接口方法可以分为以下三类:

The methods of the ResultSet interface can be broken down into three categories −

  1. Navigational methods − Used to move the cursor around.

  2. Get methods − Used to view the data in the columns of the current row being pointed by the cursor.

  3. Update methods − Used to update the data in the columns of the current row. The updates can then be updated in the underlying database as well.

游标是基于 ResultSet 的属性而可移动的。当生成 ResultSet 的相应 Statement 被创建时,会指定这些属性。

The cursor is movable based on the properties of the ResultSet. These properties are designated when the corresponding Statement that generates the ResultSet is created.

JDBC 提供了以下连接方法来创建带所需 ResultSet 的语句:

JDBC provides the following connection methods to create statements with desired ResultSet −

  1. createStatement(int RSType, int RSConcurrency);

  2. prepareStatement(String SQL, int RSType, int RSConcurrency);

  3. prepareCall(String sql, int RSType, int RSConcurrency);

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

The first argument indicates the type of a ResultSet object and the second argument is one of two ResultSet constants for specifying whether a result set is read-only or updatable.

Type of ResultSet

可能的 RSType 如下所示。如果您未指定任何 ResultSet 类型,您将自动获得一个 TYPE_FORWARD_ONLY。

The possible RSType are given below. If you do not specify any ResultSet type, you will automatically get one that is 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.

Concurrency of ResultSet

可能的 RSConcurrency 如下所示。如果您未指定任何并发的类型,您将自动获得一个 CONCUR_READ_ONLY。

The possible RSConcurrency are given below. If you do not specify any Concurrency type, you will automatically get one that is 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.

我们到目前为止编写的示例都可以如以下所示书写,它初始化一个用于创建一个前向只读 ResultSet 对象的 Statement 对象:

All our examples written so far can be written as follows, which initializes a Statement object to create a forward-only, read only ResultSet object −

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