Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

servlet issue

Status
Not open for further replies.

rvy2k

Programmer
Jun 29, 2001
39
0
0
US
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:eek: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(&quot;q&quot;+i);
}

// test if the user has answered all the question
String resultsDb = &quot;&quot;;
for (int i=0;i<numQues;i++) {
if (i+1!=numQues) {
resultsDb += &quot;'&quot; + results + &quot;',&quot;;
}
else {
resultsDb += &quot;'&quot; + results + &quot;'&quot;;
}
}
boolean success = insertIntoDb(resultsDb);
// print a thank you message
res.setContentType(&quot;text/html&quot;);
PrintWriter output = res.getWriter();
StringBuffer buffer = new StringBuffer();
buffer.append(&quot;<HTML>&quot;);
buffer.append(&quot;<HEAD>&quot;);
buffer.append(&quot;</HEAD>&quot;);
buffer.append(&quot;<BODY BGCOLOR=\&quot;#FFFFFF\&quot;>&quot;);
buffer.append(&quot;<P>&quot;);
if (success) {
buffer.append(&quot;Thank you for participating!&quot;);
}
else {
buffer.append(&quot;An error has occurred. Please press the back button of your browser&quot;);
buffer.append(&quot; and try again.&quot;);
}
buffer.append(&quot;</P>&quot;);
buffer.append(&quot;</BODY>&quot;);
buffer.append(&quot;</HTML>&quot;);
output.println(buffer.toString());
output.close();

}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
// get info from file
res.setContentType(&quot;text/html&quot;);
PrintWriter output = res.getWriter();
StringBuffer buffer = new StringBuffer();
buffer.append(&quot;<HTML>&quot;);
buffer.append(&quot;<HEAD>&quot;);
buffer.append(&quot;</HEAD>&quot;);
buffer.append(&quot;<BODY BGCOLOR=\&quot;#FFFFFF\&quot;>&quot;);
buffer.append(&quot;<P>&quot;);
///////////////////////////////////////////////////////////////////////
try {
stmt = con.createStatement();
// find the number of participation
for (int i=0;i<1;i++) {
String query = &quot;SELECT q&quot; + i + &quot; FROM &quot; + table;
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
rs.getInt(&quot;q&quot;+i);
num++;
}
}
// loop thru each question
for (int i=0;i<numQues;i++) {
int [] results = new int[num];
String query = &quot;SELECT q&quot; + i + &quot; FROM &quot; + table;
ResultSet rs = stmt.executeQuery(query);
int j=0;
while (rs.next()) {
results[j]=rs.getInt(&quot;q&quot;+i);
j++;
}
//call method
int[] total = percent(results,4);
buffer.append(&quot;Question&quot; + i + &quot;:<BR>&quot;);
for (int k=0;k<4;k++) {
buffer.append(&quot; > Answer &quot; + k + &quot;:&quot; + total[k]);
buffer.append(&quot;<BR>&quot;);
}
buffer.append(&quot;\n&quot;);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
///////////////////////////////////////////////////////////////////////
// display the results
buffer.append(&quot;</P>&quot;);
buffer.append(&quot;</BODY>&quot;);
buffer.append(&quot;</HTML>&quot;);
output.println(buffer.toString());
output.close();
}

public void destroy() {
try {
con.close();
} catch (Exception e) {
System.err.println(&quot;Problem closing the database&quot;);
}
}

public boolean insertIntoDb(String results) {
String query = &quot;INSERT INTO &quot; + table + &quot; VALUES (&quot; + results + &quot;);&quot;;
try {
stmt = con.createStatement();
stmt.execute(query);
stmt.close();
} catch (Exception e) {
System.err.println(&quot;ERROR: Problems with adding new entry&quot;);
e.printStackTrace();
return false;
}
return true;
}

public int [] percent(int [] array, int numOptions) {
System.out.println(&quot;==============================================&quot;);
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(&quot;j=&quot;+j+&quot;\t&quot;+&quot;i=&quot;+i+&quot;\ttotal[j]=&quot;+total[j]);
if (array==j) {
total[j]++;
}
}
}
System.out.println(&quot;==============================================&quot;);
return total;
}
}

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top