0

I'm relatively new to SQL, I am having some problems connecting to an AS/400 DB2 database.

I've managed to connect to MySQL, Microsoft SQL Server and Netezza databases without much trouble, but I cannot connect to this DB!

I keep getting the following error:

Java.lang.ClassNotFoundException

I've tried jt400 and db2jcc drivers, and tried the class.forName's of com.ibm.db2.jcc.DB2Driver and com.ibm.as400.access.AS400JDBCDriver, but no combination of the two have helped.

Example is:

public Connection startMyRexConnection() {
        Connection connect = null;
        ConfigReader.main();
        try {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
            connect = DriverManager.getConnection(ConfigReader.getREXurl(), ConfigReader.getREXuser(), ConfigReader.getREXpassword());
        }
        catch (ClassNotFoundException e) {
            System.err.println("Failed to load DB2 driver");
            System.err.println(e);
        } 
        catch (SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
            System.err.println("SQLState: " + ex.getSQLState());
            System.err.println("VendorError: " + ex.getErrorCode());
            System.err.println("Driver loaded, but cannot connect to db: " + ConfigReader.getREXurl());
        } 
        catch (Exception ex) {
            System.err.println("Check classpath. Cannot load db driver: " + "com.ibm.db2.jdbc.app.DB2Driver");
        }
        rexCon = connect;
        return connect;
    }

Any suggestions would be much appreciated!

Edit: This is the output from e.printStackTrace(); :

java.lang.ClassNotFoundException: com.ibm.db2.jcc.DB2Driver
java.lang.ClassNotFoundException: com.ibm.db2.jcc.DB2Driver
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1365)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1188)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.vat.ui.RexConnection.startMyRexConnection(RexConnection.java:26)
    at org.apache.jsp.loginRequestHandler_jsp._jspService(loginRequestHandler_jsp.java:143)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Adsy1944
  • 15
  • 1
  • 7
  • Use `e.printStackTrace();` when displaying exceptions, and include the full stacktrace. – Kayaman Jan 13 '20 at 10:26
  • Now added in edit section. – Adsy1944 Jan 13 '20 at 10:31
  • 1
    Well you don't have the driver in the classpath, or you misspelled the driver class name. – Kayaman Jan 13 '20 at 10:38
  • Thank you, I had set the build path on the app in Eclipse to look at my local copy of the jar, whereas I should have pointed it at the tomcat copy. Now I just need to get the connection string correct! Thanks for your help. – Adsy1944 Jan 13 '20 at 11:24

1 Answers1

0

The correct driver for IBM i is the jt400 driver, and the connection string is simple:

jdbc:as400://<host>;

You have the correct class name:

com.ibm.as400.access.AS400JDBCDriver
jmarkmurphy
  • 11,030
  • 31
  • 59
  • Thank you, I've just sat with my mentor and tried some stuff, indeed you are correct, my mistake was including the port number in the connection string (which I hadn't included in my post). Rookie mistake. – Adsy1944 Jan 13 '20 at 15:08