2015-05-20 100 views
1
執行存儲過程

我在Oracle數據庫的存儲過程是這樣的:在連接Oracle數據庫的Java

CREATE OR REPLACE PROCEDURE PSTATISTIC 
AS 
BEGIN 
    UPDATE PLACE_STATISTIC 
    SET POPULARITY = 0; 

    UPDATE PLACE_STATISTIC 
    SET POPULARITY = POPULARITY + 1 
    WHERE PLACE_ID IN (SELECT PLACE_COMMENT.PLACE_ID 
        FROM PLACE_COMMENT); 

END PSTATISTIC; 

當我把它稱爲對SQL開發人員:

EXECUTE PSTATISTIC

它執行通常,PLACE_STATISTIC表已更新

但是,當我試圖在Java上使用它時:

String sql="EXECUTE HR.PSTATISTIC"; 
Statement statement=(Statement)connectionDB.createStatement(); 
statement.execute(sql); 

它沒有Java的工作了,理由是這類錯誤的:

java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement 

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439) 
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395) 
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802) 
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436) 
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186) 
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521) 
    at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:194) 
    at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:1000) 
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1307) 
    at oracle.jdbc.driver.OracleStatement.executeInternal(OracleStatement.java:1882) 
    at oracle.jdbc.driver.OracleStatement.execute(OracleStatement.java:1847) 
    at oracle.jdbc.driver.OracleStatementWrapper.execute(OracleStatementWrapper.java:301) 

我如何可以執行Java的我PSTATISTIC程序?我授予的所有必要的權限

+0

請參閱http://www.coderanch.com/t/568116/JDBC/databases/Oracle-procedure-works-direct-SQL –

回答

1

您需要使用的CallableStatement用於執行存儲過程

String procName= "{call PSTATISTIC}"; 
CallableStatement cs = conn.prepareCall(procName); 
cs.executeQuery(); 
1

從Java代碼執行存儲過程,你需要使用CallableStatement的。聲明不能用於執行存儲過程。

Connection con = getConnection(); 
    CallableStatement cs = null; 
    try { 
     cs = con.prepareCall("{call EXECUTE PSTATISTIC}"); 
     cs.execute(); 


    } catch (SQLException e) { 
     System.err.println("SQLException: " + e.getMessage()); 
    } 
    finally { 
     if (cs != null) { 
      try { 
       cs.close(); 
      } catch (SQLException e) { 
       System.err.println("SQLException: " + e.getMessage()); 
      } 
     } 
     if (con != null) { 
      try { 
       con.close(); 
      } catch (SQLException e) { 
       System.err.println("SQLException: " + e.getMessage()); 
      } 
     } 
    } 
}