Batch Processing in JDBC:
Instead of executing a single query, we can execute a batch (group) of queries. It makes the performance fast.The java.sql.Statement and java.sql.PreparedStatement interfaces provide methods for batch processing.
Methods of Statement interface:
The required methods for batch processing are given below:
void addBatch(String query): It adds query into batch.
int[] executeBatch(): It executes the batch of queries.
Example of batch processing in jdbc
It follows following steps:
Load the driver class
Create Connection
Create Statement
Add query in the batch
Execute Batch
Close Connection
Example of batch processing using PreparedStatement:
import java.sql.*;
class GetRecords{
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 stmt=con.createStatement();
stmt.addBatch("insert into student values(123,'Vishwa','KEC')");
stmt.addBatch("insert into student values(124,'Alex','HKU')");
stmt.addBatch("insert into student values(125,'Karan','PTU')");
stmt.executeBatch(); //executing the batch
con.commit();
System.out.println("batch execute successfully");
con.close();
}
}
Outut: batch execute successfully
Example of batch processing using PreparedStatement:
import java.sql.*;
import java.io.*;
class TransactionMan {
public static void main(String []args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
con.setAutoCommit(false);
PreparedStatement ps=con.prepareStatement("insert into student values(?,?,?,?)");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("enter id");
String s1=br.readLine();
int id=Integer.parseInt(s1);
System.out.println("enter name");
String name=br.readLine();
System.out.println("enter salary");
String s3=br.readLine();
int salary=Integer.parseInt(s3);
System.out.println("enter address");
String s4=br.readLine();
ps.setInt(1,id);
ps.setString(2,name);
ps.setInt(3,salary);
ps.setInt(4,address);
ps.addBatch();
System.out.println("Want to add more records press y/n");
String ans=br.readLine();
if(ans.equals("n")){
break;
}
} // end of if block
con.commit();
System.out.println("records successfully added");
con.close(); //before closing connection commit() is called
}catch(Exception ex) {
System.out.println(ex); }
}
}
Output: records successfully added
Instead of executing a single query, we can execute a batch (group) of queries. It makes the performance fast.The java.sql.Statement and java.sql.PreparedStatement interfaces provide methods for batch processing.
Methods of Statement interface:
The required methods for batch processing are given below:
void addBatch(String query): It adds query into batch.
int[] executeBatch(): It executes the batch of queries.
Example of batch processing in jdbc
It follows following steps:
Load the driver class
Create Connection
Create Statement
Add query in the batch
Execute Batch
Close Connection
Example of batch processing using PreparedStatement:
import java.sql.*;
class GetRecords{
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 stmt=con.createStatement();
stmt.addBatch("insert into student values(123,'Vishwa','KEC')");
stmt.addBatch("insert into student values(124,'Alex','HKU')");
stmt.addBatch("insert into student values(125,'Karan','PTU')");
stmt.executeBatch(); //executing the batch
con.commit();
System.out.println("batch execute successfully");
con.close();
}
}
Outut: batch execute successfully
Example of batch processing using PreparedStatement:
import java.sql.*;
import java.io.*;
class TransactionMan {
public static void main(String []args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
con.setAutoCommit(false);
PreparedStatement ps=con.prepareStatement("insert into student values(?,?,?,?)");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("enter id");
String s1=br.readLine();
int id=Integer.parseInt(s1);
System.out.println("enter name");
String name=br.readLine();
System.out.println("enter salary");
String s3=br.readLine();
int salary=Integer.parseInt(s3);
System.out.println("enter address");
String s4=br.readLine();
ps.setInt(1,id);
ps.setString(2,name);
ps.setInt(3,salary);
ps.setInt(4,address);
ps.addBatch();
System.out.println("Want to add more records press y/n");
String ans=br.readLine();
if(ans.equals("n")){
break;
}
} // end of if block
con.commit();
System.out.println("records successfully added");
con.close(); //before closing connection commit() is called
}catch(Exception ex) {
System.out.println(ex); }
}
}
Output: records successfully added
No comments:
Post a Comment