Java ResultSet next() 方法及示例


当我们执行某些 SQL 查询(通常是SELECT 查询)时,它们会返回表格数据。

java.sql.ResultSet 接口表示 SQL 语句返回的此类表格数据。

ResultSet 对象保存由执行查询数据库语句的方法(通常是 Statement 接口的 executeQuery() 方法)返回的表格数据。

ResultSet 对象有一个光标/指针,指向当前行。最初,此光标位于第一行之前。

ResultSet 接口的 next() 方法将当前 (ResultSet) 对象的指针从当前位置移动到下一行。

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select * from MyPlayers");
rs.next();

即,第一次调用 next() 方法时,结果集指针/光标将移动到第 1 行(从默认位置)。

第二次调用 next() 方法时,结果集光标将移动到第 2 行。

返回值

此方法返回一个布尔值,指定 ResultSet 对象是否包含更多行。

如果在其当前位置的下一行没有行,则此方法返回 false,否则返回 true。

在 while 循环中使用此方法,您可以迭代结果集的内容。

while(rs.next()){
   System.out.println(rs.getString(1));
   System.out.println(rs.getString(2));
}

示例

让我们使用如下所示的 CREATE 语句在MySQL 数据库创建一个名为 MyPlayers 的表

CREATE TABLE MyPlayers(
   ID INT,
   First_Name VARCHAR(255),
   Last_Name VARCHAR(255),
   Date_Of_Birth date,
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255),
   PRIMARY KEY (ID)
);

现在,我们将使用 INSERT 语句在 MyPlayers 表中插入 7 条记录:

insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India');
insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica');
insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka');
insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India');
insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India');
insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India');
insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');

以下 JDBC 程序建立与数据库的连接,将表 MyPlayers 的内容检索到 ResultSet 对象中,使用 next() 方法将光标移动到下一行三次,并在每个位置打印该行内容。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ResultSet_next {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://127.0.0.1/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating the Statement
      Statement stmt = con.createStatement();
      //Query to retrieve records
      String query = "Select * from MyPlayers";
      //Executing the query
      ResultSet rs = stmt.executeQuery(query);
      System.out.println("Contents of the first record: ");
      //Moving the cursor from default position to 1st row.
      rs.next();
      //Current record details.
      System.out.print("ID: "+rs.getInt("ID")+", ");
      System.out.print("Name: "+rs.getString("First_Name")+", ");
      System.out.print("Age: "+rs.getString("Last_Name")+", ");
      System.out.print("Salary: "+rs.getDate("Date_Of_Birth")+", ");
      System.out.print("Country: "+rs.getString("Place_Of_Birth")+", ");
      System.out.print("Address: "+rs.getString("Country"));
      System.out.println();
      //Moving the cursor from default position to 1st row.
      rs.next();
      //Current record details.
      System.out.print("ID: "+rs.getInt("ID")+", ");
      System.out.print("Name: "+rs.getString("First_Name")+", ");
      System.out.print("Age: "+rs.getString("Last_Name")+", ");
      System.out.print("Salary: "+rs.getDate("Date_Of_Birth")+", ");
      System.out.print("Country: "+rs.getString("Place_Of_Birth")+", ");
      System.out.print("Address: "+rs.getString("Country"));
      System.out.println();
      //Moving the cursor from 1st row position to 2nd row.
      rs.next();
      //Current record details.
      System.out.print("ID: "+rs.getInt("ID")+", ");
      System.out.print("Name: "+rs.getString("First_Name")+", ");
      System.out.print("Age: "+rs.getString("Last_Name")+", ");
      System.out.print("Salary: "+rs.getDate("Date_Of_Birth")+", ");
      System.out.print("Country: "+rs.getString("Place_Of_Birth")+", ");
      System.out.print("Address: "+rs.getString("Country"));
      System.out.println();
      //Moving the cursor from 2nd row to 3rd row.
      rs.next();
      //Current record details.
      System.out.print("ID: "+rs.getInt("ID")+", ");
      System.out.print("Name: "+rs.getString("First_Name")+", ");
      System.out.print("Age: "+rs.getString("Last_Name")+", ");
      System.out.print("Salary: "+rs.getDate("Date_Of_Birth")+", ");
      System.out.print("Country: "+rs.getString("Place_Of_Birth")+", ");
      System.out.print("Address: "+rs.getString("Country"));
      System.out.println();
   }
}

输出

Connection established......
Contents of the first record:
ID: 1, Name: Shikhar, Age: Dhawan, Salary: 1981-12-05, Address: DelhiAddress: India
ID: 2, Name: Jonathan, Age: Trott, Salary: 1981-04-22, Address: CapeTownAddress: SouthAfrica
ID: 3, Name: Kumara, Age: Sangakkara, Salary: 1977-10-27, Address: MataleAddress: Srilanka
ID: 4, Name: Virat, Age: Kohli, Salary: 1988-11-05, Address: MumbaiAddress: India

更新于: 2024-05-31

23K+ 浏览量

启动你的职业生涯

通过完成课程获得认证

开始学习
广告