- 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 move the ResultSet cursor to the last row in JDBC?
Whenever we execute SQL statements using the executeQuery() method, it returns a ResultSet object which holds the tabular data returned by the SELECT queries (in general).
The ResultSet object contains a cursor/pointer which points to the current row. Initially this cursor is positioned before first row (default position).
You can move the cursor of the ResultSet object to the last row from the current position, using the last() method of the ResultSet interface.
rs.last()
This method returns a boolean value specifying whether the cursor has been moved to the last row successfully.
If there are no rows in the current ResultSet object this method returns false, else it returns true.
Let us create a table with name tutorials_data in MySQL database using CREATE statement as shown below −
CREATE TABLE tutorials_data ( tutorial_id INT, tutorial_title VARCHAR(100), tutorial_author VARCHAR(40), submission_date date, PRIMARY KEY (tutorial_id) );
Now, we will insert 5 records in tutorials_data table using INSERT statements −
insert into tutorials_data values(1, 'Java', 'Krishna Kasyap', DATE('2019-09-01')); insert into tutorials_data values(2, 'JFreeCharts', 'Satish Kumar', DATE('2019-05-01 ')); insert into tutorials_data values(3, 'JavaSprings', 'Amit Tiwari', DATE(' 2019-05-01')); insert into tutorials_data values(4, 'Android', 'Sai Ram', DATE('2019-03-01')); insert into tutorials_data values(5, 'Cassandra', 'Pruthvi Raj', DATE(' 2019-04-06'));
In the following JDBC program we have established connection with the database and retrieved the contents of the table named tutorials_data into a ResultSet object, initially the cursor in this object will be at the default position (before first row), using the last() method we have moved the cursor from default position to last row and, displayed the contents it.
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class RSCursor_last { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/mydb"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating the Statement Statement stmt = con.createStatement(); //Query to retrieve records String query = "Select * from tutorials_data"; //Executing the query ResultSet rs = stmt.executeQuery(query); //Moving the cursor from default position to last row. rs.last(); System.out.println("Contents of the first record: "); //Current record details. System.out.print("ID: "+rs.getInt("tutorial_id")+", "); System.out.print("Title: "+rs.getString("tutorial_title")+", "); System.out.print("Author: "+rs.getString("tutorial_author")+", "); System.out.print("Submission date: "+rs.getDate("submission_date")); } }
Output
Connection established...... Contents of the first record: ID: 5, Title: Cassandra, Author: Pruthvi Raj, Submission date: 2019-04-06
- Related Articles
- 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 first row in JDBC?
- How to close the ResultSet cursor automatically, after commit in JDBC?
- How to get the row count from ResultSet in JDBC
- How to move the pointer of a ResultSet to the default position using JDBC?
- How to retrieve the contents of a ResultSet from last to first in JDBC?
- How to find the current row of a ResultSet object using JDBC?
- How to move the pointer of a ResultSet to the end of the table using JDBC?
- How to delete a row from ResultSet object using JDBC?
- How to get Row and Column Count from ResultSet in JDBC
- How can you move the cursor in scrollable result sets in JDBC?
- How to insert a row into a ResultSet object using JDBC?
- How to get the row count in JDBC?
- How to Automatically Move the Cursor to a Specific Cell in Excel?
