mardi 30 mai 2017

Quiz website in java and jsp - How to select a right answer in multiple questions and get result?

I'm new at programming in java and currently in the process of creating a dynamic web project using Eclipse and mySQL Workbench. The website is going to be a quiz with five questions with four options each. The quiz itself is stored in a table in mySQL Workbench and accesable through the QuestionsDAO-file. Currently I'm having problems figuring how to assign one option as the correct answer in each of the questions in the array and how to print out the result of the quiz when you press the submit button in the jsp-file.

This is the body of jsp-file for the quiz

<%

ArrayList<Questions> quizArray =  null;

quizArray = QuestionsDAO.getAllQuestions();

int a = 1;
int o1 = 1;
int o2 = 2;
int o3 = 3;
int o4 = 4;
int qnr = 1;

for(Questions question : quizArray) {   
%>

if(request.getParameter("resultBtn") != null){

int correctAnswers = 0;


<tr>
    <td><p><%=qnr%>. <%=question.getQuestion() %></p></td>

    <td><%=question.getQoptOne() %></td>
    <input type="checkbox" id="<%= o1 %>" name="check1" value="check1"></input>
    </br>

    <td><%=question.getQoptTwo() %></td>
    <input type="checkbox" id="<%= o2 %>" name="check2" value="check2"></input>
    </br>
    <td><%=question.getQoptThree() %></td>
    <input type="checkbox" id="<%= o3 %>" name="check3" value="check3"></input>
    </br>
    <td><%=question.getQoptFour() %></td>
    <input type="checkbox" id="<%= o4 %>" name="check4"value="check4"></input>
    </br></tr>`<% } %>`



<form action="result.jsp" method="post">
    <input id="sendBtn" value="Submit" type="submit" name="resultBtn"></input>
    <input type="text" id="resultField" name="resultField"></input>
</form>

String answer1 = request.getParameter("1"); 
if(request.getParameter("check1") != null) {
    correctAnswers = correctAnswers + 1;
 } else {
    System.out.println("Wrong in question 1");
 }

String answer2 = request.getParameter("3"); 
if(request.getParameter("check3") != null) {
    correctAnswers = correctAnswers + 1;
 } else {
    System.out.println("Wrong in question 2");
 }

String answer3 = request.getParameter("2"); 
if(request.getParameter("check2") != null) {
    correctAnswers = correctAnswers + 1;
 } else {
    System.out.println("Wrong in question 3");
 }


String answer4 = request.getParameter("2");
if(request.getParameter("check2") != null) {
    correctAnswers = correctAnswers + 1;
} else {
    System.out.println("Wrong in question 4");
}

System.out.println("You have " + correctAnswers + " correct answers");}

The Questions-class

public class Questions {

long questionID;
String question;
String qoptOne;
String qoptTwo;
String qoptThree;
String qoptFour;
int correctAnswer;
private boolean isActive = true;

public Questions(long questionID, String question, String qoptOne, String qoptTwo, String qoptThree, String qoptFour, int correctAnswer){
    this(questionID, question, qoptOne, qoptTwo, qoptThree, qoptFour, correctAnswer, true);
}

public Questions(long questionID, String question, String qoptOne, String qoptTwo, String qoptThree, String qoptFour, int correctAnswer, boolean isActive){
    this.questionID = questionID;
    this.question = question;
    this.qoptOne = qoptOne;
    this.qoptTwo = qoptTwo;
    this.qoptThree = qoptThree;
    this.qoptFour = qoptFour;
    this.correctAnswer = correctAnswer;
    this.isActive = isActive;
}

public String getQuestion()
{ 
    return question;
}

public void setQuestion(String s)
{
    question=s;
}


public long getQuestionID()
{
    return questionID;
}

public void setQuestionNumber(int i)
{
    questionID = i;
}

public int getCorrectAnswer()
{
    return correctAnswer;
}

public void setCorrectAnswer(int i)
{
    correctAnswer=i;
}

public String getQoptOne()
{
    return qoptOne;
}

public String getQoptTwo()
{
    return qoptTwo;
}

public String getQoptThree()
{
    return qoptThree;
}

public String getQoptFour()
{
    return qoptFour;
}

}

The QuestionsDAO-class

public class QuestionsDAO {

static ArrayList<Questions> resultQuestions = new ArrayList<Questions>();

public static Questions getQuestion(){
    Questions result = null;
    Connection con = null;

try {

        con = JDBCConnectionFactory.getNewConnection();
        String sql = "SELECT * FROM quiz1 WHERE question LIKE ? OR q_id LIKE ? or o1 LIKE ? or o2 LIKE ? or o3 LIKE ? or o4 LIKE ?";
        java.sql.PreparedStatement prep = con.prepareStatement(sql);
        ResultSet res = prep.executeQuery();

        while(res.next()){
            String question = res.getString("quiz1.question");
            long questionID = res.getLong("quiz1.q_id");
            String qoptOne = res.getString("quiz1.o1");
            String qoptTwo = res.getString("quiz1.o2");
            String qoptThree = res.getString("quiz1.o3");
            String qoptFour = res.getString("quiz1.o4");
            resultQuestions.add(new Questions(questionID, question, qoptOne, qoptTwo, qoptThree, qoptFour, 0));
        }
    } catch(SQLException e){
        e.printStackTrace();
        } finally{
            JDBCConnectionFactory.closeConnection(con);
        }
    return result;
}


public static ArrayList<Questions> getAllQuestions(){
    ArrayList<Questions> result = new ArrayList<Questions>();
    Connection con = null;

    try{
        con = JDBCConnectionFactory.getNewConnection();
        String sql = "SELECT * FROM quiz1 ";
        java.sql.PreparedStatement prep = con.prepareStatement(sql);
        ResultSet res = prep.executeQuery();

        while(res.next()){
            String question = res.getString("quiz1.question");
            long questionID = res.getLong("quiz1.q_id");
            String qoptOne = res.getString("quiz1.o1");
            String qoptTwo = res.getString("quiz1.o2");
            String qoptThree = res.getString("quiz1.o3");
            String qoptFour = res.getString("quiz1.o4");
            result.add(new Questions(questionID, question, qoptOne, qoptTwo, qoptThree, qoptFour, 0));
        }
    } catch(SQLException e){
        e.printStackTrace();
    } finally{
        JDBCConnectionFactory.closeConnection(con);
    }
    return result;  
}


public static ArrayList<Questions> getOneQuestion(long searchLong){
    ArrayList<Questions> result = new ArrayList<Questions>();
    Connection con = null;

    try{
        con = JDBCConnectionFactory.getNewConnection();
        String sql = "SELECT * FROM quiz1 WHERE q_id LIKE ? ";
        java.sql.PreparedStatement prep = con.prepareStatement(sql);
        ResultSet res = prep.executeQuery();

        while(res.next()){
            String question = res.getString("quiz1.question");
            long questionID = res.getLong("quiz1.q_id");
            String qoptOne = res.getString("quiz1.o1");
            String qoptTwo = res.getString("quiz1.o2");
            String qoptThree = res.getString("quiz1.o3");
            String qoptFour = res.getString("quiz1.o4");
            result.add(new Questions(questionID, question, qoptOne, qoptTwo, qoptThree, qoptFour, 0));
        }
    } catch(SQLException e){
        e.printStackTrace();
    } finally{
        JDBCConnectionFactory.closeConnection(con);
    }
    return result;  
}

public static ArrayList<Questions> searchQuestions(String searchString) {
    ArrayList<Questions> results = new ArrayList<Questions>();

    Connection con = null;

    try{
        con = JDBCConnectionFactory.getNewConnection();
        String sql = "SELECT * FROM quiz1 WHERE question LIKE ? OR q_id LIKE ? or o1 LIKE ? or o2 LIKE ? or o3 LIKE ? or o4 LIKE ?";

        java.sql.PreparedStatement prep = con.prepareStatement(sql);
        prep.setString(1, searchString + "%");
        prep.setString(2, searchString + "%");
        ResultSet res = prep.executeQuery();
         while(res.next()){
             Questions question = createQuestionFromResult(res);
             results.add(question);
         }

    } catch (SQLException e){
        e.printStackTrace();
    } finally{
        JDBCConnectionFactory.closeConnection(con);
    }
    return results;
}

static Questions createQuestionFromResult(ResultSet res) throws SQLException{
    Questions result = null;

    String question = res.getString("quiz1.question");
    long questionID = res.getLong("quiz1.q_id");
    String qoptOne = res.getString("quiz1.o1");
    String qoptTwo = res.getString("quiz1.o2");
    String qoptThree = res.getString("quiz1.o3");
    String qoptFour = res.getString("quiz1.o4");
    result = new Questions(questionID, question, qoptOne, qoptTwo, qoptThree, qoptFour, 0);
    return result;



}

}




Aucun commentaire:

Enregistrer un commentaire