标题翻译
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("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);
答案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.
专注分享java语言的经验与见解,让所有开发者获益!
评论