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));
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc