一下是关于JDBC批处理的一个程序测试
Nobatch
package JDBCBatch;
import java.sql.*;
public class TestCommon {
static long startTime;
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
PreparedStatement ps = null;
try {
startTime=System.nanoTime(); //获取开始时间
ps = conn
.prepareStatement("INSERT INTO batchtab values (?, ?)");
conn.setAutoCommit(false);
for (int n = 0; n < 10000; n++) {
Integer i = new Integer(n);
ps.setString(1, i.toString());
ps.setString(2, "value" + i.toString());
ps.executeUpdate();
}
mit();
long endTime=System.nanoTime(); //获取结束时间
System.out.println("程序运行时间: "+(endTime-startTime)+"ns");
}catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("Message: " + ex.getMessage());
System.out.println("Vendor error code: " + ex.getErrorCode());
} catch (Exception e) {
e.printStackTrace();
System.err.println("Exception: " + e.getMessage());
} finally {
if (conn != null)
conn.close();
if (ps != null)
ps.close();
}
}
public static Connection getConnection() {
Connection con = null; //创建用于连接数据库的Connection对象
try {
Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动
con = DriverManager.getConnection("jdbc:mysql://localhost/hibernate",
"root","123");
} catch (Exception e) {
System.out.println("数据库连接失败" + e.getMessage());
}
return con; //返回所建立的数据库连接
}
}
下面是使用批处理的
package JDBCBatch;
import java.sql.*;
public class TestPreStatementBatch {
static long startTime;
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
ResultSet rs = null;
PreparedStatement ps=null;
try {
startTime=System.nanoTime(); //获取开始时间
ps = conn.prepareStatement("INSERT INTO batchtab values (?, ?)");
conn.setAutoCommit(false);
ps.clearBatch();
for (int n=0; n<10000; n++) {
Integer i = new Integer(n);
ps.setString(1, i.toString());
ps.setString(2, "value" + i.toString());
ps.addBatch();
}
ps.executeBatch();
mit();
long endTime=System.nanoTime(); //获取结束时间
//打印消耗时间
System.out.println("程序运行时间: "+(endTime-startTime)+"ns");
} catch (BatchUpdateException b) {
System.out.println("SQLException: " + b.getMessage());
System.out.println("SQLState: " + b.getSQLState());
System.out.println("Message: " + b.getMessage());
System.out.println("Vendor error code: " + b.getErrorCode());
System.out.print("Update counts: ");
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("Message: " + ex.getMessage());
System.out.println("Vendor error code: " + ex.getErrorCode());
} catch (Exception e) {
e.printStackTrace();
System.err.println("Exception: " + e.getMessage());
} finally {
if( conn != null )
conn.close();
if(ps !=null)
ps.close();
if(rs !=null)
rs.close();
}
}
public static Connection getConnection() {
Connection con = null; //创建用于连接数据库的Connection对象
try {
Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动
con = DriverManager.getConnection("jdbc:mysql://localhost/hibernate",
"root","123");
} catch (Exception e) {
System.out.println("数据库连接失败" + e.getMessage());
}
return con; //返回所建立的数据库连接
}
}
下面是插入10W条数据的测试
批处理:902111464ns
普通插入:1030343361ns
代码的主要区别:
这个是批处理的:
for (int n=0; n<10000; n++) {
Integer i = new Integer(n);
ps.setString(1, i.toString());
ps.setString(2, "value" + i.toString());
ps.addBatch();
}
ps.executeBatch();
这个是不使用批处理的:
for (int n = 0; n < 10000; n++) {
Integer i = new Integer(n);
ps.setString(1, i.toString());
ps.setString(2, "value" + i.toString());
ps.executeUpdate();
}