87 lines
6.9 KiB
HTML
87 lines
6.9 KiB
HTML
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
<!DOCTYPE html
|
||
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<html lang="en-us" xml:lang="en-us">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<meta name="security" content="public" />
|
||
|
<meta name="Robots" content="index,follow" />
|
||
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
||
|
<meta name="DC.Type" content="reference" />
|
||
|
<meta name="DC.Title" content="Statement batch update" />
|
||
|
<meta name="abstract" content="To perform a Statement batch update, you must turn off auto-commit. In Java Database Connectivity (JDBC), auto-commit is on by default. Auto-commit means any updates to the database are committed after each SQL statement is processed. If you want to treat a group of statements being handed to the database as one functional group, you do not want the database committing each statement individually. If you do not turn off auto-commit and a statement in the middle of the batch fails, you cannot roll back the entire batch and try it again because half of the statements have been made final. Further, the additional work of committing each statement in a batch creates a lot of overhead." />
|
||
|
<meta name="description" content="To perform a Statement batch update, you must turn off auto-commit. In Java Database Connectivity (JDBC), auto-commit is on by default. Auto-commit means any updates to the database are committed after each SQL statement is processed. If you want to treat a group of statements being handed to the database as one functional group, you do not want the database committing each statement individually. If you do not turn off auto-commit and a statement in the middle of the batch fails, you cannot roll back the entire batch and try it again because half of the statements have been made final. Further, the additional work of committing each statement in a batch creates a lot of overhead." />
|
||
|
<meta name="DC.Relation" scheme="URI" content="batchupd.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="batchpre.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="batchexc.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="batchblo.htm" />
|
||
|
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
|
||
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
|
||
|
<meta name="DC.Format" content="XHTML" />
|
||
|
<meta name="DC.Identifier" content="batchstm" />
|
||
|
<meta name="DC.Language" content="en-us" />
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
||
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
||
|
<title>Statement batch update</title>
|
||
|
</head>
|
||
|
<body id="batchstm"><a name="batchstm"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Statement batch update</h1>
|
||
|
<div><p>To perform a Statement batch update, you must turn off auto-commit.
|
||
|
In Java™ Database
|
||
|
Connectivity (JDBC), auto-commit is on by default. Auto-commit means any updates
|
||
|
to the database are committed after each SQL statement is processed. If you
|
||
|
want to treat a group of statements being handed to the database as one functional
|
||
|
group, you do not want the database committing each statement individually.
|
||
|
If you do not turn off auto-commit and a statement in the middle of the batch
|
||
|
fails, you cannot roll back the entire batch and try it again because half
|
||
|
of the statements have been made final. Further, the additional work of committing
|
||
|
each statement in a batch creates a lot of overhead.</p>
|
||
|
<div class="section"><p> See <a href="transactions.htm">Transactions</a> for more
|
||
|
details.</p>
|
||
|
<p>After turning off auto-commit, you can create a standard Statement
|
||
|
object. Instead of processing statements with methods such as executeUpdate,
|
||
|
you add them to the batch with the addBatch method. Once you have added all
|
||
|
the statements you want to the batch, you can process all of them with the
|
||
|
executeBatch method. You can empty the batch at anytime with the clearBatch
|
||
|
method.</p>
|
||
|
<p>The following example shows how you can use these methods:</p>
|
||
|
<p><strong>Example:</strong> Statement
|
||
|
batch update</p>
|
||
|
<div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm">Code example
|
||
|
disclaimer</a> for important legal information.</div>
|
||
|
<blockquote><pre>connection.setAutoCommit(false);
|
||
|
Statement statement = connection.createStatement();
|
||
|
statement.addBatch("INSERT INTO TABLEX VALUES(1, 'Cujo')");
|
||
|
statement.addBatch("INSERT INTO TABLEX VALUES(2, 'Fred')");
|
||
|
statement.addBatch("INSERT INTO TABLEX VALUES(3, 'Mark')");
|
||
|
int [] counts = statement.executeBatch();
|
||
|
connection.commit();</pre>
|
||
|
</blockquote>
|
||
|
<p>In this example, an array of integers
|
||
|
is returned from the executeBatch method. This array has one integer value
|
||
|
for each statement that is processed in the batch. If values are being inserted
|
||
|
into the database, the value for each statement is 1 (that is, assuming successful
|
||
|
processing). However, some of the statements may be update statements that
|
||
|
affect multiple rows. If you put any statements in the batch other than INSERT,
|
||
|
UPDATE, or DELETE, an exception occurs.</p>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="batchupd.htm" title="Batch update support allows any updates to the database to be passed as a single transaction between the user program and the database. This procedure can significantly improve performance when many updates must be performed at once.">Batch updates</a></div>
|
||
|
</div>
|
||
|
<div class="relconcepts"><strong>Related concepts</strong><br />
|
||
|
<div><a href="batchexc.htm" title="An important consideration of batch updates is what action to take when a call to the executeBatch method fails. In this case, a new type of exception, called BatchUpdateException, is thrown. The BatchUpdateException is a subclass of SQLException and it allows you to call all the same methods you have always called to receive the message, the SQLState, and vendor code.">BatchUpdateException</a></div>
|
||
|
<div><a href="batchblo.htm" title="You can use a blocked insert is an iSeries operation to insert several rows into a database table at a time.">Blocked insert support</a></div>
|
||
|
</div>
|
||
|
<div class="relref"><strong>Related reference</strong><br />
|
||
|
<div><a href="batchpre.htm" title="A preparedStatement batch is similar to the Statement batch; however, a preparedStatement batch always works off the same prepared statement, and you only change the parameters to that statement.">PreparedStatement batch update</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|