JDBC Batch Processing

In JDBC batch processing we can execute a batch (group) of sql statements, instead of a single sql statement. It makes the faster performance.

The java.sql.Statement and java.sql.PreparedStatement interfaces provide two methods for batch processing, such as addBatch() method and executeBatch() method.

Method                                                                     Description
void addBatch(String query). It adds query into batch.
int[] executeBatch(). It executes the batch of queries.

Let's see a demo program for batch processing in jdbc.

import java.sql.*;
class JdbcDemo5
{
public static void main(String args[])throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
con.setAutoCommit(false);
Statement st=con.createStatement();
st.addBatch("insert into student values(1o1,'akshay')");
st.addBatch("insert into user420 values(191,'santosh')");
st.executeBatch(); //executing the batch
con.commit();
con.close();
}
}

If you see the table student, two records has been added.

first we will create a table student as : create table student(regdno varchar2(25), name char(45));

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment