Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem faced while closing the ResultSet..

 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem:
Error: java.sql.SQLException: ResultSet is closed

Driver Used:
1. Microsoft Access Driver
Connection con=DriverManager.getConnection("jdbcdbcRIVER={Microsoft Access Driver (*.mdb)};DBQ="+path+"","","");

here is the code
-----------------------------------
try
{
String path="C:\\Documents and Settings\\Administrator\\Desktop\\book.mdb";
Connection con=DriverManager.getConnection("jdbcdbcRIVER={Microsoft Access Driver (*.mdb)};DBQ="+path+"","","");

Statement st=con.createStatement();
String bookname="maths";
String bookid="110";
int counter=0;
ResultSet rs=st.executeQuery("select count(*) from BookMaster where bookid='"+bookid+"'");
if(rs.next())
{
counter=rs.getInt(1);
}
if (counter>0)
{
st.executeUpdate("update BookMaster set bookname='"+bookname+"' where bookid='"+bookid+"'");
}
else
{
st.executeUpdate("insert into BookMaster(bookname,bookid) values ('"+bookname+"','"+bookid+"')");
}
rs.close();
st.close();
}
catch(Exception e)
{
e.printStackTrace();
}
-------------------------------------------
iam getting the eror at line rs.close(); i.e. while closing ResultSet.

here is the stack Trace..
java.sql.SQLException: ResultSet is closed
at sun.jdbc.odbc.JdbcOdbcResultSet.checkOpen(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.clearWarnings(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.close(Unknown Source)
at BookMaster.main(BookMaster.java:30)
-----------------------------------------------------
When i uncomment the rs.close() it is working perfectly, can anyone give the
possible reason for it.
Allso tell how to check whether ResultSet is closed or not, before i try to close it..
thanks in advance..
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can check whether a ResultSet is is open, but closing it explicitly is unnecessary anyway. Closing the statement will automatically close the ResultSet if it is still open.
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Ulf Dittmer for the reply

can you tell me when is the resultset gets closed?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhishek Reddy Chepyala:
can you tell me when is the resultset gets closed?

Ulf just did - when the statement is closed. Similarly, if the connection gets closed (due to timeout or explicitly), the statement and result set will get closed.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic