Topic
  • 5 replies
  • Latest Post - ‏2012-02-22T16:40:19Z by jguillaumes
SystemAdmin
SystemAdmin
1731 Posts

Pinned topic pls help db2 connection exception

‏2006-12-22T08:50:50Z |
am new to db2
my java code is not establishing connection with db2.
when i exceute my code iam getting the following exception:

openConnection com.ibm.db2.jcc.b.SqlException: Failure in loading T2 native library db2jcct2

pls help
arvin
Updated on 2012-02-22T16:40:19Z at 2012-02-22T16:40:19Z by jguillaumes
  • SystemAdmin
    SystemAdmin
    1731 Posts

    Re: pls help db2 connection exception

    ‏2006-12-22T13:38:29Z  
    What's your classpath and path set to?

    Posting your connection code might help as well.
  • SystemAdmin
    SystemAdmin
    1731 Posts

    Re: pls help db2 connection exception

    ‏2006-12-23T11:25:00Z  
    hi,

    my connection code is like this

    public Connection openConnection()
    {
    Connection con=null;
    try
    {
    String strIP="localhost";
    Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
    con=DriverManager.getConnection "jdbc:db2:db","administrator","administrator");
    }
    catch(Exception e)
    {
    System.out.println("Exception in openConnection "+ e);
    }

    i have set classpath to
    db2java.zip
    db2jcc_license_cu.jar
    db2jcc_license_cisuz.jar
    db2jcc.jar

    thanks
    arvin
  • SystemAdmin
    SystemAdmin
    1731 Posts

    Sample Java program which connects DB2 database

    ‏2011-10-07T08:36:46Z  
    hi,

    my connection code is like this

    public Connection openConnection()
    {
    Connection con=null;
    try
    {
    String strIP="localhost";
    Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
    con=DriverManager.getConnection "jdbc:db2:db","administrator","administrator");
    }
    catch(Exception e)
    {
    System.out.println("Exception in openConnection "+ e);
    }

    i have set classpath to
    db2java.zip
    db2jcc_license_cu.jar
    db2jcc_license_cisuz.jar
    db2jcc.jar

    thanks
    arvin
    Pls find the sample java source code to connect DB2 database.

    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.DriverManager;
    import java.sql.Connection;
    import java.sql.SQLException;

    public class DB2Connect
    {

    + public static void main(String[] argv)+
    + {+

    + try+
    + {+

    + Class.forName("com.ibm.db2.jcc.DB2Driver");+
    + }+
    + catch (ClassNotFoundException e)+
    + {+

    + System.out.println("Please include Classpath Where your DB2 Driver is located");+
    + e.printStackTrace();+
    + return;+
    + }+

    + System.out.println("DB2 driver is loaded successfully");+
    + +
    + Connection conn = null;+
    + PreparedStatement pstmt = null;+
    + ResultSet rset=null;+
    + boolean found=false;+

    + try+
    + {+

    + String url = "jdbc:db2://localhost:50000/db2DbName"; // localhost (or) IP Address+
    + String userName = "db2_usr";+
    + String passWord = "db2_pwd";+

    + conn = DriverManager.getConnection(url, userName, passWord);+
    + if (conn != null)+
    + {+

    + System.out.println("DB2 Database Connected");+
    + }+
    + else+
    + {+
    + +
    + System.out.println("Db2 connection Failed ");+
    + }+

    + pstmt=conn.prepareStatement("select sysdate from dual");+
    + rset=pstmt.executeQuery();+
    + +
    + if(rset!=null)+
    + {+

    + while(rset.next())+
    + {+

    + found=true;+
    + System.out.println("System Date: "rset.getString(1));
    + }+
    + }+

    + if (found ==false)+
    + {+

    + System.out.println("No Information Found");+
    + }+
    + }+
    + catch (SQLException e)+
    + {+

    + System.out.println("DB2 Database connection Failed");+
    + e.printStackTrace();+
    + return;+
    + }+
    + }+
    }
    Commands to execute program:

    javac -classpath .;db2jcc4.jar;db2jcc_license_cu.jar DB2Connect.java

    java -classpath .;db2jcc4.jar;db2jcc_license_cu.jar DB2Connect
  • SystemAdmin
    SystemAdmin
    1731 Posts

    Re: Sample Java program which connects DB2 database

    ‏2012-02-22T15:15:40Z  
    Pls find the sample java source code to connect DB2 database.

    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.DriverManager;
    import java.sql.Connection;
    import java.sql.SQLException;

    public class DB2Connect
    {

    + public static void main(String[] argv)+
    + {+

    + try+
    + {+

    + Class.forName("com.ibm.db2.jcc.DB2Driver");+
    + }+
    + catch (ClassNotFoundException e)+
    + {+

    + System.out.println("Please include Classpath Where your DB2 Driver is located");+
    + e.printStackTrace();+
    + return;+
    + }+

    + System.out.println("DB2 driver is loaded successfully");+
    + +
    + Connection conn = null;+
    + PreparedStatement pstmt = null;+
    + ResultSet rset=null;+
    + boolean found=false;+

    + try+
    + {+

    + String url = "jdbc:db2://localhost:50000/db2DbName"; // localhost (or) IP Address+
    + String userName = "db2_usr";+
    + String passWord = "db2_pwd";+

    + conn = DriverManager.getConnection(url, userName, passWord);+
    + if (conn != null)+
    + {+

    + System.out.println("DB2 Database Connected");+
    + }+
    + else+
    + {+
    + +
    + System.out.println("Db2 connection Failed ");+
    + }+

    + pstmt=conn.prepareStatement("select sysdate from dual");+
    + rset=pstmt.executeQuery();+
    + +
    + if(rset!=null)+
    + {+

    + while(rset.next())+
    + {+

    + found=true;+
    + System.out.println("System Date: "rset.getString(1));
    + }+
    + }+

    + if (found ==false)+
    + {+

    + System.out.println("No Information Found");+
    + }+
    + }+
    + catch (SQLException e)+
    + {+

    + System.out.println("DB2 Database connection Failed");+
    + e.printStackTrace();+
    + return;+
    + }+
    + }+
    }
    Commands to execute program:

    javac -classpath .;db2jcc4.jar;db2jcc_license_cu.jar DB2Connect.java

    java -classpath .;db2jcc4.jar;db2jcc_license_cu.jar DB2Connect
    i fixed a similar problem error by importing the package javax.sql.*   from IBM jar file distribution db2jcc_javax.jar

    regards, paolo.
  • jguillaumes
    jguillaumes
    8 Posts

    Re: pls help db2 connection exception

    ‏2012-02-22T16:40:19Z  
    It seems like you are trying to use the type 2 (native) drivers, so the shared library db2jcct has to be in your DLL path (the way of specifying that depends on your operating system... for windows it has to be in the PATH, while for most unices it should be in the LD_LIBRARY_PATH).

    You probably will want to use the type 4 driver (pure java) instead. To do so, be sure you specify the connection URL in this way:

    jdbc:db2://your_host_name_here:your_port_number_here/database_name

    For instance, for the SAMPLE database in a server running in your own machine using the default port (50000) it should be:

    jdbc:db2://localhost:50000/SAMPLE

    If you are using a connect uri of the style:

    jdbc:db2:SAMPLE

    then you will be using the T2 driver and hence getting that exception.