67,549
社区成员




public static void main(String[] args) throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager
.getConnection("jdbc:mysql://localhost/wazhu?user=root&password=root");
long begin = System.currentTimeMillis();
connection.setAutoCommit(false);
PreparedStatement pst = connection
.prepareStatement("insert into test1 values(?)");
for (int i = 0; i < 10000; i++) {
if (i == 5)
pst.setInt(1, i);
else
pst.setString(1, "aaaa");
pst.addBatch();
}
pst.executeBatch();
connection.commit();
System.out.println("cost:" + (System.currentTimeMillis() - begin));
} catch (Exception e) {
e.printStackTrace();
}
}
Statement st = connection.createStatement();
st.execute("SET autocommit=0");
st.close();
执行速度是很快了,问题是执行完pst.executeBatch();
数据库里面没数据,也就是说还是要显示的调connection.commit