从java调用存储过程时出错

时间:2012-12-27 03:30:32

标签: java postgresql jdbc prepared-statement

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

我无法找出原因,请帮忙!

3 个答案:

答案 0 :(得分:2)

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

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

记下可调用语句中的花括号。

答案 1 :(得分:1)

使用这个

 call reviseTax(?,?)

答案 2 :(得分: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 $$

声明

P.S。而不是realdouble precision也适用