Blog Archive

Java JDBC connection to DB2 AS400 database example

To make a connection to DB2 on AS400 using JDBC we need the following values

  • Host Name or IP Address of The Machine
  • User Id
  • Password

Instead of putting the values right into the program its better to soft code them using a properties file.

Sample properties file for JDBC connection mydb2.properties

#I-series ip or host name
local_system=XXX.XXX.XXX.XXX

#I-series UserId, used for login and library list
userId=XXXXXXX 

#I-series Password
password=XXXXXXX

Sample java program for JDBC connection

static Properties props;

try {

 props = new Properties();
 props.load(new FileInputStream("properties/mydb2.properties"));

 String DRIVER = "com.ibm.as400.access.AS400JDBCDriver"; 
 String URL = "jdbc:as400://" + props.getProperty("local_system").trim() + ";naming=system;errors=full";
 Connection conn = null;

 //Connect to iSeries 
 Class.forName(DRIVER); 
 conn = DriverManager.getConnection(URL, props.getProperty("userId").trim(), props.getProperty("password").trim()); 

 conn.close();

}
catch (Exception e) {
 System.out.println(e);
}

You need the jt400.jar in your classpath for the project. You can extract a copy from the iSeries IFS directory.
/QIBM/ProdData/HTTP/Public/jt400/lib/jt400.jar


No comments :

Post a Comment