aspose file tools*
The moose likes JDBC and the fly likes can i use the resultset simultaneously? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "can i use the resultset simultaneously?" Watch "can i use the resultset simultaneously?" New topic
Author

can i use the resultset simultaneously?

Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
iam using the same resultset simultaneously but iam closing it before using it next time..will it create any problem

Iam getting ResultSet is closed exception...
please tell me where exactly iam doing wrong...here is the code..

public String update(String studentID,String email,String address,String contactNumber)
{
try
{
rs=st.executeQuery("select count(*) from memdetails where memid='"+studentID+"'");
while(rs.next())
{
counter=rs.getInt(1);
}
if(counter>0)
{
st.executeUpdate("update memdetails set emailid='"+email+"',address='"+address+"',phno='"+contactNumber+"' where memid='"+studentID+"'");
rs.close();
rs=st.executeQuery("select count(*) from SC_UpdateSmartcard where memid='"+studentID+"' and file_id='3003'");
while(rs.next())
{
counter=rs.getInt(1);
}
if (counter>0)
{
st.executeUpdate("update SC_UpdateSmartcard set status='y' where memid='"+studentID+"' and file_id='3003'");
}
else
{
st.executeUpdate("insert into SC_UpdateSmartcard(student_rollno,file_id ,status) values ('"+studentID+"','3003','y')");
}
return "Data Updated Successfully";
}
else
{
return "Please Enter User Personal Data 1";
}
}
catch(Exception e)
{
System.out.println("3003:"+e.getMessage());
return "Exception Encountered";
}
finally
{
try
{
rs.close();
}
catch(Exception e)
{
System.out.println("3003:"+e.getMessage());
}
}
}

The exception is being caugth in the catch and finally block.........
can any one please tell me where exactly iam doing wrong


Abhishek
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

If you closed result set , then you can not use it anymore. Reusing the same result set possible, however:
1. it will manage the same result data
2. it can be memory costly operation for some JDBC drivers, for example Oracle driver suck entire result memory allowing you to scroll over it bi directionally.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
i dont want resuming the same resultset.. when i create another resultset...as in the code will it create any problems or else shall i make it to null i.e rs=null; before creating another resultset with the same reference... Also can you tell me how to know whether a result set is closed or not..

Now i changed little bit i didnot closed resultset anywhere except in the finally block..but it is throwing ResultSet is closed exception
in the finally block

public String update(String studentID,String email,String address,String contactNumber)
{
try
{
rs=st.executeQuery("select count(*) from memdetails where memid='"+studentID+"'");
if(rs.next())
{
counter=rs.getInt(1);
}
if(counter>0)
{
st.executeUpdate("update memdetails set emailid='"+email+"',address='"+address+"',phno='"+contactNumber+"' where memid='"+studentID+"'");
System.out.println("rsd"+rs);
rs=null;
/*updating the editing information into the table*/
rs=st.executeQuery("select count(*) from SC_UpdateSmartcard where student_rollno='"+studentID+"' and file_id='3003'");
while(rs.next())
{
counter=rs.getInt(1);
}
if (counter>0)
{
st.executeUpdate("update SC_UpdateSmartcard set status='y' where student_rollno='"+studentID+"' and file_id='3003'");
}
else
{
st.executeUpdate("insert into SC_UpdateSmartcard(student_rollno,file_id ,status) values ('"+studentID+"','3003','y')");
}
return "Data Updated Successfully";
}
else
{
return "Please Enter User Personal Data 1";
}
}
catch(Exception e)
{
System.out.println("3003:"+e.getMessage());
return "Exception Encountered";
}
finally
{
try
{
rs.close();
}
catch(Exception e)
{
System.out.println("3003:"+e.getMessage());
}
}
}

i modified little bit..it is giving ResultSet is closed in the finally block.. can you give me the reason for that
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Old result set will be collected by GC, as only you assign your resultset variable a new value. No nulling required.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hello Abhishek Reddy Chepyala,

Can you please post your code using [CODE] tags so that it will look clear?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42278
    
  64
Drivers generally permit only a single ResultSet per Statement. So if you submit a Statement.executeUpdate, any ResultSet obtained previously through that Statement is invalidated.

Instead of doing an "rs.close" at the end, do a "st.close". It will close the ResultSet as well, if necessary.


Ping & DNS - my free Android networking tools app
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Yes, thats correct. Once the resultset is closed, you may not get its content back. Perhaps, it might be a costly operation as D Rog said.

Please have a look at this thread for using [CODE] tags!
[ July 20, 2007: Message edited by: Raghavan Muthu ]
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
thanks for the reply.......i have replaced rs.close() with st.close() updated successfully but while reading it is giving Invalid Handle Exception..
when replace back st.close() with rs.close() it is working why iam unable to understand
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can i use the resultset simultaneously?