[英]insert multiple values using prepared statement using loop java
I'm trying to insert multiple values in a mysql database using the iterative (for) but it always insert the first line and stops. 我试图使用迭代(for)在mysql数据库中插入多个值,但是它总是插入第一行并停止。
String rq="insert into seance values (?, ?, ?,?,?,?);";
try (Connection cnx = Connecteur1.getConnection(); PreparedStatement pst = cnx.prepareStatement(rq)) {
for(Etudiant var:liste1)
{
if(!(listEntier.contains(var.getCode_etudiant())))
{
pst.setString(1,DateCourant.format(date));
pst.setString(2,temps.format(date) );
pst.setInt(3,codeMatiere);
pst.setInt(4, 0);
pst.setInt(5,var.getCode_etudiant());
pst.setInt(6, codeGroupe);
pst.addBatch();
}
else
{
pst.setString(1,DateCourant.format(date));
pst.setString(2,temps.format(date) );
pst.setInt(3,codeMatiere);
pst.setInt(4, 1);
pst.setInt(5,var.getCode_etudiant());
pst.setInt(6, codeGroupe);
pst.addBatch();
}
}
pst.executeBatch();
pst.close();
}
You need : 你需要 :
cnx.setAutoCommit(false)
at the beginning 一开始
and 和
cnx.commit();
at the end 在末尾
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.