67,542
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
这个我试过了..还是达不到我描述的那种效果..
果然是这个问题!你太强了~
结贴!谢谢各位
mysql小白上不起呐
这样设置了还是不行啊,设置了没效果,查了下说对超级用户init_connect不起作用,
但是公司的电脑我还是用root登录的,而且我看了它配置里面也没有这个设置
然后我又试了连上数据库后执行一次
Statement st = connection.createStatement();
st.execute("SET autocommit=0");
st.close();
执行速度是很快了,问题是执行完pst.executeBatch();
数据库里面没数据,也就是说还是要显示的调connection.commit
第二个问题对了,因为windows默认的引擎是innodb,linux是myisam.....我改了现在很快了...
还有第一个问题不知道原因...等搞定了一起结贴...
好像是跟sql_mode有关系,我把它改成""了,自己手动在数据库里插数据就算类型错了都不会报错,但是用jdbc还是会抛异常...