Execute multiple SQL statements in java Execute multiple SQL statements in java sql sql

Execute multiple SQL statements in java

Following example uses addBatch & executeBatch commands to execute multiple SQL commands simultaneously.

import java.sql.*;public class jdbcConn {   public static void main(String[] args) throws Exception{      Class.forName("org.apache.derby.jdbc.ClientDriver");      Connection con = DriverManager.getConnection      ("jdbc:derby://localhost:1527/testDb","name","pass");      Statement stmt = con.createStatement      (ResultSet.TYPE_SCROLL_SENSITIVE,      ResultSet.CONCUR_UPDATABLE);      String insertEmp1 = "insert into emp values      (10,'jay','trainee')";      String insertEmp2 = "insert into emp values      (11,'jayes','trainee')";      String insertEmp3 = "insert into emp values      (12,'shail','trainee')";      con.setAutoCommit(false);      stmt.addBatch(insertEmp1);      stmt.addBatch(insertEmp2);      stmt.addBatch(insertEmp3);      ResultSet rs = stmt.executeQuery("select * from emp");      rs.last();      System.out.println("rows before batch execution= "      + rs.getRow());      stmt.executeBatch();      con.commit();      System.out.println("Batch executed");      rs = stmt.executeQuery("select * from emp");      rs.last();      System.out.println("rows after batch execution= "      + rs.getRow());   }} 

Result:The above code sample will produce the following result.The result may vary.

rows before batch execution= 6Batch executedrows after batch execution= = 9 

Source: Execute multiple SQL statements

In the abscence of the schema or the data contained in each table I'm going to make the following assumptions:

The table special_columns could look like this:


The table alldata2 could look like this:

column_1  | column_2  | column_3---------------------------------value_1_1 | value_2_1 | value_3_1value_1_2 | value_2_2 | value_3_2    

The table alldata should, after inserts have, happened look like this:


Given these assumptions you can copy the data like this:

try (  Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl1", "test", "oracle")){  StringBuilder columnNames = new StringBuilder();  try (    Statement select = connection.createStatement();    ResultSet specialColumns = select.executeQuery("SELECT column_name FROM special_columns");    Statement insert = connection.createStatement()  )  {    while (specialColumns.next())    {      int batchSize = 0;                   insert.addBatch("INSERT INTO alldata(colname) SELECT " + specialColumns.getString(1) + " FROM alldata2");       if (batchSize >= MAX_BATCH_SIZE)      {         insert.executeBatch();        batchSize = 0;      }    }    insert.executeBatch();  }

A couple of things to note:

  • MAX_BATCH_SIZE should be set to a value based on your database configuration and the data being inserted.
  • this code is using the Java 7 try-with-resources feature to ensure the database resources are released when they're finished with.
  • you haven't needed to do a Class.forName since the service provider mechanism was introduced as detailed in the JavaDoc for DriverManager.

There are two problems in your code. First you use the same Statement object (stmt) to execute the select query, and the insert. In JDBC, executing a statement will close the ResultSet of the previous execute on the same object.

In your code, you loop over the ResultSet and execute an insert for each row. However executing that statement will close the ResultSet and therefor on the next iteration the call to next() will throw an SQLException as the ResultSet is closed.

The solution is to use two Statement objects: one for the select and one for the insert. This will however not always work by default, as you are working in autoCommit (this is the default), and with auto commit, the execution of any statement will commit any previous transactions (which usually also closes the ResultSet, although this may differ between databases and JDBC drivers). You either need to disable auto commit, or create the result set as holdable over commit (unless that already is the default of your JDBC driver).