How to read VSAM files from z/OS using Java?

1.2k Views Asked by At

I am trying to connect to VSAM files in z/OS in Java. I have tried using VSE Redirector Connector with below code:

package test;

import java.sql.DriverManager;
import java.sql.ResultSetMetaData;

public class TestVSAM {

  public static void main(String[] args) {

    String vsamCatalog = "VSESP.USER.CATALOG";
    String flightsCluster = "USERNAME.TEST.KSDS1";
    String flightsMapName = "FLIGHTS_MAP";
    
    String ordersCluster = "FLIGHT.ORDERING.ORDERS";
    String ordersMapName = "ORDERS_MAP";

    String ipAddr = "XXX.XXX.XXX.XXX";
    String userID = "XXXXXX";
    String password = "XXXXXX";
    Integer port = 123;

    try {
      System.out.println("VSE IP address------------->" + ipAddr);

      System.out.println("Your VSE user ID----------->" + userID);

      System.out.println("Password------------->" + password);

      System.out.println("Port------------->" + port);

      java.sql.Connection jdbcCon;
      java.sql.Driver jdbcDriver =
          (java.sql.Driver) Class.forName("com.ibm.vse.jdbc.VsamJdbcDriver").newInstance();

      // Build the URL to use to connect
      String url = "jdbc:vsam:" + ipAddr;

      // Assign properties for the driver
      java.util.Properties prop = new java.util.Properties();
      prop.put("port", port);
      prop.put("user", userID);
      prop.put("password", password);

      // Connect to the driver
      jdbcCon = DriverManager.getConnection(url, prop);

      // Get a statement
      java.sql.Statement stmt = jdbcCon.createStatement();

      // Execute the query ...
      java.sql.ResultSet rs = stmt.executeQuery(
          "SELECT * FROM " + vsamCatalog + "\\" + flightsCluster + "\\" + flightsMapName);

      ResultSetMetaData rsmd = rs.getMetaData();
      int columnCount = rsmd.getColumnCount();

      for (int i = 1; i <= columnCount; i++) {
        System.out.println("Key------------>" + rsmd.getColumnLabel(i));

      }
      rs.close();
      stmt.close();

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

I'm getting this error:

Connection Refused Error

My Questions:

  1. Can we use VSE Redirector Connector to read VSAM files? If yes, then how can we resolve the above error and get connected?
  2. Is there any other method to access VSAM files thru Java?
1

There are 1 best solutions below

2
user17320681 On