Sagar Lad
Sagar Lad

Reputation: 41

Runtime Error "Class Not found" while writing java program to connect DB2 and fetch data

The below code is compiled successfully.

Code Source(Jdbcexample.java) and compiled class file (JdbcExample.class) directory:-"test" When I ran this program using java JdbcExample, it throws class not found com.ibm.as400.access.AS400JDBCDriver and in job log:

code ended with 04:Unable to find class required to run Java Program".

The problem is related to class path I suppose.

can anyone please guide me how should set path/classpath and run program to avoid above error?

import java.sql.*;  

public class JDBCexample {  
  public static void main(String[] args)
  {
    Connection con = null;  
    try {  
          Class.forName("com.ibm.as400.access.AS400JDBCDriver);  
        }
    catch(ClassNotFoundException e)
        {  
          System.out.println(e);  
          System.exit(0);  
        }  
    try {  
          con = DriverManager.getConnection("jdbc:as400://yourserver", "yourUserId","yourPassword");  

          Statement stmt = con.createStatement();  
          ResultSet rs = stmt.executeQuery("SELECT * FROM YOURLIB.YOUR_PF_FILE");  

          while (rs.next())
         {  

                  String field1 = rs.getString(1);  
                  String field2 = rs.getString("fieldname");  


          }
           rs.close();
           stmt.close();
           con.close();
       }
          catch(Exception e)
          {  

          }  

          }  
}

Upvotes: 2

Views: 1064

Answers (6)

Andrés Oviedo
Andrés Oviedo

Reputation: 1428

Add classpath to your jar's MANIFEST.MF. Example:

Manifest-Version: 1.0
Main-Class: JdbcExample
Class-Path: lib/db2jcc.jar lib/commons-logging-1.1.3.jar

Upvotes: 0

Buck Calabro
Buck Calabro

Reputation: 7648

The error says it cannot find the JDBC driver. That driver is part of the IBM Toolkit for Java. In my case, I am using JTOpen instead of the version that ships with the machine. I put jt400.jar in the IFS in a directory called java.

If running from IBM i (not PASE or QShell), you set your classpath with ADDENVVAR. This works for me because I put my .jar files in /java:

ADDENVVAR ENVVAR(CLASSPATH) VALUE('.:+       
                                  /java:+   
                                  /java/*') 

The jt400 that is shipped with the machine is in the IFS. On my 7.2 machine, the path is: /QIBM/ProdData/HTTP/Public/jt400/lib - if you want to use that version, put that path in your CLASSPATH. IBM maintain a FAQ on the Toolbox.

Upvotes: 1

Rohit Pavaskar
Rohit Pavaskar

Reputation: 308

I thought you have done some silly mistake. Please check your class name. IN the above given code the class name is JDBCexample and you use java JdbcExample to run .. Use java JDBCexample

Upvotes: 0

user3308224
user3308224

Reputation: 423

try

java -cp pathtojarlib:. JdbcExample

where pathtojarlib is the path of your connector library

Upvotes: 0

Harish N
Harish N

Reputation: 19

Class.forName("com.ibm.as400.access.AS400JDBCDriver);

have you added the relevant jar for AS400JDBCDriver in your project.check it once

Upvotes: 0

janasainik
janasainik

Reputation: 831

Set your classpath with the below command and execute your java program.

SET CLASSPATH=%DIR%\lib\yourlib.jar

Upvotes: 0

Related Questions