- 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 do you check if a ResultSet is closed or not 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).
The isClosed() method of the ResultSet interface is used to determine whether the current ResultSet object is closed.
rs.isclosed()
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'));
Following JDBC program establishes connection with the database, retrieves the contents of the above table into a ResultSet object displays it contents.
In this we have set the ResultSet holdability to CLOSE_CURSORS_AT_COMMIT. Therefore, soon we close the connection the ResultSet object will be closed.
We are verifying whether the ResultSet object is closed using the isClosed() method.
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class RS_is_closed { 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/mydb"; 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 tutorials_data"); //Setting the ResultSet to CLOSE_CURSORS_AT_COMMIT con.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT); System.out.println("Contents of the ResultSet object:"); while(rs.next()) { 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")); System.out.println(); } //Closing the connection con.close(); boolean bool = rs.isClosed(); if(bool) { System.out.println("ResultSet is closed"); } else { System.out.println("ResultSet is not closed"); } } }
Output
Connection established...... Contents of the ResultSet object: ID: 1, Title: Java, Author: Krishna Kasyap, Submission date: 2019-09-01 ID: 2, Title: JFreeCharts, Author: Satish kumar, Submission date: 2019-05-01 ID: 3, Title: JavaSprings, Author: Amit Tiwari, Submission date: 2019-03-01 ID: 4, Title: Android, Author: Sai Ram, Submission date: 2019-03-01 ID: 5, Title: Cassandra, Author: Pruthvi Raj, Submission date: 2019-04-06 ResultSet is closed