Home > Database > Mysql Tutorial > body text

What is the result in JDBC? How to retrieve data from ResultSet object?

PHPz
Release: 2023-08-26 16:17:18
forward
1443 people have browsed it

The ResultSet interface in JDBC represents the tabular data generated by SQL query. It has a cursor pointing to the current line. Initially, this cursor is located before the first line.

JDBC 中的结果是什么?如何从 ResultSet 对象中检索数据?

Move the pointer in the entire result set

The next() method of the ResultSet interfacechanges the current (ResultSet ) object's pointer moves from the current position to the next line. This method returns a boolean value that returns false if there is no row next to the current position and true otherwise. Therefore, use this method in a while loop to iterate over the contents of the result set.

while(rs.next()){
}
Copy after login

Get the column values ​​of each record:

The ResultSet interface (also) provides getter methods (getXXX()) to retrieve the values ​​in each column of the row. Each getter method has two variants:

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

  • getXXX(String columnLabel ): This 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.

Example

Suppose we have a table named dataset with the following content:

+--------------+-----------+
| mobile_brand | unit_sale |
+--------------+-----------+
| Iphone       |      3000 |
| Samsung      |      4000 |
| Nokia        |      5000 |
| Vivo         |      1500 |
| Oppo         |       900 |
| MI           |      6400 |
| MotoG        |      4360 |
| Lenovo       |      4100 |
| RedMi        |      4000 |
| MotoG        |      4360 |
| OnePlus      |      6334 |
+--------------+-----------+
Copy after login

Example

The following example retrievesDataset All records in the table and print the results:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class RetrievingData {
   public static void main(String args[]) throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/TestDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");

      //Creating a Statement object
      Statement stmt = con.createStatement();
      //Retrieving the data
      ResultSet rs = stmt.executeQuery("select * from Dataset");

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

Output

Connection established......
Contents of the table
Brand: Iphone, Sale: 3000
Brand: Samsung, Sale: 4000
Brand: Nokia, Sale: 5000
Brand: Vivo, Sale: 1500
Brand: Oppo, Sale: 900
Brand: MI, Sale: 6400
Brand: MotoG, Sale: 4360
Brand: Lenovo, Sale: 4100
Brand: RedMi, Sale: 4000
Brand: MotoG, Sale: 4360
Brand: OnePlus, Sale: 6334
Copy after login

The above is the detailed content of What is the result in JDBC? How to retrieve data from ResultSet object?. 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