I am trying to make an online examination system. Questions have been added successfully and even the examiner is getting all the questions with radio options. However m having problem in counting the correct answer. In table, questions along with 4 options and a correct answer is given and m unable to count correct answer. Please suggest me how to do it. Here is my code:
java.jsp
<!DOCTYPE html>
<html>
<body id="processed">
<%
String ques=request.getParameter("ques");
String op1=request.getParameter("op1");
String op2=request.getParameter("op2");
String op3=request.getParameter("op3");
String op4=request.getParameter("op4");
String correct=request.getParameter("correct");
String driverName = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String dbName = "oes";
String userId = "root";
String password = "123456789";
try {
Class.forName(driverName);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement st = null;
ResultSet rs = null;
%>
<div class="container2">
<div class="container6">
<form class="test" action="test.jsp" method="post">
<div class="container7">
<h1>Java Programming Language</h1><br><br>
<%
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/oes","root","123456789");
st= con.createStatement();
String sql ="select * from java";
rs= st.executeQuery(sql);
int i=1;
while(rs.next())
{
%>
<table class="outertable">
<tr><b><%=i%> <%=rs.getString("ques") %></b>
<td><table class="innertable">
<tr>
<td><input type="radio" id="op1" name="radio<%=i%>" value="<%=rs.getString("op1")%>"><%=rs.getString("op1") %></td>
</tr>
<tr>
<td><input type="radio" id="op2" name="radio<%=i%>" value="<%=rs.getString("op2")%>"><%=rs.getString("op2") %></td>
</tr>
<tr>
<td><input type="radio" id="op3" name="radio<%=i%>" value="<%=rs.getString("op3")%>"><%=rs.getString("op3") %></td>
</tr>
<tr>
<td><input type="radio" id="op4" name="radio<%=i%>" value="<%=rs.getString("op4")%>"><%=rs.getString("op4") %></td>
</tr>
</table></td>
</tr>
</table>
<%
i++;
}
}
catch (Exception e) {
e.printStackTrace();
}
%>
</div>
<div class="submitbtn">
<button type="submit">Submit</button>
</div>
</form>
</div>
</div>
</body>
</html>
test.jsp
<!DOCTYPE html>
<html>
<body id="processed">
<%
String st[]=new String[10];
for(int i=0;i<st.length;i++){
int j=i+1;
st[i]=request.getParameter("radio"+j);
System.out.println(st[i]);
}
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/oes","root", "123456789");
Statement stmt=connection.createStatement();
ResultSet rs=stmt.executeQuery("select correct from java");
String ans="";
while(rs.next()){
ans+=rs.getString("correct")+" ";
}
int count=0;
String answers[]=ans.split(" ");
for(int i=0;i<answers.length;i++){
if(st[i].equals(answers[i])){
count++;
}
}
out.println("Your "+count+" answers are correct");
%>
</body>
</html>