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

How do I browse a ResultSet using a JDBC program?

WBOY
Release: 2023-08-27 16:41:02
forward
556 people have browsed it

如何使用 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()){
}
Copy after login

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("");
}
Copy after login

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("");
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template