Created
May 27, 2019 22:43
-
-
Save aminelch/e41f2026ab3c87d5e7e2dca6dbf214dc to your computer and use it in GitHub Desktop.
getAllQuestion method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Question> getAllQuestions() { | |
List<Question> questions = new ArrayList<Question>(); | |
Connection connection = null; | |
Statement statement = null; | |
ResultSet resultSet = null; | |
try { | |
connection = ConnectionConfiguration.getConnection(); | |
statement = connection.createStatement(); | |
resultSet = statement.executeQuery("SELECT* FROM questions ORDER BY RAND() LIMIT 8"); | |
while (resultSet.next()) { | |
Question question = new Question(); | |
question.setId(resultSet.getInt("id")); | |
question.setQuestion(resultSet.getString("question")); | |
question.setReponse(resultSet.getString("reponse")); | |
question.setOp1(resultSet.getString("op1")); | |
question.setOp2(resultSet.getString("op2")); | |
question.setOp3(resultSet.getString("op3")); | |
question.setOp4(resultSet.getString("op4")); | |
questions.add(question); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
if (resultSet != null) { | |
try { | |
resultSet.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
if (statement != null) { | |
try { | |
statement.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
if (connection != null) { | |
try { | |
connection.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
return questions; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment