Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using JDBC preparedStatement in a batch

Tags:

Im using Statements batchs to query my data base. Iv'e done some research now and i want to rewrite my application to use preparedStatement instead but i'm having hard time to figure out how to add queries to a preparedStatement batch.

This is what i'm doing now:

private void addToBatch(String sql) throws SQLException{ sttmnt.addBatch(sql); batchSize++; if (batchSize == elementsPerExecute){     executeBatches(); } } 

where sttmnt is a class member of type Statement.

What i want to do is to use the preparedStatement's setString(int, String) method to set some dynamic data and then add it to the batch.

Unfortunately, i don't fully understand how it works, and how i can use setString(int, String) to a specific sql in the batch OR create a new preparedStatemnt for every sql i have and then join them all to one batch.

is it possible to do that? or am i really missing something in my understanding of preparedStatement?

like image 341
Boris C Avatar asked Jul 28 '11 14:07

Boris C


People also ask

What is batch in PreparedStatement?

You can load batches of data into Vertica using prepared INSERT statements—server-side statements that you set up once, and then call repeatedly. You instantiate a member of the PreparedStatement class with a SQL statement that contains question mark placeholders for data.

Can I use same PreparedStatement multiple times?

Once a PreparedStatement is prepared, it can be reused after execution. You reuse a PreparedStatement by setting new values for the parameters and then execute it again.

How does JDBC batch work?

Batch Processing allows you to group related SQL statements into a batch and submit them with one call to the database. When you send several SQL statements to the database at once, you reduce the amount of communication overhead, thereby improving performance.

How does JDBC batch update work?

A JDBC batch update is a batch of updates grouped together, and sent to the database in one batch, rather than sending the updates one by one. Sending a batch of updates to the database in one go, is faster than sending them one by one, waiting for each one to finish.


1 Answers

Read the section 6.1.2 of this document for examples. Basically you use the same statement object and invoke the batch method after all the placeholders are set. Another IBM DB2 example which should work for any JDBC implementation. From the second site:

try {   connection con.setAutoCommit(false);           PreparedStatement prepStmt = con.prepareStatement(         "UPDATE DEPT SET MGRNO=? WHERE DEPTNO=?");   prepStmt.setString(1,mgrnum1);               prepStmt.setString(2,deptnum1);   prepStmt.addBatch();                          prepStmt.setString(1,mgrnum2);                           prepStmt.setString(2,deptnum2);   prepStmt.addBatch();   int [] numUpdates=prepStmt.executeBatch();   for (int i=0; i < numUpdates.length; i++) {     if (numUpdates[i] == -2)       System.out.println("Execution " + i +          ": unknown number of rows updated");     else       System.out.println("Execution " + i +          "successful: " + numUpdates[i] + " rows updated");   }   con.commit(); } catch(BatchUpdateException b) {   // process BatchUpdateException }  
like image 74
Sanjay T. Sharma Avatar answered Jan 06 '23 20:01

Sanjay T. Sharma