如何使用 Web 逻辑数据源连接调用存储过程



我有一个请求,要求使用 weblogic 数据源连接调用编译的 sql 存储过程?

有什么解决方案吗?

以下预言机文档应为您指明正确的方向:

配置和管理 WebLogic JDBC

我假设您使用的是Oracle而不是SQL Server。

感谢您的回复,我已经找到了解决方案。

我们可以使用以下代码:

private void callStoreProcedure() {
        Context ctx = null;
        Connection conn = null;
        ResultSet rs = null;
        Hashtable ht = new Hashtable();
        ht.put(Context.INITIAL_CONTEXT_FACTORY,   "weblogic.jndi.WLInitialContextFactory");
        ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
        try {
              ctx = new InitialContext(ht);
              DataSource ds = (DataSource) ctx.lookup("wl_datasouce");
              conn = ds.getConnection();
              CallableStatement cstmt = conn.prepareCall("{call procedure(?)}");
              cstmt.registerOutParameter(1, OracleTypes.CURSOR); 
              cstmt.executeUpdate();
              rs = (ResultSet)cstmt.getObject(1);
              // print the results
              while (rs.next()) {
                  System.out.println(rs.getInt(1) + "t" +
                      rs.getString(2) + "t" +
                      rs.getString(3));
              }

        } catch (Exception e) {
              // a failure occurred log message;
              e.printStackTrace();
              }finally {
                    //cstmt.close();
                    try {
                          conn.close();
                    } catch (SQLException e1) {
                          // TODO Auto-generated catch block
                          e1.printStackTrace();
                    }
                    conn = null;
                    try {
                          rs.close();
                    } catch (SQLException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                    }
        }
  }

这解决了我的问题。

我希望它也能帮助其他人。

相关内容

最新更新