• Post Reply Bookmark Topic Watch Topic
  • New Topic

Online examination system - problem inserting student's answer  RSS feed

 
Nicky Dee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating a simple online examination(tutorial) system using JSP and Servlet. 2 JSP pages were created: questions and studentanswer.

The questions were pulled out from a database called questions(qNo,description,answer1,answer2,answer3,studanswer),
which appear in questions.jsp page. there is a column where user enter the answer (expect to stored in studanswer textbox) and a Submit button.

Issue:
Need to insert user answer and save it into the question database. Got the following errors:

An error occurred at line: 18 in the jsp file: /studentanswer.jsp
The method setString(int, String) is undefined for the type Statement
15:
16: Statement st = con.createStatement();
17:
18: st.setString(1,studanswer);
19:
20: in numRowsChanged=st.executeUpdate():
21:


An error occurred at line: 20 in the jsp file: /studentanswer.jsp
in cannot be resolved to a type
17:
18: st.setString(1,studanswer);
19:
20: in numRowsChanged=st.executeUpdate():
21:
22: response.sendRedirect("submitted.jsp");
23: %>


Please help me to solve these. Do I need to create different table to store the user's answers? Appreciate if you can provide suggestions and/or link on how to improve this program of mine. Thank you.



=======================questions.jsp==================================

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<html>
<head>

<title>Display data from the table using jsp</title>
<style type="text/css">
<!--
.style2 {color: #FFFFFF; font-weight: bold; }
-->
</style>
</head>
<body>
<form method="post" action="studentanswer.jsp">


<p>BSc (Hons) in Information Technology</p>
<p>Module: Interactive Design</p>
<p>Tutorial : Chapter 1 - Introduction to Interactive Design.</p>
<p>
<%
try{
/* create string of connection url within specified format with machine
name, port number and database name. Here machine name id localhost and database
name is student.*/

String connectionURL="jdbc:mysql://localhost:3306/idtutorials";

//Declare a connection by using Connection interface
Connection connection=null;

/*declare object of Statement interface that is used for executing sql statements*/
Statement statement=null;

//declare a resultset that uses a table for output data from that table.
ResultSet rs=null;

//Load JBBC driver"com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();

/*Create a connection by using getConnection() method that takes parameters
of string type connection url, user name and password to connect to database*/
connection = DriverManager.getConnection(connectionURL,"root","");

/*createStatement() is used for create statement object that is used for
sending sql statements to the specified database*/
statement = connection.createStatement();

//sql query to retrieve values from the specified table
String QueryString = "SELECT * from questions";
rs=statement.executeQuery(QueryString);
%>
</p>
<p> </p>
<form method="post" action="studentanswer.jsp"><TABLE width="1245" border="1" align="center" cellpadding="15" style="background-color: #ffffcc;"><TR><TD width="95"><table width="1245" border="1" align="center" cellpadding="15" style="background-color: #ffffcc;">
<%
while(rs.next()){
%>
<tr>
<td bgcolor="#0033FF"><div align="center" class="style2">
<div align="center">No.</div>
</div></td>
<td bgcolor="#0033FF"><div align="center" class="style2">Questions</div></td>
<td bgcolor="#0033FF"><div align="center" class="style2">Choice No.1</div></td>
<td bgcolor="#0033FF"><div align="center" class="style2">Choice No.2</div></td>
<td bgcolor="#0033FF"><div align="center" class="style2">Choice No.3</div></td>
<td bgcolor="#0033FF"><div align="center" class="style2">Your Answer</div></td>
<td bgcolor="#0033FF"><div align="center"><span class="style2">Submit Your Answer</span></div></td>
</tr>
<tr>
<td width="29"><div align="center"><%=rs.getInt(1)%></div></td>
<td width="159"><%=rs.getString(2)%></td>
<td width="200"><p><%=rs.getString(3)%></p></td>
<td width="200"><%=rs.getString(4)%></td>
<td width="200"><%=rs.getString(5)%></td>
<td width="95"><input name="studanswer" type="text" value="" size="10" /></td>
<td width="120"><div align="center">
<input type="submit" value="Submit" />
</div></td>
</tr>
<%}%>
<%
//close all the connections.
rs.close();
statement.close();
connection.close();
} catch (Exception ex){
%>
<div align="center"></div>
<font size="+3" color="red">
<div align="center"></div>
<div align="center"></div>
<%
out.println("unable to connect to database.");
}
%>
</font>
</table></TD>
</TR>
</TABLE>
</form>
<body>
</body>
</html>



==================studentanswer.jsp==================================

<%@ page import ="java.sql.*" %>
<%
//getting the value from jsp page
String studanswer = request.getParameter("studanswer");

//Load database driver
Class.forName("com.mysql.jdbc.Driver");

//get connection to database
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/idtutorials",
"root", "");

//add data into database
String sql = "insert into questions values(?)";

Statement st = con.createStatement();

st.setString(1,studanswer);

in numRowsChanged=st.executeUpdate():

response.sendRedirect("submitted.jsp");
%>
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36446
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The call setString(int, String) exists on PreparedStatement, not Statement. You'd need to change
Statement st = con.createStatement();
to
PreparedStatement st = con.prepareStatement();

Also, I recommend you read about MVC. It's not considered good practice to put code in a JSP.
 
Nicky Dee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your reply. with that I have come out with a 2nd attempt.

My idea is to create 1 table for 1 question. Also, 1 table for the answer.
I created form.jsp to display the question and user will select the answer in a form of radio button.
Also created result.jsp to check the answer whether its correct or wrong.

However, the result given by user were still 0 even when the answer was correct. if the answer is correct, im expecting a value of 1 being displayed.

Once the above works, I will need to link them to a newly registered student. finally, showing the student details and the result of its attempt answering the questions.

Appreciate the help to identify what I have done wrong here(form.jsp and result.jsp). FYI, I just learnt JSP for a month and appreciate the guidance/links that I can refer to properly.

==========================form.jsp======================================

<%@page import="java.sql.*"%>
<html>
<form method="post" action="result.jsp">
<table>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/idtutorials");
Statement st=connection.createStatement();
ResultSet rs=st.executeQuery("Select * from test");
int i=1;
while(rs.next()){
%>

<%=i%></td><td>
<%=rs.getString("ques")%>
<%=rs.getString("op1")%></td><td><input type="radio" value="<%=rs.getString("op1")%>" name="radio<%=i%>"/>
<%=rs.getString("op2")%></td><td><input type="radio" value="<%=rs.getString("op2")%>" name="radio<%=i%>"/>
<%=rs.getString("op3")%></td><td><input type="radio" value="<%=rs.getString("op3")%>" name="radio<%=i%>"/>


<%
i++;
}
%>
<tr><td><input type="submit" value="submit"></td></tr>
</table>
</form>
</html>


===============================result.jsp============================================

<%@page import="java.sql.*"%>
<html>
<%
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").newInstance();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/idtutorials");
Statement stmt=connection.createStatement();
ResultSet rs=stmt.executeQuery("Select ans from answers");
String ans="";
while(rs.next()){
ans+=rs.getString("ans")+" ";
}
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");
%>
</html>

test.jpg
[Thumbnail for test.jpg]
test.db
answers.jpg
[Thumbnail for answers.jpg]
answers.db
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!