JDBC - 查看结果集示例



以下示例使用了结果集章节中描述的一些**getInt** 和**getString** 方法。此示例与导航结果集部分中解释的先前示例非常相似。

此示例代码是根据前面章节中完成的环境和数据库设置编写的。

逐行查看 ResultSet 中的记录示例

在此示例中,我们有四个静态字符串,包含数据库连接 URL、用户名、密码和 SELECT 查询。现在使用 DriverManager.getConnection() 方法,我们准备了一个数据库连接。连接准备就绪后,我们使用 connection.createStatement() 方法创建了一个 Statement 对象。在创建 Statement 对象时,我们使用了 ResultSet 类型 TYPE_SCROLL_INSENSITIVE 和 CONCUR_READ_ONLY,然后使用 statement.executeQuery() 执行 SELECT 查询并将结果存储在结果集中。

在第一步中,我们使用 ResultSet.last() 方法将结果集游标移动到最后一行,并使用 getInt() 和 getString() 方法打印最后一条记录的详细信息。getInt() 方法用于获取数字字段(如 id、age),getString() 用于获取 varchar 字段(如名字、姓氏)。在第二步中,我们使用 ResultSet.first() 方法将游标移动到第一行并打印第一条记录。作为最后一步,我们使用 ResultSet.next() 方法将游标移动到下一条记录并打印该记录。

将以下示例复制并粘贴到 ResultSetExample.java 中,编译并运行如下所示:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ResultSetExample {
   static final String DB_URL = "jdbc:mysql:///TUTORIALSPOINT";
   static final String USER = "guest";
   static final String PASS = "guest123";
   static final String QUERY = "SELECT id, first, last, age FROM Employees";

   public static void main(String[] args) {
      // Open a connection
      try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
         Statement stmt = conn.createStatement(
         ResultSet.TYPE_SCROLL_INSENSITIVE,
         ResultSet.CONCUR_READ_ONLY);
         ResultSet rs = stmt.executeQuery(QUERY);
      ) {		
         // Move cursor to the last row.
         System.out.println("Moving cursor to the last...");
         rs.last();

         // Extract data from result set
         System.out.println("Displaying record...");
         //Retrieve by column name
         int id  = rs.getInt("id");
         int age = rs.getInt("age");
         String first = rs.getString("first");
         String last = rs.getString("last");

         // Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + age);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);

         // Move cursor to the first row.
         System.out.println("Moving cursor to the first row...");
         rs.first();

         // Extract data from result set
         System.out.println("Displaying record...");
         // Retrieve by column name
         id  = rs.getInt("id");
         age = rs.getInt("age");
         first = rs.getString("first");
         last = rs.getString("last");

         // Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + age);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);
         // Move cursor to the first row.

         System.out.println("Moving cursor to the next row...");
         rs.next();

         // Extract data from result set
         System.out.println("Displaying record...");
         id  = rs.getInt("id");
         age = rs.getInt("age");
         first = rs.getString("first");
         last = rs.getString("last");

         // Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + age);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);		

      } catch (SQLException e) {
         e.printStackTrace();
      } 
   }
}

输出

现在让我们按如下方式编译上述示例:

C:\>javac ResultSetExample.java
C:\>

运行**ResultSetExample** 时,会产生以下结果:

C:\>java ResultSetExample
Moving cursor to the last...
Displaying record...
ID: 103, Age: 30, First: Sumit, Last: Mittal
Moving cursor to the first row...
Displaying record...
ID: 100, Age: 18, First: Zara, Last: Ali
Moving cursor to the next row...
Displaying record...
ID: 101, Age: 25, First: Mehnaz, Last: Fatma
C:\>

在循环中查看 ResultSet 中的记录示例

在此示例中,我们有四个静态字符串,包含数据库连接 URL、用户名、密码和 SELECT 查询。现在使用 DriverManager.getConnection() 方法,我们准备了一个数据库连接。连接准备就绪后,我们使用 connection.createStatement() 方法创建了一个 Statement 对象。在创建 Statement 对象时,我们使用了 ResultSet 类型 TYPE_SCROLL_INSENSITIVE 和 CONCUR_READ_ONLY,然后使用 statement.executeQuery() 执行 SELECT 查询并将结果存储在结果集中。

在第一步中,我们使用 ResultSet.next() 在 while 循环中将结果集游标移动到下一条记录并检查记录是否存在。在 while 循环中,我们使用了 getInt() 和 getString(),其中参数是列名以获取相应的值。打印完所有记录后,我们使用 ResultSet.first() 方法将结果集游标移动到第一条记录。在这里,我们使用了 getInt() 和 getString(),其中参数是从 1 开始的列索引以获取相应的值并打印该记录。

将以下示例复制并粘贴到 ResultSetExample.java 中,编译并运行如下所示:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ResultSetExample {
   static final String DB_URL = "jdbc:mysql:///TUTORIALSPOINT";
   static final String USER = "guest";
   static final String PASS = "guest123";
   static final String QUERY = "SELECT StudentID, FirstName, LastName , Dept FROM Students";

   public static void main(String[] args) {
      // Open a connection
      try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
         Statement stmt = conn.createStatement(
         ResultSet.TYPE_SCROLL_INSENSITIVE,
         ResultSet.CONCUR_READ_ONLY);   
         ResultSet rs = stmt.executeQuery(QUERY);
      ) {		
         // Extract data from result set
         System.out.println("Displaying record by column name ...");
         //Retrieve by column name
         while(rs.next()){
            int id  = rs.getInt("StudentID");
            String first = rs.getString("FirstName");
            String last = rs.getString("LastName");
            String dept = rs.getString("Dept");

            // Display values
            System.out.print("StudentID: " + id);
            System.out.print(", First: " + first);
            System.out.print(", Last: " + last);
            System.out.println(", Dept: " + dept);
         }

         // Move cursor to the first row.
         System.out.println("Moving cursor to the first row...");
         rs.first();

         System.out.println("Displaying record by column index...");
         // Retrieve by column index
         int id  = rs.getInt(1);
         String first = rs.getString(2);
         String last = rs.getString(3);
         String dept = rs.getString(4);

         // Display values
         System.out.print("StudentID: " + id);
         System.out.print(", First: " + first);
         System.out.print(", Last: " + last);
         System.out.println(", Dept: " + dept);

      } catch (SQLException e) {
         e.printStackTrace();
      } 
   }
}

输出

现在让我们按如下方式编译上述示例:

C:\>javac ResultSetExample.java
C:\>

运行**ResultSetExample** 时,会产生以下结果:

C:\>java ResultSetExample
Displaying record by column name ...
StudentID: 1000, First: Bonny, Last: Agarwal, Dept: Mathematics
StudentID: 1001, First: Amit, Last: Pandey, Dept: Physics
StudentID: 1002, First: Shefali, Last: Kumar, Dept: English
StudentID: 1004, First: Mohammed, Last: Ali, Dept: Mathematics
StudentID: 1005, First: Kishore, Last: Kumar, Dept: Biology
StudentID: 1006, First: Ganesh, Last: Khan, Dept: English
Moving cursor to the first row...
Displaying record by column index...
StudentID: 1000, First: Bonny, Last: Agarwal, Dept: Mathematics

C:\>
jdbc-result-sets.htm
广告

© . All rights reserved.