This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes Statement Closed Error - please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Statement Closed Error - please help" Watch "Statement Closed Error - please help" New topic
Author

Statement Closed Error - please help

Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Hi Friends,
I have two queries to run from one class.I send the queries to another class,which send me back the resultset to cook.For one query its running fine,but i don't know why for two queries ,its throwing error as "statement closed".Can you please tell me where i am going wrong???

In first method i have:
ResSet qt = new ResSet();
ResultSet rs=qt.getResultSet(query);

2nd method:
ResSet qt1 = new ResSet();
ResultSet rs1=qt1.getResultSet(query1);


SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
Hello Jas,

The error means you are probable closing your statement too early. As, i don't know where you're closing it, only thing i can tell is to check where you're using your close() method to close your statement(it should be at the end of everything since you have included statement, resultset and connection in that method).

Mala
Padma Lalwani
Ranch Hand

Joined: Nov 02, 2004
Posts: 49
Hi,
The code is pretty odd, but let me take a shot at it
I think first time around you somehow have a valid handle to the datasource (maybe you call getResults()), and as ds!=null, you get a new connection, create statement and hence everything works fine.
But looks like from what you have posted you dont close the connection or statement or resultset (or do you call close() from the calling class?). Whatever be the case, second time you dont seem to have a valid handle to datasource, and the line results = st.executeQuery(Query); runs irrespective of state of data source.
You will have to handle the case if ds is null, if so you have to call getResults() to get data source

(Also why do you have con.close(); in the getResults() method? And you might want to call getResults() as getDataSource() for clarity)

Thanks
Padma
Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Thanks friends,
I am calling the close() method from my jsp page,after I have finished with all my processing.I don't know why it's throwing the error..can you give me any other clue that might help.
Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Padma,
I've removed the con.close() method from getResultSet() and i also removed the close() call from my jsp,which calls the close() method in the calling class,which calls the close() method in this class.

Whatever be the case, second time you dont seem to have a valid handle to datasource, and the line results = st.executeQuery(Query); runs irrespective of state of data source.
You will have to handle the case if ds is null, if so you have to call getResults() to get data source

I think ,this might be the error,can you please tell me how to handle this??
Padma Lalwani
Ranch Hand

Joined: Nov 02, 2004
Posts: 49
Hi,
You will need to add the close call back to the jsp, this will close the connection to the db after you are done with processing the results.

For you error, all you need to do is add a checking for if ds is null in the first line
if(ds==null) ds= getResults();
and then continue the same code as before and please include
results = st.executeQuery(Query); inside the check for valid connection.
How can you run a statement if you dont have a connection?
You need a statement to execute query, a connection to create statement & a data source to get connection from, so all steps must complete correctly to be able to run query

And of course this is a quick fix & not best way to do it. (you dont even have to do null checking as you are not persisting the object between method invocations, unless you are doing connection pooling which i dont see, so everyting will be null until initialized)
Hope this helps,
Padma
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

This sounds like JDBC, so I'm moving it to our JDBC forum. The nice folks over on that part of the ranch will perhaps have more insights.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Statement Closed Error - please help
 
Similar Threads
missing return statement error
Database - how to increase the access time
Ned to change servlet code to pure java
Database access, potential threading problem?
Exception coming while connecting to database