I working in a program in jsp, trying myself learning a jsp.
so i doing a quiz program, where questions are get from the database tables. So here is the code of the quiz page, where questions are posted.
<%@ 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>
In the above code, i am getting a error in this line stating that,
String questionp = rs.getString("question");
muliple annotations found at this line
<% out.println("questionp"); %>
is printing the valuequestionp
not the variable. Use<% out.println(questionp); %>
or<%= questionp %>
(I think). But like greenPadawan said, please see How to Ask and provide a minimal reproducible example next time. Just by writing it, you will find your problem. I would add that you should quickly learn how to useServlets
andJstl
to write JSP correctly ;) – AxelH Nov 22 '16 at 10:24