Home  >  Article  >  Database  >  How do I browse a ResultSet using a JDBC program?

How do I browse a ResultSet using a JDBC program?

WBOY
WBOYforward
2023-08-27 16:41:02537browse

如何使用 JDBC 程序浏览 ResultSet?

The next() method of the ResultSet interface moves the pointer/cursor of the current ResultSet object from the current position to the next row. This method returns a boolean value. This method returns false if there is no row next to the current position, true otherwise.

So, using this method in a while loop, you can iterate over the contents of the ResultSet object.

while(rs.next()){
}

Get the column value of each record

ResultSetThe interface (also) provides a getter method (getXXX()) to retrieve the value in each column of the row, There are two variants of each getter method:

  • getXXX(int columnIndex): Accepts an integer value representing the column index and returns its value.

  • getXXX(String columnLabel ): Accepts a string value representing the column name and returns its value.

You need to use the corresponding getter method according to the data type of the column in the table.

while(rs.next()) {
   System.out.print("Brand: "+rs.getString("Column_Name")+", ");
   System.out.print("Sale: "+rs.getString("Column_Name "));
   ………………………
   ………………………
   System.out.println("");
}

In the same way, if it is a bidirectional ResultSet object, you can use the previous() method to navigate backwards.

Because the pointer of the ResultSet object has been positioned before the first line by default. To navigate backwards you need to move the pointer/cursor to the next line after the last line and then navigate backwards:

rs.afterLast();

System.out.println("Contents of the table");
while(rs.previous()) {
   System.out.print("Brand: "+rs.getString("Mobile_Brand")+", ");
   System.out.print("Sale: "+rs.getString("Unit_Sale"));
   System.out.println("");
}

The above is the detailed content of How do I browse a ResultSet using a JDBC program?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete