Hi,
I have a servlet that is using a database to query results from a survey. The survey has 3 questions and each one of them has 4 possible answers. The query runs fine and displays in the web browser, but once I do a refresh some of the data increments its value at a constant rate.
Here is the code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.StringTokenizer;
public class Survey extends HttpServlet {
private Connection con = null;
private Statement stmt = null;
private String url = "jdbc
dbc:survey";
private String table = "results";
private int numQues = 3;
private int [] numAns = {4,4,4};
private int num = 0;
public void init() throws ServletException {
try {
// loading the jdbc-odbc bridge
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"
;
// making a connection
con = DriverManager.getConnection(url,"anonymous","guest"
;
} catch (Exception e) {
e.printStackTrace();
con = null;
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String [] results = new String[numQues];
for (int i=0;i<numQues;i++) {
results = req.getParameter("q"+i);
}
// test if the user has answered all the question
String resultsDb = "";
for (int i=0;i<numQues;i++) {
if (i+1!=numQues) {
resultsDb += "'" + results + "',";
}
else {
resultsDb += "'" + results + "'";
}
}
boolean success = insertIntoDb(resultsDb);
// print a thank you message
res.setContentType("text/html"
;
PrintWriter output = res.getWriter();
StringBuffer buffer = new StringBuffer();
buffer.append("<HTML>"
;
buffer.append("<HEAD>"
;
buffer.append("</HEAD>"
;
buffer.append("<BODY BGCOLOR=\"#FFFFFF\">"
;
buffer.append("<P>"
;
if (success) {
buffer.append("Thank you for participating!"
;
}
else {
buffer.append("An error has occurred. Please press the back button of your browser"
;
buffer.append(" and try again."
;
}
buffer.append("</P>"
;
buffer.append("</BODY>"
;
buffer.append("</HTML>"
;
output.println(buffer.toString());
output.close();
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
// get info from file
res.setContentType("text/html"
;
PrintWriter output = res.getWriter();
StringBuffer buffer = new StringBuffer();
buffer.append("<HTML>"
;
buffer.append("<HEAD>"
;
buffer.append("</HEAD>"
;
buffer.append("<BODY BGCOLOR=\"#FFFFFF\">"
;
buffer.append("<P>"
;
///////////////////////////////////////////////////////////////////////
try {
stmt = con.createStatement();
// find the number of participation
for (int i=0;i<1;i++) {
String query = "SELECT q" + i + " FROM " + table;
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
rs.getInt("q"+i);
num++;
}
}
// loop thru each question
for (int i=0;i<numQues;i++) {
int [] results = new int[num];
String query = "SELECT q" + i + " FROM " + table;
ResultSet rs = stmt.executeQuery(query);
int j=0;
while (rs.next()) {
results[j]=rs.getInt("q"+i);
j++;
}
//call method
int[] total = percent(results,4);
buffer.append("Question" + i + ":<BR>"
;
for (int k=0;k<4;k++) {
buffer.append(" > Answer " + k + ":" + total[k]);
buffer.append("<BR>"
;
}
buffer.append("\n"
;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
///////////////////////////////////////////////////////////////////////
// display the results
buffer.append("</P>"
;
buffer.append("</BODY>"
;
buffer.append("</HTML>"
;
output.println(buffer.toString());
output.close();
}
public void destroy() {
try {
con.close();
} catch (Exception e) {
System.err.println("Problem closing the database"
;
}
}
public boolean insertIntoDb(String results) {
String query = "INSERT INTO " + table + " VALUES (" + results + "
;";
try {
stmt = con.createStatement();
stmt.execute(query);
stmt.close();
} catch (Exception e) {
System.err.println("ERROR: Problems with adding new entry"
;
e.printStackTrace();
return false;
}
return true;
}
public int [] percent(int [] array, int numOptions) {
System.out.println("=============================================="
;
int [] total = new int[numOptions];
// initialize array
for (int i=0;i<total.length;i++) {
total=0;
}
for (int j=0;j<numOptions;j++) {
for (int i=0;i<array.length;i++) {
System.out.println("j="+j+"\t"+"i="+i+"\ttotal[j]="+total[j]);
if (array==j) {
total[j]++;
}
}
}
System.out.println("=============================================="
;
return total;
}
}
Thanks!
I have a servlet that is using a database to query results from a survey. The survey has 3 questions and each one of them has 4 possible answers. The query runs fine and displays in the web browser, but once I do a refresh some of the data increments its value at a constant rate.
Here is the code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.StringTokenizer;
public class Survey extends HttpServlet {
private Connection con = null;
private Statement stmt = null;
private String url = "jdbc
private String table = "results";
private int numQues = 3;
private int [] numAns = {4,4,4};
private int num = 0;
public void init() throws ServletException {
try {
// loading the jdbc-odbc bridge
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"
// making a connection
con = DriverManager.getConnection(url,"anonymous","guest"
} catch (Exception e) {
e.printStackTrace();
con = null;
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String [] results = new String[numQues];
for (int i=0;i<numQues;i++) {
results = req.getParameter("q"+i);
}
// test if the user has answered all the question
String resultsDb = "";
for (int i=0;i<numQues;i++) {
if (i+1!=numQues) {
resultsDb += "'" + results + "',";
}
else {
resultsDb += "'" + results + "'";
}
}
boolean success = insertIntoDb(resultsDb);
// print a thank you message
res.setContentType("text/html"
PrintWriter output = res.getWriter();
StringBuffer buffer = new StringBuffer();
buffer.append("<HTML>"
buffer.append("<HEAD>"
buffer.append("</HEAD>"
buffer.append("<BODY BGCOLOR=\"#FFFFFF\">"
buffer.append("<P>"
if (success) {
buffer.append("Thank you for participating!"
}
else {
buffer.append("An error has occurred. Please press the back button of your browser"
buffer.append(" and try again."
}
buffer.append("</P>"
buffer.append("</BODY>"
buffer.append("</HTML>"
output.println(buffer.toString());
output.close();
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
// get info from file
res.setContentType("text/html"
PrintWriter output = res.getWriter();
StringBuffer buffer = new StringBuffer();
buffer.append("<HTML>"
buffer.append("<HEAD>"
buffer.append("</HEAD>"
buffer.append("<BODY BGCOLOR=\"#FFFFFF\">"
buffer.append("<P>"
///////////////////////////////////////////////////////////////////////
try {
stmt = con.createStatement();
// find the number of participation
for (int i=0;i<1;i++) {
String query = "SELECT q" + i + " FROM " + table;
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
rs.getInt("q"+i);
num++;
}
}
// loop thru each question
for (int i=0;i<numQues;i++) {
int [] results = new int[num];
String query = "SELECT q" + i + " FROM " + table;
ResultSet rs = stmt.executeQuery(query);
int j=0;
while (rs.next()) {
results[j]=rs.getInt("q"+i);
j++;
}
//call method
int[] total = percent(results,4);
buffer.append("Question" + i + ":<BR>"
for (int k=0;k<4;k++) {
buffer.append(" > Answer " + k + ":" + total[k]);
buffer.append("<BR>"
}
buffer.append("\n"
}
} catch (SQLException ex) {
ex.printStackTrace();
}
///////////////////////////////////////////////////////////////////////
// display the results
buffer.append("</P>"
buffer.append("</BODY>"
buffer.append("</HTML>"
output.println(buffer.toString());
output.close();
}
public void destroy() {
try {
con.close();
} catch (Exception e) {
System.err.println("Problem closing the database"
}
}
public boolean insertIntoDb(String results) {
String query = "INSERT INTO " + table + " VALUES (" + results + "
try {
stmt = con.createStatement();
stmt.execute(query);
stmt.close();
} catch (Exception e) {
System.err.println("ERROR: Problems with adding new entry"
e.printStackTrace();
return false;
}
return true;
}
public int [] percent(int [] array, int numOptions) {
System.out.println("=============================================="
int [] total = new int[numOptions];
// initialize array
for (int i=0;i<total.length;i++) {
total=0;
}
for (int j=0;j<numOptions;j++) {
for (int i=0;i<array.length;i++) {
System.out.println("j="+j+"\t"+"i="+i+"\ttotal[j]="+total[j]);
if (array==j) {
total[j]++;
}
}
}
System.out.println("=============================================="
return total;
}
}
Thanks!