簡體   English   中英

如何使用Weblogic數據源連接調用存儲過程?

[英]How to call stored procedure using Weblogic data source connection?

我有使用weblogic數據源連接調用已編譯的sql存儲過程的請求嗎?

有什么解決辦法嗎?

以下oracle文檔應為您指明正確的方向:

配置和管理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();
                    }

        }
  }

這解決了我的問題。

我希望它也會對其他人有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM