tags:

views:

27

answers:

1

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>
+2  A: 

Sorry if I sound harsh, but there's too much wrong with the given code that I don't know where to start with answering/correcting. The basic concepts are completely misunderstood. You basically need to rewrite everything from the beginning on.

I suggest you to leave this project completely aside for now and start with those tutorials. Once you have a decent understanding how it's supposed to work/fit all together, then restart developing from blank based on what you've learnt from those tutorials.

To the point: use a JSP page for view. Use a Servlet class to control, preprocess and postprocess the model/view. Use HTML inputs/buttons in JSP to send request parameters. Use those request parameters in servlet to take actions accordingly. Use a model object (Javabean class) to hold the data. Use a DAO class to interact with database and take/return model objects. Use taglibs like JSTL in JSP to control page flow. Use EL in JSP to access model data.

Good luck.

BalusC
thanks, I think you are right, too much mess.