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.