从PostgreSQL调用存储过程

时间:2015-10-08 09:21:05

标签: java postgresql stored-procedures jdbc

我正在尝试调用存储过程。 存储过程通过数据库中的测试证明并执行

CREATE OR REPLACE FUNCTION statapi.pui_src_ter(p_src_ter_id id_t, p_aname name_t, p_adesc desc_t, p_sortord sortord_t)
  RETURNS integer AS
$BODY$
DECLARE
    afected_rows integer := 0;
BEGIN
update statdwh.src_ter
   set  aname = p_aname,
    adesc = p_adesc,
    sortord = p_sortord,
    actdt = current_timestamp
 where src_ter_id = p_src_ter_id;
get diagnostics afected_rows = row_count;
if (afected_rows = 0) then
    insert into statdwh.src_ter(src_ter_id, aname, adesc, sortord, actdt)
        values (p_src_ter_id, p_aname, p_adesc, p_sortord, current_timestamp)
        returning src_ter_id into p_src_ter_id;
    get diagnostics afected_rows = row_count;
end if;

return afected_rows;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION statapi.pui_src_ter(id_t, name_t, desc_t, sortord_t)
  OWNER TO postgres;

呼叫本身

public void storedProcedureCall() throws ClassNotFoundException, SQLException {
    CallableStatement cst = connection.prepareCall("{call statapi.pui_src_ter(?,?,?,?)}");
    cst.setInt(1, 11);
    cst.setString(2, "aname_att");
    cst.setString(3, "adesc_att");
    cst.setInt(4, 222);
    ResultSet result3 = cst.executeQuery();
    while (result3.next()){
        System.out.println(result3.getString(1));
    }
    cst.execute();
}

返回" 1"正如预期的那样,自修改了一条记录,但在数据库中(我通过pgAdmin看)记录没有出现  我做错了什么?

1 个答案:

答案 0 :(得分:0)

高级研究员回答简单地说#34;添加connection.commit();"