从java调用hsql函数

时间:2016-01-27 08:39:42

标签: java hsqldb

我正在尝试使用hsqldb从java调用oracle函数进行集成测试。

功能:(function.sql)

FUNCTION balance (acct_id NUMBER) RETURN NUMBER IS
acct_bal NUMBER;
BEGIN
  SELECT bal INTO acct_bal FROM accts WHERE acct_no = acct_id;
  RETURN acct_bal;
END;

Java代码:

    int acctNo = 0;
    CallableStatement cstmt = connection.prepareCall("{? = CALL balance(?)}");
    cstmt.registerOutParameter(1, Types.FLOAT);
    cstmt.setInt(2, acctNo);
    cstmt.executeUpdate();
    float acctBal = cstmt.getFloat(1);
    System.out.print("Test print: " + acctNo +  " " +acctBal);

错误:

java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: BALANCE
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCCallableStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.prepareCall(Unknown Source)

请在这方面帮助我。 注意:创建表和插入数据工作正常。

1 个答案:

答案 0 :(得分:2)

然后必须将该函数更改为HSQLDB语法并在表之后创建它。修改后的函数如下所示:

CREATE FUNCTION balance (acct_id INTEGER) RETURNS DOUBLE
BEGIN ATOMIC
  DECLARE acct_bal DOUBLE;
  SELECT bal INTO acct_bal FROM accts WHERE acct_no = acct_id;
  RETURN acct_bal;
END;