• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Statement Closed Error - please help

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You showed up just in time for the waffles! And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic