package pub.qingyun;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestBatchSaveDemo {
private static final Integer SUBMIT_NUM = 5000;
private static String url = "";
private static String password = "";
private static String username = "";
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, password);
conn.setAutoCommit(false);
String sql = "insert into t_user (id ,name, ...) valuse (? ,? ,...)";
ps = conn.prepareStatement(sql);
int count = 0;
for (int i = 0; i < 200000; i++) {
ps.setString(1, "***");
ps.setString(2, "***");
ps.setString(3, "***");
ps.addBatch();
count++;
if (count % SUBMIT_NUM == 0) {
ps.executeBatch();
ps.clearBatch();
System.out.println("count:" + count);
}
}
ps.executeBatch();
ps.clearBatch();
conn.commit();
System.out.println("Done!");
} catch (Exception e) {
try {
assert conn != null;
conn.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.setAutoCommit(true);
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}