1.Statement()批量处理(11119毫秒10000条数据)
@Test
public void testStatement() throws Exception {
Connection connection = null;
Statement statement =null;
try {
connection = JDBCTools.getConnection();
statement = connection.createStatement();
String sql = "";
long begin = System.currentTimeMillis();
for(int i = 0; i< 10000; i++){
sql = "INSERT INTO CUSTOMERSS(ID ,NAME ,EMAIL ,BRITH,CORE ) VALUES ("+(i+1)+",'"+(i+1)+"_name','"+(i+1)+"_EMAIL','19-04-15',100)";
System.out.println(sql);
statement.executeUpdate(sql);
}
long end = System.currentTimeMillis();
System.out.println(end-begin);
} catch (Exception e) {
e.printStackTrace();
} fially {
JDBCTools.colse(statement, connection, null);
}
}
2.PrepareStatement()批量处理(1125毫秒10000条数据)
@Test
public void testPrepareStatement() throws Exception {
Connection connection = null;
PreparedStatement preparedStatement =null;
try {
connection = JDBCTools.getConnection();
JDBCTools.begin(connection);
String sql = "INSERT INTO CUSTOMERSS(ID ,NAME ,EMAIL ,BRITH,CORE ) VALUES (?,?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
Date date = new Date(new java.util.Date().getTime());
long begin = System.currentTimeMillis();
for(int i = 0; i< 10000; i++){
preparedStatement.setInt(1, i+1);
preparedStatement.setString(2, "name");
preparedStatement.setString(3, "EMAI");
preparedStatement.setDate(4,date );
preparedStatement.setInt(5, 100);
System.out.println(sql);
preparedStatement.executeUpdate();
}
long end = System.currentTimeMillis();
System.out.println(end-begin);
JDBCTools.commit(connection);
} catch (Exception e) {
e.printStackTrace();
JDBCTools.rollback(connection);
} finally {
JDBCTools.colse(preparedStatement, connection, null);
}
}
3.Batch()批量处理(138毫秒10000条数据)
•当需要成批插入或者更新记录时。可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理。
通常情况下比单独提交处理更有效率
•JDBC的批量处理语句包括下面两个方法:
–addBatch(String):添加需要批量处理的SQL语句或是参数;
–executeBatch();执行批量处理语句;
@Test
public void testBatch() throws Exception {
Connection connection = null;
PreparedStatement preparedStatement =null;
try {
connection = JDBCTools.getConnection();
JDBCTools.begin(connection);
String sql = "INSERT INTO CUSTOMERSS(ID ,NAME ,EMAIL ,BRITH,CORE ) VALUES (?,?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
Date date = new Date(new java.util.Date().getTime());
long begin = System.currentTimeMillis();
for(int i = 0; i< 10000; i++){
preparedStatement.setInt(1, i+1);
preparedStatement.setString(2, "name");
preparedStatement.setString(3, "EMAI");
preparedStatement.setDate(4,date );
preparedStatement.setInt(5, 100);
//积攒SQL
preparedStatement.addBatch();
//达到一定数据后发送SQL
if ((i+1)%300 == 0 ) {
//执行
preparedStatement.executeBatch();
//清空
preparedStatement.clearBatch();
}
}
if (10000 % 300 != 0 ) {
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
long end = System.currentTimeMillis();
System.out.println(end-begin);
JDBCTools.commit(connection);
} catch (Exception e) {
e.printStackTrace();
JDBCTools.rollback(connection);
} finally {
JDBCTools.colse(preparedStatement, connection, null);
}
}