File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes JDBC Driver not found. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC Driver not found." Watch "JDBC Driver not found." New topic
Author

JDBC Driver not found.

Holmes Wong
Ranch Hand

Joined: Feb 18, 2002
Posts: 163
Hi, I am trying to access db2 database using a servlet. The following is the code:

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DisplayServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.print("<html><head>");
out.print("</head><body>");
out.print("<form action=\"");
out.print( req.getRequestURI() );
out.print("\" method=\"post\">");
out.print("<input type=\"submit\" ");
out.print("value=\" \"> ");
out.print("Display Records</form>");
out.print("</body></html>");
doPost(req, res);
out.close();
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.print("<html><head>");
out.print("</head><body>");
out.print("<code><pre>");
out.print("<font color=green>ID\tFirst ");
out.println("Name\tLast Name\n</font>");
// debugging info
long time1 = System.currentTimeMillis();
// connecting to database
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
con = DriverManager.getConnection("jdbc b2:SAMPLE", "name", "password");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM Names");
// displaying records
while(rs.next()) {
out.print(rs.getObject(1).toString());
out.print("\t");
out.print(rs.getObject(2).toString());
out.print("\t\t");
out.print(rs.getObject(3).toString());
out.print("\n");
}

} catch (SQLException e) {
throw new
ServletException("Servlet Could not display records.", e);
} catch (ClassNotFoundException e) {
throw new
ServletException("JDBC Driver not found.", e);
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(con != null) {
con.close();
con = null;
}
} catch (SQLException e) {}
}
// debugging info
long time2 = System.currentTimeMillis();
out.print("</pre></code>");
out.print("<p>Search took : ");
out.print( (time2 - time1) );
out.print(" ms.</p>");
out.print("<p\"><a href=\"");
out.print( req.getRequestURI() );
out.print("\">Back</a></p>");
out.print("</body></html>");
out.close();
}
}

When I run this code using Tomcat4.0, I got the follwoing error:

javax.servlet.ServletException: JDBC Driver not found.
at DisplayServlet.doPost(DisplayServlet.java:77)
at DisplayServlet.doGet(DisplayServlet.java:26)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2344)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:462)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:163)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1011)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1106)
at java.lang.Thread.run(Thread.java:479)

root cause
java.lang.ClassNotFoundException: COM.ibm.db2.jdbc.app.DB2Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1307)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1156)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:310)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:115)
at DisplayServlet.doPost(DisplayServlet.java:55)
at DisplayServlet.doGet(DisplayServlet.java:26)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2344)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:462)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:163)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1011)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1106)
at java.lang.Thread.run(Thread.java:479)

I could access my database without using servlet, why did it give me this error when using servlet? Thx.
One another thing, if I have IBM HTTP server running, I cannot start Tomcat. I have to stop HTTP servers to run Tomcat, I am wondering if this makes any difference. But I still could access database using Java applications. Just weird.
[ December 15, 2002: Message edited by: Holmes Wong ]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi,
don't u have to have the jdbc driver in your webserver's classpath as well? say in the directory where you have ur servlets (though this won't b a good idea to have)...
it might be the case that you have the jdbc dirver available for your application where its not a servlet and you are running it from your system but when you use servlets then we have to make sure that all the classes are available in the directory where webserver looks for the classes..
regards
maulin
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I'll re-iterate what Maulin said.
Application servers don't use the system CLASSPATH, they have their own. This has to do with the scurity of applications running within the servers. Usually you have to put the drivers in a particular place for the server to find them.
For Tomcat, place the jar in the [ TOMCAT_HOME ]/common/lib directory - this will make it available to all applications running on the Tomcat server. A better place is the [ TOMCAT_HOME ]/webapps/[ CONTEXT_NAME ]/WEB_INF/lib directory, since the drivers will only be available to a single application.

One another thing, if I have IBM HTTP server running, I cannot start Tomcat. I have to stop HTTP servers to run Tomcat, I am wondering if this makes any difference. But I still could access database using Java applications. Just weird.
This is because they both try to use the port for communication - 8080.
You can easily change this value in the config settings so that they are looking at different ports eg 8080 and 8081, then they will be able to run at the same time.
Dave
Holmes Wong
Ranch Hand

Joined: Feb 18, 2002
Posts: 163
Thanks, guys. Your answers really enlighted me.
Still not very clear about the jar file David mentioned. For applications, I put "db2java.zip"
file in the CLASSPATH. Are you referring to this .zip file? Should I just put this in [TOMCAT_HOME ]/common/lib directory? Thanks.

Originally posted by David O'Meara:
I'll re-iterate what Maulin said.
Application servers don't use the system CLASSPATH, they have their own. This has to do with the scurity of applications running within the servers. Usually you have to put the drivers in a particular place for the server to find them.
For Tomcat, place the jar in the [ TOMCAT_HOME ]/common/lib directory - this will make it available to all applications running on the Tomcat server. A better place is the [ TOMCAT_HOME ]/webapps/[ CONTEXT_NAME ]/WEB_INF/lib directory, since the drivers will only be available to a single application.

One another thing, if I have IBM HTTP server running, I cannot start Tomcat. I have to stop HTTP servers to run Tomcat, I am wondering if this makes any difference. But I still could access database using Java applications. Just weird.
This is because they both try to use the port for communication - 8080.
You can easily change this value in the config settings so that they are looking at different ports eg 8080 and 8081, then they will be able to run at the same time.
Dave
Holmes Wong
Ranch Hand

Joined: Feb 18, 2002
Posts: 163
Could anyone who knows this help me out on this ?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I believe that is the correct file.
You may have to rename it to .jar for the ClassLoader to pick it up.
There have also been cases where people needed to unzip the file then rejar it, since renaming didn't work.
Dave
Holmes Wong
Ranch Hand

Joined: Feb 18, 2002
Posts: 163
Thank you very much, David. It worked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC Driver not found.