单选按钮不返回值JSP

问题描述 投票:0回答:1

我正在用JSP编写测验,单选按钮当前返回NULL而不是分配的值。我该如何解决?

并且是的,我知道不应在JSP中使用scriptlet,但不确定如何解决此问题。稍后我将对此进行一些研究,但是现在,我只想知道为什么单选按钮返回NULL。

System.out.println输出仅用于跟踪值,这些值将在最终代码中删除。我已经包含了主要代码,以防万一有一个错误也需要修复。抱歉,没有代码注释,这是我必须非常快地完成作业的一个项目。

Connection conn = null;
ResultSet rs;
Statement st;
String action;
Scanner input = new Scanner(System.in);
System.out.println("Enter Question ID (int):");
int score = 0;

int QID = Integer.parseInt(input.nextLine());
try {
    Class.forName("org.mariadb.jdbc.Driver");
    System.out.println("Connecting to a selected database...");
    conn = DriverManager.getConnection(
            "jdbc:mariadb://ebs-db.eastbarnetschool.com/Quiz", "Quiz","quiz123");
    System.out.println("Connection made");

    CallableStatement stmt = conn.prepareCall("{call QuestionTitle(?, ?)}");
        stmt.setInt(1, QID); 
        stmt.registerOutParameter(2, Types.VARCHAR);
        stmt.execute();
        String description = stmt.getString(2);
        System.out.println(description);
%>
<br>
<br/>
<center>

<table border="1" width="500px" bgcolor="lightblue" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">

<form name="quiz" method="post">

<h1 align="center"><font color="white" face="arial">Quiz</font></h1>
<table border="0" width="500px" cellspacing="2" cellpadding="6">
<tr>
<td width="50%"><font color="steelblue" face="arial" size=4><span style="font-weight:normal"> QUESTION <%=QID%></span></font></td>
<tr>
<td width="100%"><font color="black" face="arial" size=4><span style="font-weight:normal"><%=description%></span></font></td></tr>

<% 
     CallableStatement stmt1 = conn.prepareCall("{call DisplayAnswers(?)}");
    stmt1.setInt(1, QID); 
    rs = stmt1.executeQuery();
    stmt1.execute();

    ArrayList<String> answers = new ArrayList<String>();

    while(rs.next()) {
        String ADescription = rs.getString("Answer_Description");
        answers.add(ADescription);
        }

%>
<tr>
<td>        
1:<input type="radio" name="button" value= "<%=answers.get(0)%>" /><font face="arial" size=3><%=answers.get(0) %></font></td>
    <tr>
    <td>
2: <input type="radio" name="button" value="<%=answers.get(1)%>" /><font face="arial" size=3><%=answers.get(1) %></font></td>
    <tr>
    <td>
3: <input type="radio" name="button" value="<%=answers.get(2)%>" /><font face="arial" size=3><%=answers.get(2) %></font></td>
    <tr>
    <td>
4: <input type="radio" name="button" value="<%=answers.get(3)%>" /><font face="arial" size=3><%=answers.get(3) %></font></td>

<tr><td><center><input type="submit" value="Next" name="next"><input type="submit" value="Submit" name="submit"></center></td></tr> 
</table>
</form>     
<%  
System.out.println(answers);        

action = request.getParameter("button");
System.out.println("\n" + action);

if (action == null){
    System.out.println("Please select a button");
}

CallableStatement stmt2 = conn.prepareCall("{call GetCorrectAnswer(?, ?)}");
    stmt2.setInt(1, QID); 
    stmt2.registerOutParameter(2, Types.VARCHAR);
    stmt2.execute();
    String CorrectDescription = stmt2.getString(2);
    System.out.println("\nCorrect Answer: " + CorrectDescription);

    System.out.println("\nChosen: " + (request.getParameter("button")));

    if(request.getParameterValues("button") != null) {
       if(correctAnswer.equals(CorrectDescription)) {
            out.println("Correct!");
            score ++;
        }

        else{
            out.println("Incorrect!");  
    }   

    }

    //System.out.println("Score = "+ score);
java html jsp radio-button
1个回答
0
投票

您的存储过程,DisplayAnswers似乎不正确。如果希望从中获取值,则它必须具有OUT参数。另外,请确保在更正存储过程stmt1.registerOutParameter(2, java.sql.Types.VARCHAR);的实现后调用DisplayAnswers。另外,如果您使用存储过程,则应获得类似String answerDescription = stmt1.getString(2);的值。

但是,在这种情况下,我建议您使用PreparedStatement而不是CallableStatement,即形成类似的查询

SELECT answer_description FROM answers WHERE qid = ?

,然后在设置参数值后获得结果集。检查https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html了解更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.