Tuesday, 5 February 2013

BATCH STATEMENT | Java J2EE Tutorial pdf

BATCH STATEMENT

A batch update is a set of multiple update statements that is submitted to the database for processing as a batch. Sending batch updates can, in some situations, be much more efficient than sending update statements separately. This ability to send updates as a unit, referred to as the batch update facility, is one of the features provided with the JDBC API. We can add SQL commands to this list with the method addBatch and empty it with the method clearBatch.When we have finished adding statements to the list, we call the method
executeBatch to send them all to the database to be executed as a unit, or batch
For example
import java.sql.*;
public class batchdemo
{
public static void main(String args[])throws Exception
{
Class.forName("weblogic.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection
("jdbc:bea:sqlserver://localhost:1433","java","java");
Statement st=con.createStatement();
con.setAutoCommit(false);
st.addBatch("insert into book values(04,'J2SE')");
st.addBatch("insert into book values(05,'J2EE')");
st.addBatch("insert into book values(06,'J2ME')");
st.executeBatch();
System.out.println("\nRecords Inserted");
con.commit();
con.setAutoCommit(true);
st.close();
con.close();
}
}

Table
bid               bname
----------- ---------------
1                   Pascal
2                   Cobol
3                   Fortran
Output

Table, After Execution
bid                 bname
----------- ---------------
1                   Pascal
2                  Cobol
3                  Fortran
4                   J2SE
5                   J2EE
6                   J2ME

No comments: