This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes Multiple prepareStatement on one Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Multiple prepareStatement on one Connection" Watch "Multiple prepareStatement on one Connection" New topic
Author

Multiple prepareStatement on one Connection

Sidd Nakade
Greenhorn

Joined: Feb 20, 2006
Posts: 8
Hi,
how does multiple prepared statment work on one connection object.
Unknowingly I was using it for a while and it does work. Now while reviewing my code i was wondering how does this actually work.

Here is the code sample:
Connection box1Con = Commons.getBOX1DBConnection();
Connection box2Con = Commons.getBOX2DBConnection();
queryResource = readResource();

PrepareStatement ps1 = box1Con.prepareStatement(query1);
PrepareStatement ps1 = box2Con.prepareStatement(query2);

Thanks for the help in advance.
Sidd
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"Sidd Sidd ",
Please check your private messages.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Raees Uzhunnan
Ranch Hand

Joined: Aug 15, 2002
Posts: 126
There is no need to have two connection for 2 prepared statement!. You can open any number of prepared statements as long as you are closing it after closing the ResutSets

Whats up with Ben ? he is sending private messages !. post it man ; let others too read !

Raees


Sun Certified Enterprise Architect
Java Technology Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Originally posted by Raees Uzhunnan:
Whats up with Ben ? he is sending private messages !. post it man ; let others too read !

Raees
The PM is private because it is private information.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Originally posted by Campbell Ritchie:
The PM is private because it is private information.


The name "Sidd Sidd" might be a clue...


My Blog: Down Home Country Coding with Scott Selikoff
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Raees Uzhunnan,
Please check your private messages.
Raees Uzhunnan
Ranch Hand

Joined: Aug 15, 2002
Posts: 126
I apologize ben ; Yeah you are right ! if you are asking questions, be man enough !!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Originally posted by Scott Selikoff:


. . . might be a clue...
I was trying to keep quiet about that.
Sidd Nakade
Greenhorn

Joined: Feb 20, 2006
Posts: 8
I forgot to mention, I am using both the prepareStatements in a loop.
ex:
Connection box1Con = Commons.getBOX1DBConnection();
Connection box2Con = Commons.getBOX2DBConnection();
queryResource = readResource();

PrepareStatement ps1 = box1Con.prepareStatement(query1);
PrepareStatement ps1 = box2Con.prepareStatement(query2);

for(int i=0; i < 100; i++){
rs1 = ps1.executeQuery();
rs1.....
rs1.close();


rs2 = ps2.executeQuery();
rs2.....
rs2.close();

}


ps1.close();
ps2.close();


Thanks
Sidd N
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"Sidd N",
Please check your private messages, again.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Originally posted by Campbell Ritchie:
I was trying to keep quiet about that.


Well I've got to keep 'the conspiracy' under wraps.
Sidd Nakade
Greenhorn

Joined: Feb 20, 2006
Posts: 8
Raees Uzhunnan, as you mentioned using multiple prepared statements is not an issue if we close it after closing resultset. But in my example I am closing the result set and not closing the preparedstatment. Do you see this as an issue.
Raees Uzhunnan
Ranch Hand

Joined: Aug 15, 2002
Posts: 126
Not an issue ; you can call as many execute as possible on a statement before closing it. But make sure you are not creating as many statements for the same query

Raees
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple prepareStatement on one Connection