- 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 pointer of a ResultSet to the end of the table using JDBC?
The afterLast() method of the ResultSet interface moves the cursor/pointer after the last row of the ResultSet object.
rs.afterLast();
Assume we have a table name dataset 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 | +--------------+-----------+
Following example demonstrates the creation of bi-directional ResultSet. Here we trying to create a bi-directional ResultSet object which retrieves the data from the table name dataset and, we are trying to print rows of the dataset table from last to first.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class BidirectionalResultSet { 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(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); //Retrieving the data ResultSet rs = stmt.executeQuery("select * from Dataset"); rs.afterLast(); System.out.println("Contents of the table"); while(rs.previous()) { 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: Vivo, Sale: 1500 Brand: Nokia, Sale: 5000 Brand: Samsung, Sale: 4000 Brand: IPhone, Sale: 3000
- Related Articles
- How to move the pointer of a ResultSet to the default position using JDBC?
- How to get the table name of the current ResultSet using 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 update the contents of a ResultSet using a JDBC program?
- How to find the current row of a ResultSet object using JDBC?
- How to get all the column names from a ResultSet using JDBC
- How to get the number of columns of a table using JDBC?
- How to get the datatype of a column of a table using JDBC?
- How to get the size of a column of a table using JDBC?
- How to Navigate through a ResultSet using a JDBC program?
- How to retrieve the contents of a ResultSet from last to first in JDBC?
- How to get the number of records in a table using JDBC?

Advertisements