Get your CodeRanch badge!*
The moose likes JDBC and the fly likes how to execute multiple different queries in one execute? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to execute multiple different queries in one execute?" Watch "how to execute multiple different queries in one execute?" New topic
Author

how to execute multiple different queries in one execute?

Sakina Shaikh
Greenhorn

Joined: Jun 09, 2012
Posts: 2
I want to execute the multiple queries or job in one execute. Something like this eg:




Or multiple select queries.Queries will be dynamic.

But I am not able to do this.What is the way to run multiple queries separated by semi colon.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29241
    
139

Sakina Shaikh wrote:But I am not able to do this.What is the way to run multiple queries separated by semi colon.

Don't. This isn't supported by all databases/drivers.

If you can only have one database roundtrip, you should use a stored procedure.

If you only had insert/update/delete, you could use addBatch()/executeBatch() to do all of them in one database roundtrip. Select can't be included though.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Sakina Shaikh
Greenhorn

Joined: Jun 09, 2012
Posts: 2
I have tried this:


Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29241
    
139

You generally want to state what problem you had and not just post the code you tried. I see two problems though:
1) You always use result[0] and not the index of the statement. Which means this runs the select statement repeatedly.
2) The statements could be run out of order if you have a select after the insert/update.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3435
    
  47

Welcome to the ranch, Sakina!

Have you achieved what you wanted?

I noticed you've fixed the result[0] bug pointed out by Jeanne in your post. To prevent a confusion, please refrain from editing posts others have already replied to in a manner that makes the replies meaningless; or at least put a note into the edited post so that the changes are obvious. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to execute multiple different queries in one execute?
 
Similar Threads
Table Accessing through indexes
Sybase to Oracle conversion. (SQL)
WITH clause generates ORA-01009: missing mandatory parameter
Using same object for multiple uses
Oracle XMLType data size