1

I'm trying to insert multiple values in a mysql database using the iterative (for) but it always insert the first line and stops.

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();

}
Werner
  • 2,537
  • 1
  • 26
  • 38
zbart3i
  • 11
  • 6

1 Answers1

1

You need :

cnx.setAutoCommit(false) 

at the beginning

and

cnx.commit();

at the end

Gerard Rozsavolgyi
  • 4,834
  • 4
  • 32
  • 39