2

我在postgres数据库中编写了以下存储过程

create or replace function reviseTax(revisiondate date, taxrate decimal) returns         table(employeeid integer, month date, difference decimal) AS $$
declare
    thisrow record;
    newTaxAmt decimal;
    differenceAmt decimal;
begin 
for thisrow in select * from employee_salary where salarydate >= revisiondate loop
    newTaxAmt = thisrow.income * $2;
    differenceAmt = newTaxAmt - thisrow.tax;
    update employee_salary es set tax = newTaxAmt where es.employeeid = thisrow.employeeid;
    insert into taxrevision (employeeid , month , difference) values (thisrow.employeeid , thisrow.salarydate , differenceAmt);

end loop;
return query select * from taxrevision as something;
end $$ language plpgsql;

从数据库调用时,这工作正常。问题是当我使用 JDBC 代码从 java 调用这个存储过程时

我的java代码如下

System.out.println("Enter the effective date(yyyy-mm-dd) for the tax revision");
dateString = scanner.next();
float newTaxRate = 0.3F;

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(false);
java.util.Date utilDate = df.parse(dateString);
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

CallableStatement callable = DBConnection.prepareCall("{call revisetax(?,?)}");
callable.setDate(1,sqlDate);
callable.setFloat(2,newTaxRate);
callable.executeQuery();
DBConnection.commit();

我收到以下错误:

ERROR: function revisetax(unknown, real) does not exist
  Hint: No function matches the given name and argument types. You might need to add             explicit type casts.
  Position: 14

我无法弄清楚为什么,请帮助!

4

3 回答 3

2

当您在数据库中调用存储过程/函数时要使用该CallableStatement对象。

CallableStatement callable = new CallableStatement("{call my_function(?)}");

请注意可调用语句中的花括号。

于 2012-12-27T03:35:02.960 回答
1

用这个

 call reviseTax(?,?)
于 2012-12-28T04:39:30.213 回答
0

我得到了它!!问题在于存储过程标头中的十进制数据类型

create or replace function reviseTax(revisiondate date, taxrate decimal) returns         table(employeeid integer, month date, difference decimal) AS $$

宣布

而不是decimal我使用real它并且它有效!

所以函数头应该是

create or replace function reviseTax(revisiondate date, taxrate real) returns         table(employeeid integer, month date, difference decimal) AS $$

宣布

PS 代替realdouble precision也可以

于 2012-12-27T05:20:36.753 回答