• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ERROR with using JDBC to connect to DB2 Express C

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is your JDBC connection class? How are you adding it to your CLASSPATH?
 
Jonathon Stride
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
..
 
Jonathon Stride
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know. Sorry.

Anybody else?
 
Jonathon Stride
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic