how to get the result query one by one in jsp and mysql

Posted by user261002 on Stack Overflow See other posts from Stack Overflow or by user261002
Published on 2010-04-24T15:54:26Z Indexed on 2010/04/24 21:43 UTC
Read the original article Hit count: 336

Filed under:
|
|

I am trying to implement as Online Mock exam in JSP, but I have a problem to get the questions one by one, it get connceted for the first time, and show me the first question and answers, but when I click on "next" again, it still show me the first question, I think by clicking on "next" it start querying again. please help me. this is my bean :

database.SQLSelectStatement sqlselect;
database.SQLSelectStatement sqlselect2;
static ResultSet questions;
static ResultSetMetaData rsm;
static ResultSet answers;
public void setConnection() throws SQLException {
    if (database.DatabaseManager.getInstance().connectionOK()) {
        sqlselect = new database.SQLSelectStatement("question", "question", "0");
        sqlselect2 = new database.SQLSelectStatement("answers", "question_id", "0");
        questions = sqlselect.executeWithNoCondition();
    }
}
public int i=0;
public String getQuestions() throws SQLException {
    String result = "";
    rsm = questions.getMetaData();
    for (int i = 0; i < rsm.getColumnCount(); i++) {
        result += "<th>" + rsm.getColumnName(i + 1) + "</th>";
    }
    if (!questions.isLast()) {
        questions.next();
        System.out.println(i+1);
        result += "<tr>";
        result += "<td>" + questions.getInt(1) + "</td>";
        result += "<td>" + questions.getString(2) + "</td>";
        result += "</tr>";
        result += "<tr>";
        sqlselect2.setValue(String.valueOf(questions.getInt(1)));
         answers = sqlselect2.Execute();

        while (answers.next()) {
            result += "<tr> <td colspan='2'><input type='radio' name='answer' value='" + answers.getString(2) + "'> " + answers.getString(2) + "</td></tr>";
        }
        result += "</tr>";
        answers.close();
    }
    return result;
}

this is the HTML:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>JSP Page</h1>
        <jsp:useBean id="exam" class="exam.ExamQuestions"></jsp:useBean>

        <%
        exam.setConnection();                
        %>
        <form  method="post">
            <table >

                <%=exam.getQuestions()%>
            </table>
            <input type="submit" name="action" value="next"/>
        </form>
        <%
        String action = request.getParameter("action");
        if ("next".equals(action)) {
            out.println(request.getParameter("answer"));
        }

        %>
    </body>
</html>

© Stack Overflow or respective owner

Related posts about jsp

Related posts about servlets