我正在将CQM插入数据库,首先插入考试坐标并检索其ID,然后插入其问题的标题和内容并检索其ID,然后它就可以工作了。
问题是当我插入命题时:例如,如果问题的id为7,则将其更改为0 ,并且插入失败
这是我的servlet
Examen examen = (Examen) request.getSession().getAttribute("examen");
int id_examen = ExamDAO.ajouter_examen(examen);
Question quest = new Question(titre, contenu, id_examen);
int id_question = ExamDAO.ajouter_question(quest);
System.out.println("id_question " + id_question);
//// the problem is here !!
Question prop = new Question(id_question, propositions);
int propu = ExamDAO.ajouter_prop(prop);
System.out.println(prop);
数据库代码
public static int ajouter_prop(Question prop) {
Connection cnx;
int propKey = 0;
try {
cnx = Connect.getConnection();
cnx.setAutoCommit(false);
String req = "insert into prop(id_question, contenu) values(?,?)";
PreparedStatement st = cnx.prepareStatement(req);
st.setInt(1, prop.getIdQuiz());
for (Proposition propp : prop.getListProp()) {
st.setString(2, propp.getContenu());
st.addBatch();
}
st.executeBatch();