<%@ page import = "java.util.*"%>
<%@ page import = "java.lang.*"%>
<%@ page import="java.sql.*" %>
<%@ page language="java" import="java.sql.*" errorPage="" %>
<!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=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String ans=" ";
if(request.getParameter("correctAns")!=null)
{
ans=request.getParameter("correctAns").toString();
}
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String userName ="root";
String pass="password";
Statement st = null;
ResultSet qrst;
ResultSet rs = null;
String a,b;
int count = 0;
try {
Class.forName(driver);
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "password");
st = conn.createStatement();
rs = st.executeQuery("select * from questionnaires");
while(rs.next()) {
%>
<br>
<br/>
<center>
<table border="1" width="500px" bgcolor="white" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">
<form name="form1" >
<h2 align="center"><font color="red">Online Quize Application</font></h2>
<b>Select Correct Answer</b>
<table border="0" width="500px" cellspacing="2" cellpadding="4">
<tr>
<td width="50%"> Question Number: <%= rs.getString("qid")%></td>
<input type="hidden" name="correctAns" value="<%=rs.getString("correctAns")%>" />
<tr>
<td><%= rs.getString("quest") %></td></tr>
<tr>
<td>
1: <input type="radio" name="a" value= "QA" /></td>
<td><%= rs.getString("QA") %></td></tr>
<tr>
<td>
2: <input type="radio" name="a" value="QB" /></td>
<td><%= rs.getString("QB") %></td></tr>
<tr>
<td>
3: <input type="radio" name="a" value="QC" /></td>
<td><%= rs.getString("QC") %> </td></tr>
<tr>
<td>
4: <input type="radio" name="a" value="QD" /> </td>
<td> <%= rs.getString("QD") %> </td></tr>
<tr>
<td>
<center>
<input type="submit" value="Submit" name="submit"></center></td></tr>
</table>
</form>
</td>
</tr>
</table>
</center>
</body>
<%
a=request.getParameter("a");
%>
<%
int items = 0 ;
if(a.equals(ans))
{
count++;
items++;
out.println("Correct");
}
else{
out.println("wrong");
items++;
}
if(count <= items)
{
String strcount = Integer.toString(count);
String stritems = Integer.toString(items);
out.println("Score :" . strcount . "/" .stritems);
}
%>
<%
}}
catch (Exception ex) {
ex.printStackTrace();
%>
<%
} finally {
if (rs != null) rs.close();
if (st != null) st.close();
if (conn != null) conn.close();
}
out.println("Score="+count);
%>
</html>
Hello, I'm having some problem with my code here. Because I have 2 question in my database and I want to check whether I answered correctly or not and I want to add the number of correct answer. I know there is something wrong with my code. Im really new at using JSP. Please help me. Thank you so much.