Skip to content

Instantly share code, notes, and snippets.

@aminelch
Created May 27, 2019 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aminelch/e41f2026ab3c87d5e7e2dca6dbf214dc to your computer and use it in GitHub Desktop.
Save aminelch/e41f2026ab3c87d5e7e2dca6dbf214dc to your computer and use it in GitHub Desktop.
getAllQuestion method
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