@WebMethod(operationName = "showAllQuestion") @WebResult(name = "Question") public java.util.ArrayList<Question> showAllQuestion() { ArrayList<Question> questions = new ArrayList<Question>(); try { Statement statement = conn.createStatement(); String sql; sql = "SELECT * FROM Question ORDER BY id DESC"; PreparedStatement dbStatement = conn.prepareStatement(sql); ResultSet result = dbStatement.executeQuery(); while (result.next()) { int count = countAnswer(result.getInt("id")); UserWS userws = new UserWS(); User user = userws.getUserById(result.getInt("user_id")); questions.add( new Question( result.getInt("id"), result.getInt("user_id"), result.getString("topic"), result.getString("content"), result.getInt("vote"), result.getString("date"), count, user.getUserName())); } result.close(); statement.close(); } catch (SQLException ex) { Logger.getLogger(QuestionWS.class.getName()).log(Level.SEVERE, null, ex); } return questions; }
@WebMethod(operationName = "getAnswerByQuestionId") @WebResult(name = "Answer") public java.util.ArrayList<Answer> getAnswerByQuestionId(@WebParam(name = "id") int id) { ArrayList<Answer> answers = new ArrayList<Answer>(); try { Statement statement = conn.createStatement(); String sql; sql = "SELECT * FROM Answer WHERE question_id=?"; PreparedStatement dbStatement = conn.prepareStatement(sql); dbStatement.setInt(1, id); ResultSet result = dbStatement.executeQuery(); while (result.next()) { UserWS userws = new UserWS(); User user = userws.getUserById(result.getInt("user_id")); answers.add( new Answer( result.getInt("id"), result.getInt("question_id"), result.getInt("user_id"), result.getString("content"), result.getInt("vote"), result.getString("date"), user.getUserName())); } result.close(); statement.close(); } catch (SQLException ex) { Logger.getLogger(AnswerWS.class.getName()).log(Level.SEVERE, null, ex); } return answers; }