1、Connection创建类
package com.abin.lee.db.oracle.batch;
import java.sql.Connection;
import java.sql.DriverManager;
public class OracleConnection {
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
private static final String URL="jdbc:oracle:thin:@localhost:1521:XE";
private static final String USER="abin";
private static final String PWD="abin";
private static final String DRIVER="oracle.jdbc.driver.OracleDriver";
public static Connection getConnection(){
Connection conn=null;
if(null==threadLocal.get()){
try {
Class.forName(DRIVER);
conn=DriverManager.getConnection(URL, USER, PWD);
threadLocal.set(conn);
} catch (Exception e) {
e.printStackTrace();
}
}else{
conn=threadLocal.get();
}
return conn;
}
}
2、测试语句
package com.abin.lee.db.oracle.batch;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import junit.framework.TestCase;
public class OracleBatch extends TestCase {
public void testinsert() throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
conn = OracleConnection.getConnection();
String sql = "insert into abing(id,name) values (?,?)";
ps = conn.prepareStatement(sql);
System.out.println("start="
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")
.format(new java.util.Date()));
try {
for (int i = 0; i <= 50000; i++) {
ps.setObject(1, i);
ps.setObject(2, "abin" + i);
ps.addBatch();
if (i % 10000 == 0) {
// System.out.println("i="+i);
ps.executeBatch();
ps.clearBatch();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("end="
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")
.format(new java.util.Date()));
}
}