1

I can figure out how to connect to an AS400 through jt400 with JNDI resources just fine:

    Connection conn = null;
    Statement stmt = null;
    try {
        Context ctx = (Context) new InitialContext().lookup("java:comp/env");
        conn = ((DataSource) ctx.lookup("jdbc/AS400")).getConnection();
        System.out.println(conn.getClientInfo());

        stmt = conn.createStatement();
        //SQL data fetch using the connection
        ResultSet rs = stmt.executeQuery("SELECT * FROM LIBRARY.TABLE");
        while (rs.next()) {
            System.out.println(rs.getString("COLUMN1"));
        }
        conn.close();
        conn = null;
    }
    catch(Exception e){System.out.println(e);}

However, another part of the application utilizes DataQueues (from the same jt400 library):

    String queue = "/QSYS.LIB/" + libraryName +".LIB/" + queueName +".DTAQ";

    try{
        system = new AS400(server, user, pass);
        DataQueue dq = new DataQueue(system, queue);

        // Convert the Data Strings to IBM format
        byte[] byteData = message.getBytes("IBM285");

        dq.write(byteData);
        System.out.println("Wrote to DataQueue");

    }catch(Exception e){
        e.printStackTrace();
        System.err.println(e);
    }finally{
        // Make sure to disconnect
        if(system != null){
            try{
                system.disconnectAllServices();
                System.out.println("Disconnected from DataQueue.");
            }catch(Exception e){
                System.err.println(e);
            }
        }
    }

Inside of this working code for DataQueues references server, user, pass, which isn't ideal.

I'd like to utilize the AS400 JNDI connection I already set up, but every example I see about connecting Java to DataQueues references an example much like this one.

The documentation all seem to point to AS400 system objects which are hard-coded references to servername, user, pass, etc.

Is there better way to utilize DataQueue() with a JNDI reference?

4

1 回答 1

0

正如上面评论中所假设的,DataQueue根本不是 JDBC 连接的一部分,它不能用于配置连接以用于读取和写入 DataQueue。由于是这种情况,即使 jt400 库与 JDBC 连接,它也不能共享 JDBC 使用的连接方法。除非在 DataQueue/Java在线示例(全部 1 个)中指定了硬编码连接,否则需要属性文件或其他基于服务器的解决方案。

于 2017-01-25T22:14:17.520 回答