
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Current Row of ResultSet Object Using JDBC
The getRow() method of the ResultSet class returns the row number at which the ResultSet pointer exists in the current instance.
Assume we have a table named cricketers_data with 6 records as shown below:
+------------+------------+---------------+----------------+-------------+ | First_Name | Last_Name | Date_Of_Birth | Place_Of_Birth | Country | +------------+------------+---------------+----------------+-------------+ | Shikhar | Dhawan | 1981-12-05 | Delhi | India | | Jonathan | Trott | 1981-04-22 | CapeTown | SouthAfrica | | Lumara | Sangakkara | 1977-10-27 | Matale | Srilanka | | Virat | Kohli | 1987-04-30 | Delhi | India | | Rohit | Sharma | 1987-04-30 | Nagpur | India | | Ravindra | Jamnagar | 1988-12-06 | NULL | India | +------------+------------+---------------+----------------+-------------+
Following JDBC program establishes a connection with the database, retrieves the contents of the cricketers_data table into a ResultSet object and prints the current position of the ResultSet pointer.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class ResultSet_CurrentRow { 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/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating a Statement object Statement stmt = con.createStatement(); //Query to retrieve the contents of the employee_data table String query = "select * from cricketers_data"; //Executing the query ResultSet rs = stmt.executeQuery(query); //Moving the ResultSet pointer to a particular position rs.absolute(2); //Getting the current row of the ResultSet object int current_row = rs.getRow(); System.out.println(current_row); } }
Output
Connection established...... 2
Advertisements