@Override public List<Result> getResult(Examination examination, User user) throws DatabaseException { List<Result> resultList = null; Connection connection = null; PreparedStatement statement = null; ResultSet rs = null; try { connection = DBUtility.getConnection(); statement = connection.prepareStatement(SELECT_BY_EXAM_USER); statement.setInt(1, examination.getExaminationId()); statement.setInt(2, user.getUserId()); rs = statement.executeQuery(); resultList = createResultObject(rs); } catch (ClassNotFoundException ex) { LOG.log(Level.SEVERE, null, ex); throw new DatabaseException(ex); } catch (SQLException ex) { LOG.log(Level.SEVERE, null, ex); throw new DatabaseException(ex); } finally { DBUtility.close(rs); DBUtility.close(statement); DBUtility.close(connection); } return resultList; }
@Override public Result getResult(int resultId) throws DatabaseException { List<Result> resultList = null; Result result = null; Connection connection = null; PreparedStatement statement = null; ResultSet rs = null; try { connection = DBUtility.getConnection(); statement = connection.prepareStatement(SELECT_BY_ID); statement.setInt(1, resultId); rs = statement.executeQuery(); resultList = createResultObject(rs); if (resultList != null && !resultList.isEmpty()) { result = resultList.get(0); } } catch (ClassNotFoundException ex) { LOG.log(Level.SEVERE, null, ex); throw new DatabaseException(ex); } catch (SQLException ex) { LOG.log(Level.SEVERE, null, ex); throw new DatabaseException(ex); } finally { DBUtility.close(rs); DBUtility.close(statement); DBUtility.close(connection); } return result; }
@Override public void saveOrUpdateResult(Result result) throws DatabaseException { Connection connection = null; PreparedStatement statement = null; try { connection = DBUtility.getConnection(); // If result id = 0, then we are trying to save a new record, else update if (result.getResultId() == 0) { statement = connection.prepareStatement(INSERT_STMT); statement.setInt(1, result.getExamination().getExaminationId()); statement.setInt(2, result.getUser().getUserId()); statement.setInt(3, result.getNumberCorrect()); statement.setInt(4, result.getNumberIncorrect()); statement.setInt(5, result.getMarksObtained()); statement.setInt(6, result.getIsPass()); statement.executeUpdate(); } } catch (ClassNotFoundException ex) { LOG.log(Level.SEVERE, null, ex); throw new DatabaseException(ex); } catch (SQLException ex) { LOG.log(Level.SEVERE, null, ex); throw new DatabaseException(ex); } finally { DBUtility.close(statement); DBUtility.close(connection); } }
@Override public void deleteResult(Result result) throws DatabaseException { Connection connection = null; PreparedStatement statement = null; try { connection = DBUtility.getConnection(); statement = connection.prepareStatement(DELETE_STMT); statement.setInt(1, result.getResultId()); statement.execute(); } catch (ClassNotFoundException ex) { LOG.log(Level.SEVERE, null, ex); throw new DatabaseException(ex); } catch (SQLException ex) { LOG.log(Level.SEVERE, null, ex); throw new DatabaseException(ex); } finally { DBUtility.close(statement); DBUtility.close(connection); } }