*** URGENT HELP REQUIRED: Servlet cant access DB over JDBC
Hi Guys, This may sound silly.. but i had problems when my Servlet tried to access the Db using JDBC.
I am using PostgreSQL 8 as DB.
The JDBC code that has been used accessing the DB, was also implemented in a desktop application and worked fine. but when it came to servlets there were problems.
Here is the code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
publicclass VerifyLoginextends HttpServlet
{
publicvoid doGet (HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
{
String user_id= req.getParameter ("uid");
String pass= req.getParameter ("pwd");
try
{
Class.forName("org.postgresql.Driver");
}
catch (ClassNotFoundException cnfe)
{
res.sendRedirect("/project/Error1");
}
System.out.println("Registered the driver ok, so let's make a connection.");
Connection c =null;
try
{
c = DriverManager.getConnection("jdbc:postgresql://localhost/bookstore","postgres","password");
}
catch (SQLException se)
{
res.sendRedirect("/project/Error2");
}
try
{
PreparedStatement login = c.prepareStatement("SELECt password FROM login WHERE username=?");
login.setString(1,user_id);
ResultSet rs=login.executeQuery();
while (rs.next())
{
if ( pass.equals(rs.getString(1)))
{
res.sendRedirect("/project/Success");
}
else
{
res.sendRedirect("/project/Login");}
}
}
catch ( SQLException e)
{
}
}
}
And this is the error I got:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfillingthis request.
exception
java.lang.IllegalStateException
org.apache.coyote.tomcat5.CoyoteResponseFacade.sendRedirect(CoyoteResponseFacade.java:418)
VerifyLogin.doGet(VerifyLogin.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:747)
javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
sun.reflect.GeneratedMethodAccessor60.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:249)
java.security.AccessController.doPrivileged(Native Method)
javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:282)
org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:165)
note The full stack trace of the root cause is available in the Sun-Java-System/Application-Server logs.
Where is the problem? Please help me out.. I am at my wits end.

