<%@ 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;
int items = 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");
if(a.equals(ans))
{
count++;
items++;
out.println("Correct");
}
else{
out.println("wrong");
items++;
}
}
catch (Exception ex) {
ex.printStackTrace();
%>
<%
} finally {
if (rs != null) rs.close();
if (st != null) st.close();
if (conn != null) conn.close();
}
%>
<%
if(count <= items)
{
//String strcount = Integer.toString(count);
//String stritems = Integer.toString(items);
//out.println("Score :" . count . "/" .items);
out.print("Score :");
out.print(count);
out.print("/");
out.println(items);
}
%>
</html>
Hello, I'm creating a simple Online Exam System but i'm having some problem with my codes. I want to count all the correct answers and wrong answers. How should I do it? I mean, what code should I use? I'm a beginner in jsp. Please help. Thank you so much.