单选按钮在JSP中未返回值

huangapple go评论123阅读模式
英文:

Radio Button not returning value JSP

问题

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);
%>
英文:

I am developing a quiz in a JSP and the radio buttons are currently returning NULL instead of the assigned value. How do I fix this?

And yes I am aware that scriptlets should not be used in JSPs but I am unsure of how to resolve this. I will do some research on this later, but for now, I just want to know why the radio buttons are returning NULL.

The System.out.println outputs are just for me to track the values, these will be removed in the final code. I have included the main code just in case there is an error that also needs to be fixed. Apologies for the lack of code commenting, this is a project I had to do extremely quick for homework.

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

int QID = Integer.parseInt(input.nextLine());
try {
    Class.forName(&quot;org.mariadb.jdbc.Driver&quot;);
    System.out.println(&quot;Connecting to a selected database...&quot;);
    conn = DriverManager.getConnection(
            &quot;jdbc:mariadb://ebs-db.eastbarnetschool.com/Quiz&quot;, &quot;Quiz&quot;,&quot;quiz123&quot;);
    System.out.println(&quot;Connection made&quot;);
    
    CallableStatement stmt = conn.prepareCall(&quot;{call QuestionTitle(?, ?)}&quot;);
		stmt.setInt(1, QID); 
		stmt.registerOutParameter(2, Types.VARCHAR);
		stmt.execute();
		String description = stmt.getString(2);
		System.out.println(description);
%&gt;
&lt;br&gt;
&lt;br/&gt;
&lt;center&gt;

&lt;table border=&quot;1&quot; width=&quot;500px&quot; bgcolor=&quot;lightblue&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;&gt;
&lt;tr&gt;
&lt;td width=&quot;100%&quot;&gt;

&lt;form name=&quot;quiz&quot; method=&quot;post&quot;&gt;

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

&lt;% 
     CallableStatement stmt1 = conn.prepareCall(&quot;{call DisplayAnswers(?)}&quot;);
	stmt1.setInt(1, QID); 
	rs = stmt1.executeQuery();
	stmt1.execute();
	
	ArrayList&lt;String&gt; answers = new ArrayList&lt;String&gt;();
		
	while(rs.next()) {
		String ADescription = rs.getString(&quot;Answer_Description&quot;);
		answers.add(ADescription);
		}
		
%&gt;
&lt;tr&gt;
&lt;td&gt;		
1:&lt;input type=&quot;radio&quot; name=&quot;button&quot; value= &quot;&lt;%=answers.get(0)%&gt;&quot; /&gt;&lt;font face=&quot;arial&quot; size=3&gt;&lt;%=answers.get(0) %&gt;&lt;/font&gt;&lt;/td&gt;
	&lt;tr&gt;
	&lt;td&gt;
2: &lt;input type=&quot;radio&quot; name=&quot;button&quot; value=&quot;&lt;%=answers.get(1)%&gt;&quot; /&gt;&lt;font face=&quot;arial&quot; size=3&gt;&lt;%=answers.get(1) %&gt;&lt;/font&gt;&lt;/td&gt;
	&lt;tr&gt;
	&lt;td&gt;
3: &lt;input type=&quot;radio&quot; name=&quot;button&quot; value=&quot;&lt;%=answers.get(2)%&gt;&quot; /&gt;&lt;font face=&quot;arial&quot; size=3&gt;&lt;%=answers.get(2) %&gt;&lt;/font&gt;&lt;/td&gt;
	&lt;tr&gt;
	&lt;td&gt;
4: &lt;input type=&quot;radio&quot; name=&quot;button&quot; value=&quot;&lt;%=answers.get(3)%&gt;&quot; /&gt;&lt;font face=&quot;arial&quot; size=3&gt;&lt;%=answers.get(3) %&gt;&lt;/font&gt;&lt;/td&gt;
	
&lt;tr&gt;&lt;td&gt;&lt;center&gt;&lt;input type=&quot;submit&quot; value=&quot;Next&quot; name=&quot;next&quot;&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;submit&quot;&gt;&lt;/center&gt;&lt;/td&gt;&lt;/tr&gt;	
&lt;/table&gt;
&lt;/form&gt;		
&lt;%	
System.out.println(answers);		

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

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

CallableStatement stmt2 = conn.prepareCall(&quot;{call GetCorrectAnswer(?, ?)}&quot;);
	stmt2.setInt(1, QID); 
	stmt2.registerOutParameter(2, Types.VARCHAR);
	stmt2.execute();
	String CorrectDescription = stmt2.getString(2);
	System.out.println(&quot;\nCorrect Answer: &quot; + CorrectDescription);
	
	System.out.println(&quot;\nChosen: &quot; + (request.getParameter(&quot;button&quot;)));
	
	if(request.getParameterValues(&quot;button&quot;) != null) {
	   if(correctAnswer.equals(CorrectDescription)) {
	    	out.println(&quot;Correct!&quot;);
	    	score ++;
	    }
	    
	    else{
	        out.println(&quot;Incorrect!&quot;);	
	}	

	}

	//System.out.println(&quot;Score = &quot;+ score);

答案1

得分: 0

你的存储过程 DisplayAnswers 似乎不正确。如果你希望从中获得值,它必须具有 OUT 参数。此外,在修正了 DisplayAnswers 存储过程的实现之后,确保调用 stmt1.registerOutParameter(2, java.sql.Types.VARCHAR);。另外,如果你正在使用存储过程,你应该像这样获取值:String answerDescription = stmt1.getString(2);

然而,我建议在这种情况下使用 PreparedStatement 而不是 CallableStatement,即构造类似以下的查询语句:

SELECT answer_description FROM answers WHERE qid = ?

然后在设置参数值后获取结果集。更多细节请参考 https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

英文:

Your stored procedure, DisplayAnswers doesn't seem to be correct. It must have OUT parameter(s) if you are expecting to get value(s) from it. Also, make sure to call stmt1.registerOutParameter(2, java.sql.Types.VARCHAR); after correcting the implementation of your stored procedure, DisplayAnswers. Also, if you are using a stored procedure, you should get a value like String answerDescription = stmt1.getString(2);.

However, I recommend you use a PreparedStatement instead of a CallableStatement in this case i.e. form your query something like

SELECT answer_description FROM answers WHERE qid = ?

and then get the resultset after setting the value of the parameter. Check https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html for more details.

huangapple
  • 本文由 发表于 2020年1月30日 18:54:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/59984370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定