wood burning stoves 2.0*
The moose likes JDBC and the fly likes Problem faced while closing the ResultSet.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem faced while closing the ResultSet.." Watch "Problem faced while closing the ResultSet.." New topic
Author

Problem faced while closing the ResultSet..

Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
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..


Abhishek
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42030
    
  64
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.


Ping & DNS - my free Android networking tools app
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
thanks Ulf Dittmer for the reply

can you tell me when is the resultset gets closed?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30580
    
154

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
GeeCON Prague 2014
 
subject: Problem faced while closing the ResultSet..