aspose file tools*
The moose likes Servlets and the fly likes Database access with servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Database access with servlet" Watch "Database access with servlet" New topic
Author

Database access with servlet

Manoj Singh
Ranch Hand

Joined: Aug 10, 2000
Posts: 41
HI guys !
I trying to access database through my servlet. I am using Visual age for Java. I am using access database for which I have created a dsn on my machine.
But this the error I am getting on invoking the servlet
<b>
Error 405
An error has occured while processing request:http://localhost:8080/servlet/aetna.DBServlet
Message: HTTP method GET is not supported by this URL
Target Servlet: aetna.DBServlet
StackTrace:
--------------------------------------------------------------------------------
Root Error-1: HTTP method GET is not supported by this URL
com.ibm.servlet.engine.webapp.WebAppErrorReport: HTTP method GET is not supported by this URL
java.lang.Throwable(java.lang.String)
java.lang.Exception(java.lang.String)
javax.servlet.ServletException(java.lang.String)
com.ibm.websphere.servlet.error.ServletErrorReport(java.lang.String)
com.ibm.servlet.engine.webapp.WebAppErrorReport(java.lang.String)
void com.ibm.servlet.engine.webapp.WebAppDispatcherResponse.sendError(int, java.lang.String)
void javax.servlet.http.HttpServlet.doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
void javax.servlet.http.HttpServlet.service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
void javax.servlet.http.HttpServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.StrictServletInstance.doService(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.IdleServletState.service(com.ibm.servlet.engine.webapp.StrictLifecycleServlet, javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.StrictLifecycleServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.ServletInstance.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse, com.ibm.servlet.engine.webapp.WebAppServletInvocationEvent)
void com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(com.ibm.servlet.engine.webapp.ServletInstanceReference, javax.servlet.ServletRequest, javax.servlet.ServletResponse, com.ibm.servlet.engine.webapp.WebAppServletInvocationEvent)
void com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch(javax.servlet.ServletRequest, javax.servlet.ServletResponse, com.ibm.servlet.engine.webapp.WebAppServletInvocationEvent)
void com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(com.ibm.servlet.engine.webapp.WebAppRequest, javax.servlet.http.HttpServletResponse, boolean)
void com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(javax.servlet.ServletRequest, javax.servlet.ServletResponse, boolean)
void com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.InvokerServlet.service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
void javax.servlet.http.HttpServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.StrictServletInstance.doService(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.IdleServletState.service(com.ibm.servlet.engine.webapp.StrictLifecycleServlet, javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.StrictLifecycleServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.ServletInstance.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse, com.ibm.servlet.engine.webapp.WebAppServletInvocationEvent)
void com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(com.ibm.servlet.engine.webapp.ServletInstanceReference, javax.servlet.ServletRequest, javax.servlet.ServletResponse, com.ibm.servlet.engine.webapp.WebAppServletInvocationEvent)
void com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch(javax.servlet.ServletRequest, javax.servlet.ServletResponse, com.ibm.servlet.engine.webapp.WebAppServletInvocationEvent)
void com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(com.ibm.servlet.engine.webapp.WebAppRequest, javax.servlet.http.HttpServletResponse, boolean)
void com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(javax.servlet.ServletRequest, javax.servlet.ServletResponse, boolean)
void com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.srt.WebAppInvoker.handleInvocationHook(java.lang.Object)
void com.ibm.servlet.engine.invocation.CachedInvocation.handleInvocation(java.lang.Object)
void com.ibm.servlet.engine.srp.ServletRequestProcessor.dispatchByURI(java.lang.String, com.ibm.servlet.engine.srp.ISRPConnection)
void com.ibm.servlet.engine.oselistener.OSEListenerDispatcher.service(com.ibm.servlet.engine.oselistener.api.IOSEConnection)
void com.ibm.servlet.engine.http_transport.HttpTransportHandler.handleConnection(java.net.Socket)
void com.ibm.servlet.engine.http_transport.HttpTransportHandler.run()
void java.lang.Thread.run()
</b>
This is my code:
<b><i>
public void performTask(HttpServletRequest request, HttpServletResponse response) throws IOException {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String user="n017710";
String pwd ="190103";
String dsn = "jdbc dbc:mydsn";
response.setContentType("text/html");
PrintWriter out = response.getWriter();

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(dsn,user,pwd);

stmt = con.createStatement();

rs = stmt.executeQuery("Select * from employees");
out.println("<html><head><title>Employees</title></head>");
out.println("<body>");
while(rs.next()) {
out.println("<b>" + rs.getString("Name") + " " + rs.getString("password"));
}
out.println("</b></body></html>");
}catch(ClassNotFoundException e){
out.println("COuldn't load database driver " + e);
}catch(SQLException e){
out.println("SQL Exception caught: "+ e);
}finally {
try {
if(con != null)
con.close();
}catch (SQLException e){
System.out.println(e);
}
}
}
</b></i>
Waiting for replies ....
thnx Manoj

Sun Certified Java 2 Programmer<BR>" Only those who will risk going too far can possibly find out how far one can go "
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Am I missing something here? I did not see where you are overiding the GET or POST method.

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Manoj Singh
Ranch Hand

Joined: Aug 10, 2000
Posts: 41
The full code is given below:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Creation date: (09/06/2001 9:44:11 AM)
* @author: Manoj Singh
*/
public class DBServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
performTask(request,response);

}
/**
* Process incoming requests for information
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void performTask(HttpServletRequest request, HttpServletResponse response) throws IOException {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String user="n017710";
String pwd ="190103";
String dsn = "jdbc dbc:mydsn";
response.setContentType("text/html");
PrintWriter out = response.getWriter();

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(dsn,user,pwd);

stmt = con.createStatement();

rs = stmt.executeQuery("Select * from employees");
out.println("<html><head><title>Employees</title></head>");
out.println("<body><b>");
//while(rs.next()) {
//out.println(rs.getString("Name"));
//}
out.println("</b></body></html>");
}catch(ClassNotFoundException e){
out.println("COuldn't load database driver " + e);
}catch(SQLException e){
out.println("SQL Exception caught: "+ e);
}finally {
try {
if(con != null)
con.close();
}catch (SQLException e){
System.out.println(e);
}
}

}


}
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

I'd check the web.xml file. It looks like the server implementation's HttpServlet.doGet method may be checking for authority and finding that it's denied so it throws an exception.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database access with servlet