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

比如说:我有一个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();
        }
}

你可能感兴趣的:(sql,事务,批量执行)