- 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
How to find the current row of a 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
- Related Articles
- How to delete a row from ResultSet object using JDBC?
- How to insert a row into a ResultSet object using JDBC?
- How to get the table name of the current ResultSet using JDBC?
- How to get the row count from ResultSet in JDBC
- How to move the ResultSet cursor to the next row in JDBC?
- How to move the ResultSet cursor to the previous row in JDBC?
- How to move the ResultSet cursor to the last row in JDBC?
- How to move the ResultSet cursor to the first row in JDBC?
- How to get the current value of a particular row from a database using JDBC?
- How to update the contents of a ResultSet using a JDBC program?
- How to get Row and Column Count from ResultSet in JDBC
- How to Navigate through a ResultSet using a JDBC program?
- How to move the pointer of a ResultSet to the default position using JDBC?
- How to get all the column names from a ResultSet using JDBC
- How to move the pointer of a ResultSet to the end of the table using JDBC?

Advertisements