JTOpen is a free tool provided by IBM to communicate with the AS400. Here I will show you how to connect and query using a native JDBC connection.
In order for this to work, you need to things: 1: The Java Toolbox must be setup on the AS400. 2: You must have the JTOpen package imported into your Java Project.
It is really just like any other JDBC connection, just with a different driver. Of course you will have to modify the sql query in order for this to work with your data.
Please read the comments in the code to understand what is going on.
Code:
package com.mycompany.data;
import java.sql.*;
import com.ibm.as400.access.*;
public class testAS400JDBC
{
public testAS400JDBC(){
}
public static void main(String[] args){
// define login info for as400
String host = "1.2.3.4";
String user = "USERNAME";
String pwd = "PASSWORD";
try{
// make sure driver exists
Class.forName("com.ibm.as400.access.AS400JDBCDriver");
}catch(Exception e){
System.out.println(e.toString());
}
try{
// create a new connection from driver
Connection con = DriverManager.getConnection("jdbc:as400://" + host, user, pwd);
// create new statement from connection
java.sql.Statement stmt = con.createStatement();
// sql
String sql="SELECT FNAME,LNAME FROM LIBRARY.FILE FETCH FIRST 10 ROWS ONLY";
// execute query
ResultSet rs = stmt.executeQuery(sql);
// loop through results
while(rs.next()){
System.out.println(rs.getString(1) + " " + rs.getString(2));
}
// close connection
con.close();
}catch(Exception e){
System.out.println(e.toString());
}
}
}