HI TO ALL, here i want to execute 2 query's using same connection and same statement and resulset. is there anything wrong in this issue? should i need to do create seperate connections and stmt and result for both query's
thanx in advace cinux [ April 01, 2006: Message edited by: Ernest Friedman-Hill ]
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Martin Simons
Ranch Hand
Joined: Mar 02, 2006
Posts: 196
posted
0
Using the same connection is of course fine, but you create the statement with the query, and the result set is created by executing the statement. So for the last two it cannot be the same. If you mean using the same variables, then yeah, no problem, but the items will not be "concatenated" or any such stuff. The variables will have one value (or set of values) for one query, and be completely different (new) items with the second query.
Roger Chung-Wee
Ranch Hand
Joined: Sep 29, 2002
Posts: 1683
posted
0
Just remember that creating two Statements, even if using the same reference variable, means two Statement objects - both of which should be closed in order to prevent cursor leaks.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
saikrishna cinux
Ranch Hand
Joined: Apr 16, 2005
Posts: 689
posted
0
yep i am using same variables for 2 different queries like ia m using rs for Resultset and con fro Connection stmt for Statement;
i am copying my program here exactly like this
con = getConnection (); stmt = con.createStatement (); query = "select COUNT(*) from testuser where totalscore<="+totalUserScore; System.out.println(query);
before the second query string I would place a rs.close( ) and stmt.close( ) as well as another stmt = con.createStatement( ) just to be sure. As far as reusing the variables no problem. Don't worry about the "extra" lines of code or the extra actions involved. An additional time needed to accomplish these are truely nominal.
Roger Chung-Wee
Ranch Hand
Joined: Sep 29, 2002
Posts: 1683
posted
0
The code looks as if it should work.
As you are creating two Resultset objects, then ensure that you close both.
On closing resources: it's a good idea to put close operations in a finally block to make sure they are called:
Follow the same pattern with Statement and ResultSet. Many frameworks introduce abstractions to simplify this. For example, in Spring one can write:
There is no emoticon for what I am feeling!
saikrishna cinux
Ranch Hand
Joined: Apr 16, 2005
Posts: 689
posted
0
Originally posted by Jeff Albertson:
Follow the same pattern with Statement and ResultSet. Many frameworks introduce abstractions to simplify this. For example, in Spring one can write: [/QB]
sir here i didn't get what u mean in the last lines of code.. what is this ocde for? it's little bit complicated code i couldn't get it please can u explain me once again clearly... thanking u