Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

Mutiple ResultSet objects with same Statement object

Thread view: 
teser3@hotmail.com - 27 Sep 2007 23:08 GMT
Is it okay to create 3 different ResultSets with the same Statement
object? Here is what I am currently using in my Database statements
with Oracle and everything works great. But I am wondering if this
will create Database resource leakages or other issues:

Code:
//Db connection called from another Java class
.....
ResultSet results1 = null;
ResultSet results2 = null;
ResultSet results3 = null;
Statement statement = null;
......
       statement = connection.createStatement();
       results1 = statement.executeQuery("select name from
tableone");
       if(results1.next())
       {
            int myvar = ....
       }

       results2 = statement.executeQuery("select food, sugar from
tabletwo");
       if(results2.next())
       {
            int myvar2 = ....
       }

       results3 = statement.executeQuery("select rock from
tablethree");
       if(results3.next())
       {
            int myvar3 = ....
       }

       ...
       //Finally block here that closes results1, results2, results3,
statement and connection object references
Arne Vajhøj - 27 Sep 2007 23:29 GMT
> Is it okay to create 3 different ResultSets with the same Statement
> object? Here is what I am currently using in my Database statements
> with Oracle and everything works great. But I am wondering if this
> will create Database resource leakages or other issues:

It is OK, but I think you should close the previous result set before
opening a new one.

Arne
teser3@hotmail.com - 28 Sep 2007 00:42 GMT
On Sep 27, 6:29 pm, Arne Vajh?j <a...@vajhoej.dk> wrote:
> tes...@hotmail.com wrote:
> > Is it okay to create 3 different ResultSets with the same Statement
[quoted text clipped - 6 lines]
>
> Arne

You mean like this?

try {
results1 = statement.executeQuery("select name from
tableone");
       if(results1.next())
       {
            int myvar = ....
       }
       ....
       finally {
       //close results1 object only here

    try {
results2 = statement.executeQuery("select name from
tableone");
       if(results2.next())
       {
            int myvar = ....
       }
       ....
       finally {
       //close results2 object only here

try {
results3 = statement.executeQuery("select name from
tableone");
       if(results3.next())
       {
            int myvar = ....
       }
       ....
       finally {
       //close results3 object only here

       ...

       //Finally block here that closes results3,
statement and connection object references
Arne Vajhøj - 29 Sep 2007 01:41 GMT
>> tes...@hotmail.com wrote:
>>> Is it okay to create 3 different ResultSets with the same Statement
[quoted text clipped - 3 lines]
>> It is OK, but I think you should close the previous result set before
>> opening a new one.

> You mean like this?
>
[quoted text clipped - 35 lines]
>         //Finally block here that closes results3,
> statement and connection object references

That is one way.

Except that you should not close result in the fourth
finally if you do so in the third.

Arne


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.