【发布时间】:2016-11-22 10:11:06
【问题描述】:
我在一个jsp程序中工作,尝试自己学习jsp。
所以我做了一个测验程序,问题是从数据库表中获取的。所以这里是测验页面的代码,问题发布的地方。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%@ include file="_header.jsp"%>
<center>QUIZ PROGRAM</center>
<br />
<%@ page import="java.sql.*"%>
<%
//print the question and answer
int questionaire = 1;
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/quiz", "root", "");
PreparedStatement pst = con.prepareStatement("SELECT * FROM questions WHERE id = ?");
pst.setInt(1, questionaire);
ResultSet rs = pst.executeQuery();
int questionId;
String questionp;
if (rs.next()) {
questionId = rs.getInt("id");
String questionp = rs.getString("question");
//String option1 = rs.getString("option1");
//String option2 = rs.getString("option2");
//String right = rs.getString("right");
questionaire++;
}
//get the answer and check
//String question1 = "asd";
%>
<center>
<form method="post" action="quiz.jsp">
<table border="1" cellpadding="5" cellspacing="2" align="center">
<thead>
<tr>
<th colspan="2"><% out.println(questionp); %></th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="radio" name="question"
value="value1">Yes</td>
<td><input type="radio" name="question"
value="value2">No</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"
value="Next" onClick="next();" /></td>
</tr>
</tbody>
</table>
</form>
</center>
</body>
</html>
在上面的代码中,我在这一行中收到一个错误,指出,
String questionp = rs.getString("question");
在这一行发现了多个注释
【问题讨论】:
-
您的问题是什么?您面临的问题是什么?请参阅此link 了解如何正确陈述您的问题。
-
<% out.println("questionp"); %>正在打印值questionp而不是变量。使用<% out.println(questionp); %>或<%= questionp %>(我认为)。但就像 greenPadawan 说的,请看How to Ask 并提供minimal reproducible example 下一次。只要写出来,你就会发现你的问题。我要补充一点,您应该快速学习如何使用Servlets和Jstl来正确编写JSP;) -
@greenPadawn 抱歉,我没有解释我遇到的问题,现在我已经编辑了问题,解释了我的问题,但仍然显示错误。
-
@AxelH 谢谢,我已经更新了我的问题,正如你所说,我通过删除双引号更改了代码
标签: jsp