Mutiple ResultSet objects with same Statement object

T

teser3

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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
 
T

teser3

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

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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

That is one way.

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

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top