我有这样的代码,
Let m = 7;
for(int i=0, i<m, i++){
con.setAutoCommit(false)
PreparedStatement ps = con.prepareStatement("INSERT INTO Numbers VALUES (i, "This is sparta")");
ps.executeUpdate();
con.commit();
con.setAutoCommit(true);
}
现在的问题是,我的数据库表看起来像这样......
Number Name
________________________________
1234567 This is sparta
而不是......
Number Name
_________________________________
1 This is sparta
2 This is sparta
3 This is sparta
4 This is sparta
5 This is sparta
6 This is sparta
7 This is sparta
我哪里出错了?
由于
答案 0 :(得分:2)
即使你使它工作,这也不是一个准备好的声明。你应该使用值占位符 充分利用PreparedStatement
con.setAutoCommit(false);
final String value = "This is Xerxes";
PreparedStatement ps = con.prepareStatement("INSERT INTO Numbers VALUES (?,?)");
for (int i=0, i<m, i++){
ps.setInt(1,i);
ps.setString(2,value);
ps.executeUpdate();
}
ps.close();
con.commit();
在所有insert语句完成后,您应该将其设置为单个提交