Ikk*_*kki 1 java mysql batch-file prepared-statement
我正在尝试使用preparedStatement Batch,但我遇到了问题.
以下代码不会给我错误,但它只在表中插入地图的最后一个键,我不知道为什么.
这肯定是一个非常愚蠢的错误,但这是我第一次使用addBatch()方法.
Class.forName("com.mysql.jdbc.Driver");
this.connect = DriverManager.getConnection("jdbc:mysql://localhost/" + this.database + "?user=" + this.user + "&password=" + this.password);
String s;
for (String key : this.map.keySet())
{
s = ("insert into " + this.database + ".user (nickname) values (?)");
this.preparedStatement = this.connect.prepareStatement(s);
this.preparedStatement.setString(1, key);
this.preparedStatement.addBatch();
}
this.preparedStatement.executeBatch();
Run Code Online (Sandbox Code Playgroud)
提前致谢!
在循环之外准备语句和查询:
s = ("insert into " + this.database + ".user (nickname) values (?)");
this.preparedStatement = this.connect.prepareStatement(s);
for (String key : this.map.keySet())
{
this.preparedStatement.setString(1, key);
this.preparedStatement.addBatch();
}
this.preparedStatement.executeBatch();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2628 次 |
| 最近记录: |