2015-05-20 71 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()); 
      } 
     } 
    } 
}