首页 > 解决方案 > 单选按钮不返回值 JSP

问题描述

我正在 JSP 中开发一个测验,单选按钮当前返回 NULL 而不是分配的值。我该如何解决?

是的,我知道脚本不应该在 JSP 中使用,但我不确定如何解决这个问题。稍后我会对此进行一些研究,但现在,我只想知道为什么单选按钮返回 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);

标签: javahtmljspradio-button

解决方案


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

但是,我建议您在这种情况下使用 aPreparedStatement而不是 a CallableStatement,即形成您的查询,例如

SELECT answer_description FROM answers WHERE qid = ?

然后在设置参数值后得到结果集。查看https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html了解更多详情。


推荐阅读