在Oracle 10g中创建下面的存储函数balance:
CREATE OR REPLACE 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;
/
然后创建CalculateBalance类来调用存储函数balance:
/*
* @(#) CalculateBalance.java
*/
import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.*;
public class CalculateBalance {
public static void main(String[] args) throws SQLException {
OracleDataSource ods = new OracleDataSource();
String url = "jdbc:oracle:thin:@taurus:1521/oracle.mybizinfo.com";
ods.setURL(url);
ods.setUser("scott");
ods.setPassword("tiger");
Connection conn = ods.getConnection();
try {
CallableStatement cstmt = conn.prepareCall(
"{ ? = CALL balance(?) }");
cstmt.registerOutParameter(1, Types.FLOAT);
cstmt.setInt(2, 115);
cstmt.executeUpdate();
float acctBal = cstmt.getFloat(1);
System.out.println("acct_id = 115, bal = " + acctBal);
cstmt.close();
conn.close();
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
System.exit(0);
}
}
运行结果为:出现格式不正确的 SQL92 串: 6. Expecting "call"
我不清楚是什么地方出错误了?该怎么改正?
附:表accts
ACCT_NO BAL
---------- ----------
115 10000
116 20000
129 15000