Hi,
1.I created a table called "users" in a database "content" with MS Access
97.
This databse has been registered to ODBC driver.
2. I tried to use a servlet (T3.class) to check login.
This servlet is called by login.html with a form included, which a user
can input
username and password.
3. It worked fine when I input username and password in the HTML form first
time, and click
a submmit button. However, I got following error message when I refresh
the login.html
page, input another set username and password into the form, and click
the submmit
button. The servlet engine is Tomcat3.1 run on Windows 98 with jdk1.3 as
a stand alone mode .
java.sql.SQLException: General error
at sun.jdbc.odbc.JdbcOdbc.throwGenericSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLAllocStmt(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.createStatement(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.createStatement(Unknown Source)
at T3.doPost(T3.java:72)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:503)
at
org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:597)
at
org.apache.tomcat.servlets.InvokerServlet.service(InvokerServlet.java:257)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:503)
at
org.apache.tomcat.core.ContextManager.service(ContextManager.java:559)
at
org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:160)
at
org.apache.tomcat.service.TcpConnectionThread.run(SimpleTcpEndpoint.java:338)
at java.lang.Thread.run(Unknown Source)
Could any one tell me why?
Thank you very much in advance!
Peter
==================
Following are source codes for login.html, and T3.java.
===================
login.html:
==================
<html><title></title><head></head>
<body>
<center>
<form method="post" action="http://localhost/servlet/T3" name="login">
<table>
<tr><td align=right>
Username:
</td><td>
<input type="text" name="username" size="10" maxlength="20">
</td></tr>
<tr><td align=right>
Password:
</td><td>
<input type="text" name="password" size="10" maxlength="20">
</td></tr>
<p></p>
</table>
<table>
<tr><td>
<input type="submit" value="submit">
</td></tr>
</table>
</form>
</center>
</body>
</html>
===================
T3.java
===================
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class T3 extends HttpServlet
{
//database connection is shared by all requests
static Connection dbConn;
/**
* init method is called when servlet is initialized.
* Establishes a database connection when servlet is initially
* loaded that can be shared across all requests.
*/
public void init(ServletConfig config) throws
ServletException
{
super.init(config); //pass ServletConfig to parent
try
{
//load JDBC-ODBC Bridge driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//establish database connection to content using ODBC
dbConn = DriverManager.getConnection(
"jdbc:odbc:content");
}
catch (ClassNotFoundException e) //Class.forName throws
{
System.out.println("JDBC-ODBC bridge not found!");
return;
}
catch (SQLException e) //DriverManager.getConnection throws
{
System.out.println("SQL exception thrown in init!");
return;
}
}
/**
* doGet method is called in response to a GET request
*/
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException
{
int entry = 0;
String CDID = "0";
String username;
String password;
String inputUsername = request.getParameter("username");
String inputPassword = request.getParameter("password");
PrintWriter out = response.getWriter();
try
{
response.setContentType("text/html"); //returns HTML
//get handle to output stream
out.println("username="+inputUsername+" password="+inputPassword);
//create statement
Statement stat = dbConn.createStatement();
//query database for result set
ResultSet rs = stat.executeQuery(
"SELECT * FROM " +
"users");
ResultSetMetaData meta = rs.getMetaData();
int cols = meta.getColumnCount();
out.println("clos=" +cols);
//generate HTML document to return to client
out.println("<HTML>");
out.println("<HEAD><TITLE>User List</TITLE></HEAD>");
out.println("<BODY>");
out.println("<H2>Customer List</H2>");
out.println("<TABLE BORDER=1>"); //create an HTML table
out.println("<TR><TH>Username</TH>");
out.println("<TH>Password</TH>");
out.println("<TH>CDID</TH></TR>");
while (rs.next() && (entry != 2)) //iterate through all records
{
//add a table row for each record
username = rs.getString("Username");
password = rs.getString("Password");
CDID = rs.getString("CDID");
out.println("<TR><TD>" + username + "</TD><TD>"
+ password + "</TD><TD>"
+ CDID + "</TD><TD>"
+ "</TD></TR>");
if(inputUsername.equals(username) &&
inputPassword.equals(password))entry++;
}
out.println("</TABLE>");
out.println("</BODY></HTML>");
//out.close();
//dbConn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
if(entry == 0)
out.println("Error login!");
//response.sendRedirect("http://localhost/loginerror.html");
else if(entry == 1){
if(CDID.equals("1"))response.sendRedirect("http://localhost/page1.html");
if(CDID.equals("2"))response.sendRedirect("http://localhost/page2.html");
}
else
response.sendRedirect("http://localhost/JSP/wheretogo.jsp");
}
/**
* Tells the server about this servlet
*/
public String getServletInfo()
{
return "Sample JDBC servlet";
}
}
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
Share information about yourself, create your own public profile at
http://profiles.msn.com.
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html