2013-10-07 103 views

回答

0

感謝您的回覆,我找到了解決方案。

我們可以使用下面的代碼:

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(); 
        } 

     } 
    } 

這解決了我的問題。

我希望它也能幫助別人。

相關問題