AS400系统与JNDI的连接
AS400 system connection with JNDI
我可以弄清楚如何使用 JNDI 资源通过 jt400 连接到 AS400:
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);}
但是,应用程序的另一部分使用了数据队列(来自同一个 jt400 库):
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);
}
}
}
DataQueues 引用的工作代码内部 server, user, pass
,这并不理想。
我想利用我已经设置的 AS400 JNDI 连接,但是我看到的每个关于将 Java 连接到 DataQueues 的示例都引用了一个与此非常相似的示例。
documentation all seem to point to AS400 系统对象,它们是对服务器名称、用户、密码等的硬编码引用
是否有更好的方法将 DataQueue()
与 JNDI 引用结合使用?
正如上面评论中所假设的那样,DataQueue is not part of the JDBC connection at all, it can't be used to configure the connection for usage to reading and writing to a DataQueue. Since this is the case, it can't also share connection methods that JDBC uses even though the jt400 library connects with JDBC. A properties file or other server-based solutions is required unless a hard-coded connection is specified in the DataQueue/Java examples online(全部 1 个)。
我可以弄清楚如何使用 JNDI 资源通过 jt400 连接到 AS400:
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);}
但是,应用程序的另一部分使用了数据队列(来自同一个 jt400 库):
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);
}
}
}
DataQueues 引用的工作代码内部 server, user, pass
,这并不理想。
我想利用我已经设置的 AS400 JNDI 连接,但是我看到的每个关于将 Java 连接到 DataQueues 的示例都引用了一个与此非常相似的示例。
documentation all seem to point to AS400 系统对象,它们是对服务器名称、用户、密码等的硬编码引用
是否有更好的方法将 DataQueue()
与 JNDI 引用结合使用?
正如上面评论中所假设的那样,DataQueue is not part of the JDBC connection at all, it can't be used to configure the connection for usage to reading and writing to a DataQueue. Since this is the case, it can't also share connection methods that JDBC uses even though the jt400 library connects with JDBC. A properties file or other server-based solutions is required unless a hard-coded connection is specified in the DataQueue/Java examples online(全部 1 个)。