Batch Update in Java with Example Program. This Java Batch Update DB (database) example describes the basic operations performed on the JDBC Batch Update database connectivity code.In this example you will learn about how to write java code for Batch Update with oracle database.
Program:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class BatchUpdationDemo {
public static void main(String args[]){
Connection con=null;
Statement st = null;
try {
Class.forName(“oracle.jdbc.driver.OracleDriver”);
con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:ORCL”,”scott”,”tiger”);
st = con.createStatement();
st.addBatch(“insert into eshusoft values(‘myuser’,’mypwd’)”);
st.addBatch(“update eshusoft set password=’mypwd2′
where username=’myuser'”);
st.addBatch(“delete from eshusoft where username=’myuser'”);
int b[] = st.executeBatch();
System.out.print(“rows updated: “+ b.length);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Result:
rows updated : 3