从java中调用oracle存储函数不带参数

时间:2016-05-02 10:55:20

标签: java oracle

我在oracle中有以下功能。

CREATE OR REPLACE function CUSTOMERAPP.procOneParameter
return  boolean
IS
BEGIN

  return true;

END;

现在我想从这个函数中获取返回值。为此,我有以下代码。

public class CallFunctionFromOracle {

   public static void main(String... arg) throws SQLException {
      Connection con = null;
      CallableStatement callableStmt = null;
      try {
           // registering Oracle driver class
           Class.forName("oracle.jdbc.driver.OracleDriver");

           // getting connection
           con = DriverManager.getConnection(
                    "jdbc:oracle:thin:@10.11.201.166:1521:xe", "customerapp", "customerapp");

           System.out.println("Connection established successfully!");


           callableStmt = con.prepareCall("{ ? = call procOneParameter()}");

           //Database function will return value as OUT parameter
           callableStmt.registerOutParameter(1, java.sql.Types.NUMERIC);

           //IN parameter -
           //set methods are used for setting IN parameter values of Stored procedure
//           callableStmt.setInt(2, 11);

           //Execute database Function,
           callableStmt.execute();

           // Then retrieve values returned by method using using get methods.
           System.out.println("salary = " + callableStmt.getBoolean(1));

           System.out.println("Function executed successfully, "
                    + "salary has been fetched from Employee table");

      } catch (ClassNotFoundException e) {
           e.printStackTrace();
      } catch (SQLException e) {
           e.printStackTrace();
      }
      finally{
           try {
               if(callableStmt!=null) callableStmt.close(); //close CallableStatement
               if(con!=null) con.close(); // close connection
           } catch (SQLException e) {
               e.printStackTrace();
           }
      }
  }

}

但运行此代码会给我以下错误。

java.sql.SQLException: ORA-06550: line 1, column 13:
PLS-00382: expression is of wrong type
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:837)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:445)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:523)
    at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:204)
    at oracle.jdbc.driver.T4CCallableStatement.executeForRows(T4CCallableStatement.java:1007)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1315)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3576)
    at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3677)
    at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:4714)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1374)
    at callfunctionfromoracle.CallFunctionFromOracle.main(CallFunctionFromOracle.java:44)

如何解决此错误?

我的问题不是possible duplicate of this questions。这里我说的是“PLS-00382:表达式类型错误”错误,而在该问题中用户谈到“无效列类型错误”错误。

所以我的问题与那个问题不同。

1 个答案:

答案 0 :(得分:-1)

将prepareCall中的plsql块更改为:

callableStmt = con.prepareCall("begin ? := procOneParameter(); end;");