File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes ERROR with using JDBC to connect to DB2 Express C Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "ERROR with using JDBC to connect to DB2 Express C" Watch "ERROR with using JDBC to connect to DB2 Express C" New topic
Author

ERROR with using JDBC to connect to DB2 Express C

Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
IDE : Eclipse (latest)
Server : Tomcat 5.5 ( 6.0 has a weird bug )
JRE Version : 6.0

following line generates the error :

10:
11: boolean flag=false;
12:
13: Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
14: Connection con = DriverManager.getConnection("jdbc:db2:WorkExa");
15:
16: Statement stmt= con.createStatement();


a ClassNotFoundException and ServletException error occurs



error Stacktrace:

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

javax.servlet.ServletException: COM.ibm.db2.jdbc.app.DB2Driver
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:841)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:774)
org.apache.jsp.loginhandler_jsp._jspService(loginhandler_jsp.java:157)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.lang.ClassNotFoundException: COM.ibm.db2.jdbc.app.DB2Driver
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1386)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1232)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:125)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
java.lang.ClassLoader.loadClassInternal(Unknown Source)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Unknown Source)
org.apache.jsp.loginhandler_jsp._jspService(loginhandler_jsp.java:63)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

2 interviews failed cause of not having SCJP ( and counting...)
not anymore !

SCJP 6 (70%)

now ready to count other reasons :P...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
Where is your JDBC connection class? How are you adding it to your CLASSPATH?
Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
here is the top of the JSP page from where im calling the database

___________________________________________
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" language="java"
import="java.sql.*,java.io.*,com.me.company.*,java.util.*,java.text.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<%
String loginid= request.getParameter("user");
String password = request.getParameter("pass");
String type = request.getParameter("type");

boolean flag=false;

Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
Connection con = DriverManager.getConnection("jdbc:db2:WorkExa");

Statement stmt= con.createStatement();
String query ="select * from login where loginid ='" + loginid + "' and type='"+type+"'";
ResultSet rs = stmt.executeQuery(query);



<some more code here >
____________________________________________

com.me.company contains all the servlets that i have defined.

should this be done inside a servlet ?(calling the db)

im using a dynamic web project in eclipse , im assuming it includes jre6 while exporting ?





on the internet i checked up and copied the file 'db2jcc' , 'db2jcc4' and 'db2jcc_license_cu' into(from somewhere in DB2 installation)

1. <Apache home directory>\common\lib\

2. <Apache home directory >\webapps\<project folder extracted from WAR that i export>\WEB_INF\lib\

3. C:\Program Files\Java\jre6\lib\ext\

but still i get the same error when i browse to the jsp page from apache



Books im using :
Kogent J2EE projects
Oreilly HF Servlets and JSP ( Exam book but its good for understanding too)
Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
Campbell Ritchie wrote:Where is your JDBC connection class? How are you adding it to your CLASSPATH?

is jdbc integrated inside jre6 ?

jre6 is integrated into eclipse ( it prompts for an installation of JRE before installing )

inside the project i can set the build path and link folders into the project

i have linked the jre6 as well as a folders containing the db2jcc* files
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
Jonathon Stride wrote:is jdbc integrated inside jre6 ?
Not as far as I know.
jre6 is integrated into eclipse ( it prompts for an installation of JRE before installing )
No it isn't. It is used from outside Eclipse.
inside the project i can set the build path and link folders into the project

i have linked the jre6 as well as a folders containing the db2jcc* files
Did you use the "external jars" window in Eclipse?
Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
..
Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
i went to < project name > - > right click -> import -> Java EE -> App Client JAR file -> selected each file one by one and included it(db2jcc* files)

in the SS you can see that the files are also present in the imported JRE
<p name >\Java Resources\Libraries\JRE System Library\< all the jre file JARs>




as they are present in C:\Java\JRE\lib\ext\ folder

The entire folder with JRE and db2jcc is also included directly
as
<p name>\Java Resources\db2jcc
and
<p name>\Java Resources\jre6

what else do you think can cause the problem im having ?
is tomcat not capable of connecting to db2 ?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
Don't know. Sorry.

Anybody else?
Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
Campbell Ritchie wrote:

.....


thanks a lot !

apparently the driver problem was solved yesterday by copying the jars and db2jcc* files
however i was getting more errors from the query part (mostly cause of db2 not being set up properly)



the working code ( for reference of others having a similar problem :- )



<%@ page import ="java.sql.*" %>
<%

Class.forName("com.ibm.db2.jcc.DB2Driver");
Connection cn=DriverManager.getConnection("jdbc:db2:WorkExa","","");

String query="select * from login";
Statement stmt=cn.createStatement();

out.println("<br>Going into query");

ResultSet rs=stmt.executeQuery(query);

while(rs.next())
{
out.println("<br>"+rs.getString(1));
out.println("<br>"+rs.getString(2));
out.println("<br>"+rs.getString(3));
}
%>


_______________________________________________________

considering all the JARs are setup properly as mentioned in above posts
upon exporting the project WAR to the tomcat webapps folder and running it
the above code successfully prints the contents of the table ( login - which has 3 columns)
Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
advice from a java noobie (wannabe pro) to all java coders !!

try
{

}
catch(Exception e)
{

}

^^^^^^^^^^^^^ Helps a lot in finding where exactly the error is coming from
even if your program is 6 lines , use it :P

saves a lot of headache
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ERROR with using JDBC to connect to DB2 Express C
 
Similar Threads
problem in running a servlet
Axis2 - WSDL to code
what is this error
to retreive datas from mysql table in jsp
JSP-database problem