Groups > DB2 > DB2 on AS400 server > How to enable Batch insert using AS400JDBC Driver




How to enable Batch insert using AS400JDBC Driver

How to enable Batch insert using AS400JDBC Driver
Tue, 25 Mar 2008 17:36:49 EDT
Hi<br />
I have a Java Program and wanted to insert say 10000 rows into a table. In Java
I Loop through an array and use addBatch() and after the loop i do an
executeBatch(). But it performs slow and takes the same time as
executeUpdate().<br />
<br />
I understand that the batch Insert need to be enabled, please find below the
code. Any code samples or corrections to the below code really appreciated and
will help.<br />
<br />
<u>Create Connection code</u><br />
import java.sql.Connection;<br />
<br />
AS400JDBCDataSource objDataSource = new
AS400JDBCDataSource("servername");<br />
objDataSource.setDatabaseName("schemaname");<br />
objDataSource.setBlockCriteria(2);<br />
objDataSource.setBlockSize(512);<br />
objDataSource.setPrefetch(true);<br />
<br />
try {<br />
<br />
Connection conn =
objDataSource.getConnection("userid","password");<br
/>
}<br />
<br />
<u>Code to create prepared statement and adding to batch</u><br
/>
PreparedStatement preparedStatement = null;<br />
conn.setAutoCommit(false);<br />
preparedStatement = conn.prepareStatement(strQuery);<br />
<br />
for loop<br />
   set values to prepared statement <br />
   preparedStatement.addBatch();<br />
end for loop<br />
<br />
preparedStatement.executeBatch();<br />
conn.commit();<br />
<br />
Note that only the data source is As400jdbcdatasource<br />
the connection and prepared statements are java.sql package.<br />
<br />
Post Reply
about | contact