I want to execute the multiple queries or job in one execute. Something like this eg:
String query="select * from tab1;insert into tab1 values(...);update tab1..;delete from tab1...;"
Statement st = con1.createStatement();
ResultSet rs = st.executeQuery(query);
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.
you can achieve that using Following example uses addBatch & executeBatch commands to execute multiple SQL commands simultaneously.
Batch Processing allows you to group related SQL statements into a batch and submit them with one call to the database. reference
When you send several SQL statements to the database at once, you reduce the amount of communication overhead, thereby improving performance.
DatabaseMetaData.supportsBatchUpdates()
method to determine if the target database supports batch update processing. The method returns true if your JDBC driver supports this feature.executeBatch()
is used to start the execution of all the statements grouped together.addBatch()
method. However, you cannot selectively choose which statement to remove.EXAMPLE:
refer http://www.tutorialspoint.com/javaexamples/jdbc_executebatch.htm
I'm not sure that you want to send two SELECT statements in one request statement because you may not be able to access both
ResultSet
s. The database may only return the last result set.Multiple ResultSets
However, if you're calling a stored procedure that you know can return multiple resultsets something like this will work
Multiple SQL Statements
If you're talking about multiple SQL statements and only one SELECT then your database should be able to support the one
String
of SQL. For example I have used something like this on SybaseThis will depend on the syntax supported by your database. In this example note the addtional
spaces
padding the statements so that there is white space between the staments.