private static final String SQL_CREATE_QUESTION = "INSERT INTO questions ('question', 'test_id') VALUE (?, ?);"; private static final String SQL_DELETE_QUESTION = "DELETE FROM questions WHERE id = ?"; private static final String SQL_CREATE_ANSWER = "INSERT INTO answers ('answer', 'correct', 'question_id') VALUE (?, ?, ?);"; private static final String SQL_DELETE_ANSWER = "DELETE FROM answer WHERE id = ?"; // Questions public boolean createQuestion(int test_id, String question_text) throws DBException { PreparedStatement pstmt = null; Connection con = null; ResultSet rs = null; boolean createStatus = false; try { con = getConnection(); pstmt = con.prepareStatement(SQL_CREATE_QUESTION); pstmt.setString(1, question_text); pstmt.setInt(2, test_id); pstmt.executeUpdate(); con.commit(); LOG.debug("Question created"); createStatus = true; } catch (SQLException ex) { rollback(con); LOG.error("Cannot create the question", ex); } finally { close (rs); close(pstmt); close(con); } return createStatus; } public boolean deleteQuestion(int question_id) throws DBException { PreparedStatement pstmt = null; Connection con = null; ResultSet rs = null; boolean deleteStatus = false; try { con = getConnection(); pstmt = con.prepareStatement(SQL_DELETE_QUESTION); pstmt.setInt(1, question_id); pstmt.executeUpdate(); con.commit(); LOG.debug("Question deleted"); deleteStatus = true; } catch (SQLException ex) { rollback(con); LOG.error("Cannot delete question", ex); } finally { close (rs); close(pstmt); close(con); } return deleteStatus; } // Answers public boolean createAnswer(String answer_text, boolean correct, int question_id) throws DBException { PreparedStatement pstmt = null; Connection con = null; ResultSet rs = null; boolean createStatus = false; try { con = getConnection(); pstmt = con.prepareStatement(SQL_CREATE_ANSWER); pstmt.setString(1, answer_text); pstmt.setBoolean(2, correct); pstmt.setInt(3, question_id); pstmt.executeUpdate(); con.commit(); LOG.debug("Answer created"); createStatus = true; } catch (SQLException ex) { rollback(con); LOG.error("Cannot create the answer", ex); } finally { close (rs); close(pstmt); close(con); } return createStatus; } public boolean deleteAnswer(int answer_id) throws DBException { PreparedStatement pstmt = null; Connection con = null; ResultSet rs = null; boolean deleteStatus = false; try { con = getConnection(); pstmt = con.prepareStatement(SQL_DELETE_ANSWER); pstmt.setInt(1, answer_id); pstmt.executeUpdate(); con.commit(); LOG.debug("Answer deleted"); deleteStatus = true; } catch (SQLException ex) { rollback(con); LOG.error("Cannot delete answer", ex); } finally { close (rs); close(pstmt); close(con); } return deleteStatus; }