- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is Result in JDBC? How to retrieve data from ResultSet object?
A ResultSet interface in JDBC represents the tabular data generated by SQL queries. It has a cursor which points to the current row. Initially, this cursor is positioned before the first row.
Moving the pointer throughout result set
The next() method of the ResultSet interface moves the pointer of the current (ResultSet) object to the next row, from the current position. This method returns a boolean value, if there are no rows next to its current position it returns false, else it returns true. Therefore, using this method in the while loop you can iterate the contents of the result set.
while(rs.next()){ }
Getting the column values of each record:
ResultSet interface (also) provides getter methods (getXXX()) to retrieve values in each column of a row. Every getter methods have two variants:
getXXX(int columnIndex): This accepts an integer value representing the index of the column and returns its value.
getXXX(String columnLabel ): This accepts a String value representing the name of the column and returns its value.
You need to use the respective getter method based on the datatype of the column in the table.
Example
Assume we have a table named dataset with content as shown below:
+--------------+-----------+ | 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 | +--------------+-----------+
Example
The following example retrieves all the records of the Dataset table and prints 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(""); } } }
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
- Related Articles
- How to retrieve the contents of a ResultSet from last to first in JDBC?
- How to delete a row from ResultSet object using JDBC?
- How to retrieve binary data from a table using JDBC?
- How to read/retrieve data from Database to JSON using JDBC?
- How to retrieve a DATALINK object from a table using JDBC?
- What is ResultSet holdability in JDBC?
- What is Type_FORWARD_ONLY ResultSet in JDBC?
- What is TYPE_SCROLL_INSENSITIVE ResultSet in JDBC?
- What is TYPE_SCROLL_SENSITIVE ResultSet in JDBC?
- What is ResultSet Concurrency in JDBC?
- What is CONCUR_UPDATABLE ResultSet in JDBC? Explain?
- What is CONCUR_READ_ONLY ResultSet in JDBC? Explain?
- How to get the row count from ResultSet in JDBC
- How to retrieve Date from a table in JDBC?
- How to insert a row into a ResultSet object using JDBC?
