File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes PreparedStatement ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "PreparedStatement ???" Watch "PreparedStatement ???" New topic

PreparedStatement ???

Sanjeev Shahi
Ranch Hand

Joined: Feb 26, 2001
Posts: 83
Can we use more than one SQL im prepraredStatement? If yes please give example
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
I'm not sure I understand your question. Can you use more than one preparedStatement in your code?
Sure, just make sure you give each one a different variable name or you'll just keep rewrtining over the previous one.

Or are you asking if you can have more than one SQL statment associated with a preparedStatement?
The answer to that is yes, but it might defeat the purpose of the preparedStatement. IF you create a single preparedStatement in your class and keep switching the code in it then you loose the benefit of having a prepared statement. If, for example, you have an insert statement in your preparedStatement and you use it then create a new preparedStatement with the same variable name you've just lost the first one.
If your statements are for one use only then you might not need the preparedStatemnet at all - unless you set them all up when your class is first loaded and they just sit there and wait on the user, that would save a little overhead and time but probably not too much unless they are fairly lengthy queries.
hope that helps

Sanjeev Shahi
Ranch Hand

Joined: Feb 26, 2001
Posts: 83
Hi! dave ,
Thanks for replying.
preparedStatement ps1 = conn.prepareStatement("sql goes here");preparedStatement ps2 = conn.prepareStatement("sql goes here");preparedStatement ps3 = conn.prepareStatement("sql goes here");
Actually I meant can we put more than one SQL statement in the bracket.
PreparedStatment ps1 = con.prepareStatment(
select * from table1,
select* from table2);
I hope I am clear.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

you can only do this through the CallableStatement interface. You create the database stored procedure which can execute many sql statements and call it from the java program.
I agree. Here's the link:
subject: PreparedStatement ???
It's not a secret anymore!