繁体   English   中英

不带参数从Java调用oracle存储函数

[英]call oracle stored function from java taking no parameter

我在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)

我该如何解决这个错误?

我的问题不可能重复出现这些问题 在这里,我谈论的是“ PLS-00382:表达式的类型错误”错误,而在该问题中,用户谈论的是“无效的列类型错误” error。

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

将prepareCall中的plsql块更改为:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM