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

}

1 Answer 1

1

You need :

cnx.setAutoCommit(false) 

at the beginning

and

cnx.commit();

at the end

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.