0

On Window 10 OS, I am attempting to complete a sample java code that I got online, below is the code. I am not using fancy IDE, just plain old notepad.

import java.sql.*;                               //1 

public class EzJavaAS400 { 
    public static void main(String[] args)  {  
        String urlPrefix = "jdbc:as400:";  
        String url;  
        String user;  
        String password;
        String item_TabName;
        String item_SrvName; //2 
          
        Connection con;  
        Statement stmt;  
        ResultSet rs;    
        System.out.println("**** Enter class EzJavaAS400");     // Check the that first argument has the correct form for the portion
           // of the URL that follows jdbc:db2:,
           // as described
           // in the Connecting to a data source using the DriverManager 
           // interface with the IBM Data Server Driver for JDBC and SQLJ topic.
           // For example, for IBM Data Server Driver for 
           // JDBC and SQLJ type 2 connectivity, 
           // args[0] might be MVS1DB2M. For 
           // type 4 connectivity, args[0] might
           // be //stlmvs1:10110/MVS1DB2M.
        // https://www.ibm.com/docs/en/db2-for-zos/11?topic=samples-example-simple-jdbc-application
        // pass these variables => java EzJavaAS400 //172.24.92.70:50000/WCSK01 gpatil Passw0rd@12
        // "C:\Program Files\IBM\SQLLIB\java\jdk\bin\javac" EzJavaAS400.java


          
        if (args.length != 3)   {   
            System.err.println("Invalid value. First argument appended to " +     "jdbc:db2: must specify a valid URL.");   
            System.err.println("Second argument must be a valid user ID.");   
            System.err.println("Third argument must be the password for the user ID.");   
            System.exit(1);  
        }  
        url = urlPrefix + args[0];  
        user = args[1];  
        password = args[2];  
        try   {                                        // Load the driver
                //Class.forName("com.ibm.db2.jcc.DB2Driver");                             //3a 
             
            Class.forName("com.ibm.as400.access.AS400JDBCDriver");               //3a 

               
            System.out.println("**** Loaded the JDBC driver");

                // Create the connection using the IBM Data Server Driver for JDBC and SQLJ
               
            con = DriverManager.getConnection(url, user, password);         //3b 
                // Commit changes manually
               
            con.setAutoCommit(false);   
            System.out.println("**** Created a JDBC connection to the data source");

                // Create the Statement
               
            stmt = con.createStatement();                      //4a 
               
            System.out.println("**** Created JDBC Statement object");

                // Execute a query and generate a ResultSet instance
               
            rs = stmt.executeQuery("select table_schema, table_name from qsys2.tables fetch first 5 rows only with ur");          //4b 
               
            System.out.println("**** Created JDBC ResultSet object");   
            System.out.println("");

                // Print sample table names to standard output device
               
            while (rs.next()) {    
                item_TabName = rs.getString(1);
                item_SrvName = rs.getString(2);    
                System.out.println("Test Record ... " + item_SrvName + " => " + item_TabName);   
            }  
            System.out.println("");   
            System.out.println("**** Fetched all rows from JDBC ResultSet");    // Close the ResultSet
               
            rs.close();   
            System.out.println("**** Closed JDBC ResultSet");       // Close the Statement
               
            stmt.close();   
            System.out.println("**** Closed JDBC Statement");

                // Connection must be on a unit-of-work boundary to allow close
               
            con.commit();   
            System.out.println("**** Transaction committed");       // Close the connection
               
            con.close();                               //6 
               
            System.out.println("**** Disconnected from data source");

               
            System.out.println("**** JDBC Exit from class EzJavaAS400 - no errors");

              
        }    
        catch (ClassNotFoundException e)   {   
            System.err.println("Could not load JDBC driver");   
            System.out.println("Exception: " + e);   
            e.printStackTrace();  
        }

          
        catch (SQLException ex)                           //5  
           {   
            System.err.println("SQLException information");   
            while (ex != null) {    
                System.err.println("Error msg: " + ex.getMessage());    
                System.err.println("SQLSTATE: " + ex.getSQLState());    
                System.err.println("Error code: " + ex.getErrorCode());    
                ex.printStackTrace();    
                ex = ex.getNextException(); // For drivers that support chained exceptions
                   
            }  
        } 
    }  // End main
}   // End EzJavaAS400

