Java J2ME JSP J2EE Servlet Android

Java batch sql execution example

Here is an example of execution batch sql.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* This is a demo class for batch execution example
*
* @author Sharif Uddin
*
*/
public class BatchExmpl {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}
public void batchTest(){
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection( "jdbc:mysql://localhost:3307/mysql", "root", "");

String sql = "INSER INTO mytab(id,name) VALUES(?,?)";
PreparedStatement pstmt = connection.prepareStatement(sql);
for(int i=1;i<10;i++){
pstmt.setLong(1, i);
pstmt.setString(2, "Name_"+i);
pstmt.addBatch();
}
int updateCount[] = pstmt.executeBatch();
for(int i=0;i
System.out.println(updateCount[i]);
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
}

}