Statement Batch
STATEMENT EXECUTE can only perform a SQL statement at a time, if there are multiple SQL statements to be executed, you can use the executebatch () method to perform multiple SQL statements in a method call. To increase the execution efficiency! First use Addbatch ( Methods to join the SQL statement to be executed, then execute executebatch ():
Statement Stmt = conn.createstatement ();
Stmt.addbatch ("INSERT INTO Message Values ('Michael Chen',"
"'blog.9cbs.net/ybugchen', 'message',"
"2005-2-4 ',' Welcome ')");
Stmt.addbatch ("INSERT INTO Message Values ('Jim',"
"'blog.9cbs.net/ybugchen', 'message',"
"2005-2-4 ',' Welcome ')");
Stmt.addbatch ("INSERT INTO Message Values ('Susan',"
"'blog.9cbs.net/ybugchen', 'message',"
"2005-2-4 ',' Welcome ')");
Stmt.executebatch ();
In the case of Connection automatic [recognition] (COMMIT), executes ExecuteBatch (), update the database every time SQL is executed, if there is a row of SQL in the middle, the previous SQL statement is updated to the database. It has taken into effect, and the statement after the error will not be executed, if you want to make sure all the Batch SQL statements can be executed, you can set the [license] of the connection to false, after the executebatch () method is successful, execute the commit () method confirmation Update to the database.
Conn.setautocommit (false);
Statement Stmt = conn.createstatement ();
Stmt.addbatch ("INSERT INTO Message Values ('Michael Chen',"
"'blog.9cbs.net/ybugchen', 'message',"
"2005-2-4 ',' Welcome ')");
Stmt.addbatch ("INSERT INTO Message Values ('Jim',"
"'blog.9cbs.net/ybugchen', 'message',"
"2005-2-4 ',' Welcome ')");
Stmt.addbatch ("INSERT INTO Message Values ('Susan',"
"'blog.9cbs.net/ybugchen', 'message',"
"2005-2-4 ',' Welcome ')");
Stmt.executebatch ();
CONN.COMMIT ();
If you perform executebatch () If the SQL error occurs, it will throw the BatchUpdateException exception, which can get the correct SQL Rock by the getUpdateCounts () method of this exception component.
{
CONN.ADDBATCH (...)
....
Stmt.executebatch ();
}
Catch (BatchUpdateException E)
{
System.out.println ("Successfully executed SQL statement bar =" E.getupdatecounts ());
}