PreparedStatement批量更新关键代码 无 import java.sql.Connection;import java.sql.PreparedStatement; //...String sql = "insert into employee (name, city, phone) values (?, ?, ?)";Connection connection = new getConnection();PreparedStatement p
PreparedStatement批量更新关键代码
import java.sql.Connection;
import java.sql.PreparedStatement;
//...
String sql = "insert into employee (name, city, phone) values (?, ?, ?)";
Connection connection = new getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
for (Employee employee: employees) {
ps.setString(1, employee.getName());
ps.setString(2, employee.getCity());
ps.setString(3, employee.getPhone());
ps.addBatch();
}
ps.executeBatch();
ps.close();
connection.close();
本文原创发布php中文网,转载请注明出处,感谢您的尊重!
本文介绍了一种使用PreparedStatement进行批量更新数据库的方法,通过设置参数并调用addBatch()和executeBatch()方法来提高插入效率。
472

被折叠的 条评论
为什么被折叠?



