Tomcat DB2 connection - No suitable driver

Discussion in 'Java' started by Andrew Johnson, Oct 20, 2004.

  1. Hi,

    I am trying to make a UBD DB2 7.2 connection using the Java
    COM.ibm.db2.jdbc.app.DB2Driver via Tomcat 3.2.1 on Solaris (and also
    on an AIX system with 3.3.1). I am attempting this either via JSP or
    a servlet.

    I have a separate java application that I can run from the command
    line and as long as the user has the correct env variables:

    DB2DIR=/opt/IBMdb2/V7.1
    DB2INSTANCE=repowner
    INSTHOME=/home/repowner

    Then the connection is fine. I am using the same code to try and make
    the connection via tomcat.

    try
    {
    Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
    // next line is printed out
    System.out.println(new java.util.Date().toString() + " DRIVER
    FOUND.");
    }
    catch(ClassNotFoundException e)
    {
    System.out.println(new java.util.Date().toString() + " Error 1:
    "+e);
    }
    String dbType="db2";
    // String ip="10.0.0.185";
    // String port="8790";
    String dbName="ppreport";
    String reason="testing db connection";
    String userName="repowner";
    String password="ilink1";
    Connection con = null;

    String url = "jdbc:db2:"+dbName;

    boolean dbConnected = false;

    try
    {
    System.out.println(new java.util.Date().toString() + " Connecting
    to database URL = " + url + " for " + reason);


    Enumeration e = DriverManager.getDrivers();
    while (e.hasMoreElements()) {
    Driver d = (Driver) e.nextElement();
    // doesn't print anything here
    System.out.println("driver is " + d.toString());
    }
    System.out.println("a1");
    // gets to here and stops!
    Driver d = DriverManager.getDriver(url);
    System.out.println("a1.1"+d.toString());

    con = DriverManager.getConnection(url, userName, password);

    java.sql.Statement stmt = con.createStatement();
    System.out.println("SETTING SCHEMA TO "+dbName);
    try {
    stmt.execute("SET SCHEMA = "+dbName);
    System.out.println("SCHEMA SET.");
    }
    catch (Exception ex) {
    System.out.println("Error Setting Schema:"+ex);
    }

    System.out.println(new java.util.Date().toString() + " Database
    connection established.XX");
    dbConnected = true;
    }
    catch(Exception e)
    {
    System.out.println(new java.util.Date().toString() + " Failed to
    connect, reason : \n" + e);

    }

    However via tomcat the driver cannot be loaded by the class loader.
    The class is found, but not loaded. The error I receive is
    "java.sql.SQLException: No suitable driver"

    I have placed the db2java.zip (renamed as db2java.jar) in several
    places to try and solve the problem (restarting Tomcat each time) but
    to no avail:

    /usr/apache/tomcat/common/lib/db2java.jar
    ..../WEB-INF/classes/db2java.jar

    I have read many posts, but I can't understand how the driver will
    know the correct env variables even if the class is loaded.

    Any help would be much appreciated!!!

    Thanks!
    Andy.
     
    Andrew Johnson, Oct 20, 2004
    #1
    1. Advertising

  2. Andrew Johnson

    Rhino Guest

    "Andrew Johnson" <> wrote in message
    news:...
    > Hi,
    >
    > I am trying to make a UBD DB2 7.2 connection using the Java
    > COM.ibm.db2.jdbc.app.DB2Driver via Tomcat 3.2.1 on Solaris (and also
    > on an AIX system with 3.3.1). I am attempting this either via JSP or
    > a servlet.
    >
    > I have a separate java application that I can run from the command
    > line and as long as the user has the correct env variables:
    >
    > DB2DIR=/opt/IBMdb2/V7.1
    > DB2INSTANCE=repowner
    > INSTHOME=/home/repowner
    >
    > Then the connection is fine. I am using the same code to try and make
    > the connection via tomcat.
    >
    > try
    > {
    > Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
    > // next line is printed out
    > System.out.println(new java.util.Date().toString() + " DRIVER
    > FOUND.");
    > }
    > catch(ClassNotFoundException e)
    > {
    > System.out.println(new java.util.Date().toString() + " Error 1:
    > "+e);
    > }
    > String dbType="db2";
    > // String ip="10.0.0.185";
    > // String port="8790";
    > String dbName="ppreport";
    > String reason="testing db connection";
    > String userName="repowner";
    > String password="ilink1";
    > Connection con = null;
    >
    > String url = "jdbc:db2:"+dbName;
    >
    > boolean dbConnected = false;
    >
    > try
    > {
    > System.out.println(new java.util.Date().toString() + " Connecting
    > to database URL = " + url + " for " + reason);
    >
    >
    > Enumeration e = DriverManager.getDrivers();
    > while (e.hasMoreElements()) {
    > Driver d = (Driver) e.nextElement();
    > // doesn't print anything here
    > System.out.println("driver is " + d.toString());
    > }
    > System.out.println("a1");
    > // gets to here and stops!
    > Driver d = DriverManager.getDriver(url);
    > System.out.println("a1.1"+d.toString());
    >
    > con = DriverManager.getConnection(url, userName, password);
    >
    > java.sql.Statement stmt = con.createStatement();
    > System.out.println("SETTING SCHEMA TO "+dbName);
    > try {
    > stmt.execute("SET SCHEMA = "+dbName);
    > System.out.println("SCHEMA SET.");
    > }
    > catch (Exception ex) {
    > System.out.println("Error Setting Schema:"+ex);
    > }
    >
    > System.out.println(new java.util.Date().toString() + " Database
    > connection established.XX");
    > dbConnected = true;
    > }
    > catch(Exception e)
    > {
    > System.out.println(new java.util.Date().toString() + " Failed to
    > connect, reason : \n" + e);
    >
    > }
    >
    > However via tomcat the driver cannot be loaded by the class loader.
    > The class is found, but not loaded. The error I receive is
    > "java.sql.SQLException: No suitable driver"
    >
    > I have placed the db2java.zip (renamed as db2java.jar) in several
    > places to try and solve the problem (restarting Tomcat each time) but
    > to no avail:
    >
    > /usr/apache/tomcat/common/lib/db2java.jar
    > .../WEB-INF/classes/db2java.jar
    >
    > I have read many posts, but I can't understand how the driver will
    > know the correct env variables even if the class is loaded.
    >
    > Any help would be much appreciated!!!
    >

    Andy,

    It might be wise for you to post this problem to comp.lang.java.databases
    and/or comp.databases.ibm-db2 since those forums are more closely attuned to
    database issues.

    Rhino
     
    Rhino, Oct 20, 2004
    #2
    1. Advertising

  3. Andrew Johnson

    Sudsy Guest

    Andrew Johnson wrote:
    <snip>
    > /usr/apache/tomcat/common/lib/db2java.jar
    > .../WEB-INF/classes/db2java.jar
    >
    > I have read many posts, but I can't understand how the driver will
    > know the correct env variables even if the class is loaded.
    >
    > Any help would be much appreciated!!!


    The file should be placed in WEB-INF/lib and leave it as a ZIP file.

    --
    Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.
     
    Sudsy, Oct 20, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Nentwig

    DB2: no suitable driver

    Timo Nentwig, Sep 20, 2003, in forum: Java
    Replies:
    9
    Views:
    11,404
    Hugo Pragt
    Sep 23, 2003
  2. Replies:
    0
    Views:
    4,644
  3. Replies:
    0
    Views:
    7,675
  4. Steve
    Replies:
    0
    Views:
    254
    Steve
    Feb 28, 2006
  5. Dinesh
    Replies:
    0
    Views:
    182
    Dinesh
    Apr 3, 2006
Loading...

Share This Page