您的位置:首页 > 数据库

通过PreparedStatement批量执行sql语句【sql语句相同,值不同】

2015-07-17 14:11 323 查看
比如说:我有一个List需要添加到数据库中,那么我该如何通过PreparedStatement来操作呢?


public void addCustomerByCommit(Connection conn , List<Customer> customerList)
{
String sql = "inseret into customer(id , name , remark)values(?,?,?)";
try
{
PreparedStatement ps = conn.prepareStatement(sql);
for(Customer customer :customerList){
int index = 1;
ps.setInt(index++ , customer.getId())
ps.setString(index++, customer.getName());
ps.setString(index++, customer.getRemark());
ps.addBatch();
}
ps.executeBatch();
}
catch (SQLException e)
{
//这里呢你可以做点自己想做的事情
e.printStackTrace();
}
}
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: