Bug in storing data
I have a problem with an application I built using Flash for the front end and servlets/beans for the backend.
The problem doesn't throw an exception error but something is wrong. I hope I am not posting this in the wrong section but I posted it in the JSP area and no one offered any help.
Anyway my problem is that when the user interacts with the application, the results from the exercises are to be stored in an access database. However sometimes the scores aren't stored even though Flash sends the data every time. So I think the problem is in the servlet I created to store the results.
Can you take a look at the servlet I created and tell me where the discrepancy may lie. I created the servlet while I was learning J2EE and haven't touched it since with the idea of 'if it aint broke don't fix it' however it is broke as this bug exists.
/*
* StoreData.java
*
* Created on 03 December 2003, 16:53
*/
package virtualward.java;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.lang.*;
/**
*
*
*
*/
publicclass StoreDataextends HttpServlet{
//Connect to database
public Connection conn =null;
public Statement st =null;
public ResultSet rs =null;
int count;
//vars from flash to compare with db data
int patient;
int patientComplete;
int day;
int month;
int year;
int hour;
int minute;
String timeDurationS;
int studentId;
int score;
int maxScore;
int accessTime;
String assessment;
String chosenItems;
String firstDataStored;
String secondDataStored;
//other vars
String theDate;
String theTime;
/** Initializes the servlet.
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protectedvoid doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException{
//connecting to the database
// ServletOutputStream out = resp.getOutputStream();
resp.setContentType("text/html");
ServletInputStream in = req.getInputStream();
ServletOutputStream out = resp.getOutputStream();
try{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
conn = DriverManager.getConnection("jdbc:odbc:virtualWardServer","****","*******");// server dsn virtualWardServer, local dsn virtualward
//getting the data from flash variables and converting them from strings to ints.
patient = Integer.parseInt(req.getParameter("patient"));
patientComplete = Integer.parseInt(req.getParameter("patientComplete"));
day = Integer.parseInt(req.getParameter("day"));
month = Integer.parseInt(req.getParameter("month"));
year = Integer.parseInt(req.getParameter("year"));
hour = Integer.parseInt(req.getParameter("hour"));
minute = Integer.parseInt(req.getParameter("minute"));
timeDurationS = req.getParameter("durationCut");
studentId = Integer.parseInt(req.getParameter("studentId"));
score = Integer.parseInt(req.getParameter("score"));
maxScore = Integer.parseInt(req.getParameter("maxScore"));
assessment = req.getParameter("assessment");
chosenItems = req.getParameter("chosenItems");
patient = Integer.parseInt(req.getParameter("patient"));
theDate = day+"/"+month+"/"+year;
theTime = hour+":"+minute;
try{
/**************************************************************************************************************88
* insert latest scores into the table
*/
String template ="INSERT INTO TbResults (IDStudent, ScoreDate, AccessTime, Patient, Duration,"+
"Assessment, Score, ScoreLimit, ChosenItems) VALUES (?,?,?,?,?,?,?,?,?);";
// create a prepared statement for storing the data
PreparedStatement pstmt = conn.prepareStatement(template);
pstmt.setInt(1, studentId);
pstmt.setString(2, theDate);
pstmt.setString(3, theTime);
pstmt.setInt(4, patient);
pstmt.setString(5, timeDurationS);
pstmt.setString(6, assessment);
pstmt.setInt(7, score);
pstmt.setInt(8, maxScore);
pstmt.setString(9, chosenItems);
pstmt.executeUpdate();
pstmt.close();
firstDataStored ="Stored score";
}
catch (Exception ex){
// out.print("&quesLoaded=failed&");
// out.println("Exception 01 message is: "+ ex.getMessage() + "\nStacktrace shows:");
ex.printStackTrace();
// out.println("String representation is\n "+ ex.toString());
}
/**************************************************************************************************************88
updating the patient level the student is at*/
try{
String template ="UPDATE TbStudent SET PatientLevel = ? WHERE IDStudent = ?;";
PreparedStatement pstmt = conn.prepareStatement(template);
pstmt.setInt(1, patientComplete);
pstmt.setInt(2, studentId);
pstmt.executeUpdate();
pstmt.close();
secondDataStored ="Data stored ok! Please close this window to continue.";
}
catch (Exception ex){
ex.printStackTrace();
}
finally{
try{
if (conn !=null) conn.close();
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
catch (Exception ex){
ex.printStackTrace();
}
}
protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException{
doPost(req, resp);
}
}

