2

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)
          {  

          }  

          }  
}
Sagar Lad
  • 41
  • 3
  • possible duplicate of [classpath - running a java program from the command line](http://stackoverflow.com/questions/11462421/classpath-running-a-java-program-from-the-command-line) –  Dec 03 '14 at 06:35
  • Use Eclipse for good. – zawhtut Dec 03 '14 at 06:37

6 Answers6

1

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.

Buck Calabro
  • 7,558
  • 22
  • 25
0

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

SET CLASSPATH=%DIR%\lib\yourlib.jar
janasainik
  • 811
  • 5
  • 20
  • 40
0

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

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

Harish N
  • 19
  • 1
0

try

java -cp pathtojarlib:. JdbcExample

where pathtojarlib is the path of your connector library

user3308224
  • 423
  • 2
  • 5
  • 16
0

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

Rohit Pavaskar
  • 308
  • 2
  • 12
0

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
Andrés Oviedo
  • 1,388
  • 1
  • 13
  • 28