This is how I am calling/invoking it...

java EzJavaAS400 //DEV400/DEV400 user01 Passw0rd@12

But I get this error message ...

Could not load JDBC driver
Exception: java.lang.ClassNotFoundException: com.ibm.as400.access.AS400JDBCDriver
java.lang.ClassNotFoundException: com.ibm.as400.access.AS400JDBCDriver
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at EzJavaAS400.main(EzJavaAS400.java:49)

To resolve the issue, first I appended the CLASSPATH variable to include the path where "jt400.jar" is, error did not disappear. Then I thought path is too long so it might be an issue.

So I copied the "jt400.jar" to the same directory as my java file.

Error still did not disappear - wondering what am I missing. The SQL Client tool that uses this "jt400.jar" and that works just fine.

  • You need to put the library on the classpath of your application. How you do that, depends on how your application is started. For your command, the `CLASSPATH` environment variable should work, or adding -cp .;\jt400.jar (i.e. `java .;\jt400.jar EzJavaAS400 //DEV400/DEV400 user01 Passw0rd@12`). As an aside, assuming the driver is JDBC 4.0 or higher, you don't need to load the driver with `Class.forName`. – Mark Rotteveel May 18 '22 at 07:19
  • @gvphubli.blogspot.com - Did Mark Rotteveel's comment or answers to the [linked question](https://stackoverflow.com/questions/17408769/how-do-i-resolve-classnotfoundexception) work? – John Y May 18 '22 at 15:08
  • @Mark Rotteveel - that is the first thing I tried, first by setting CLASSPATH environment variable and also using -cp, either of them did not resolve the issue. I did not understand this "you don't need to load the driver with Class.forName" - how do I load, My code is show above can you modify the necessary line for ease of explanation? – gvphubli.blogspot.com May 19 '22 at 01:15
  • @MarkRotteveel - Do you have specific familiarity with jt400.jar or JTOpen? I understand that any time an experienced Java practitioner on a mainstream platform sees a low-rep user with a question containing the string `ClassNotFoundException`, they simply dismiss it as "some clueless newbie again; time to point them to the FAQ". But anything involving the IBM midrange platform (AS/400, iSeries, IBM i) is a special beast. It requires special consideration, and assumptions you may have from your mainstream experience may not apply. – John Y May 19 '22 at 02:05
  • @gvphubli.blogspot.com - First, make sure you have at least looked at the linked question. Something in the answers there might apply to your situation after all. But if nothing there helps you, try checking out the "Related" questions in the right-hand sidebar. Some of them deal specifically with jt400. – John Y May 19 '22 at 02:37
  • @JohnY No, I don't have special experience with them, but this is a fundamental Java problem, and has nothing specifically to do with jt400.jar. – Mark Rotteveel May 19 '22 at 11:13
  • @MarkRotteveel- you are very much right, I was also getting frustrated with that treatment. Any link I follow all were just telling how to set ClassPath. I tried multiple versions of jt400.jar files finally I found the version (from another product package) that is working fine - issue is resolved. – gvphubli.blogspot.com May 20 '22 at 07:20
  • @MarkRotteveel - Judging by OP's last comment, I would say the issue has (well, *had*) plenty to do specifically with jt400.jar. If it were "merely" a classpath issue, OP would have either not even posted this question in the first place, or would have stopped after your first comment. – John Y May 20 '22 at 18:33
  • @gvphubli.blogspot.com - For my own curiosity, which version of jt400.jar eventually wound up working for you? Where was it? – John Y May 20 '22 at 18:37
  • "_issue is resolved_" - On Stack Overflow, you are welcome to [answer your own question](https://stackoverflow.com/help/self-answer). You will potentially be helping future visitors, that way. You will also have the space to provide more of an explanation, along with the solution. – andrewJames May 26 '22 at 20:38

0 Answers0