从Java调用Postgres存储过程,表返回

时间:2018-10-04 22:02:05

标签: java postgresql stored-procedures jdbc java-stored-procedures

我做了一个Postgres存储过程:

df <- read.table(text =
    "Player            Start.Date        Inns_Bat    Ground     match
'NFD Thomson (AUS)' 2018-08-09        1           'Lord\\'s'     1
'NFD Thomson (AUS)' 2014-12-12        3           'Lord\\'s'     23
'NFD Thomson (AUS)' 2018-08-09        1           'Eden G'     97
'SM Curran (ENG)'   2018-08-09        4           'Lord\\'s'     NA", header = T)

我试图像这样在Java中使用它:

CREATE OR REPLACE FUNCTION GetUser(ipUserId integer)
RETURNS setof users AS $$
BEGIN
  IF ipUserId is null THEN
    return query select * from users A order by modifieddate desc;
  END IF;
  return query select * from users where iduser = ipUserId;
END;
$$ LANGUAGE plpgsql;

    StoredProcedureQuery query = entityManager.createStoredProcedureQuery("GetUser").
            registerStoredProcedureParameter("ipUserId",
                    Integer.class, ParameterMode.IN)
            .registerStoredProcedureParameter("users",
                    List.class, ParameterMode.OUT)
            .setParameter("postId", 1);

我想将结果存储在列表中。

由于遇到各种错误,我该怎么办?

更新:

这些是错误:

 StoredProcedureQuery query = entityManager.createStoredProcedureQuery("GetUser")
                .registerStoredProcedureParameter(1,void.class, ParameterMode.REF_CURSOR)
                .registerStoredProcedureParameter(2,Integer.class, ParameterMode.IN)
                .setParameter(2, ipIdUser);

3 个答案:

答案 0 :(得分:1)

您可以尝试使用CallableStatement。 假设您的Connection变量没问题:

#!/usr/bin/python
import psutil
import sys
from subprocess import Popen

for process in psutil.process_iter():
    if process.cmdline() == ['python', 'update.py']:
         sys.exit('Process found: exiting.')

print('Process not found: starting it.')
Popen(['python', 'update.py'])

要获得结果:CallableStatement stmt = con.prepareCall("{call SCHEMA.PROCEDURE_NAME (?, ?)}"); stmt.setInt(1, custom_var); stmt.registerOutParameter(2, OracleTypes.INTEGER); stmt.execute();

如果无法成功,请尝试使用JdbcTemplate:

stmt.getInt(3); stmt.getString(4)

要获取单个结果:SimpleJdbcCall call = new SimpleJdbcCall(this.jdbcTemplate).withSchemaName(SCHEMA).withProcedureName(PROC); MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue("ipUserId", custom_var); Map out = call.execute(params);

或者您可以将所有结果保存在列表中以供日后使用:

Integer.parseInt("" + out.get("OUT_PARAM_NAME")); (String) out.get("OUT_PARAM_NAME2"));

答案 1 :(得分:1)

我找到了一个非常简单的解决方案,只需执行一个SQL查询以使用hibernate调用该过程即可。

    String SqlString = "select * from GetUser({0})";

    if (ipIdUser == null )
        SqlString = MessageFormat.format(SqlString, "NULL");
    else
        SqlString = MessageFormat.format(SqlString, ipIdUser);

    LOGGER.info("SqlSting =" + SqlString);

    return entityManager.createNativeQuery(SqlString, User.class)
            .getResultList();

答案 2 :(得分:0)

为什么不对getResultList使用StoredProcedureQuery?这样可以避免进行字符串操作。

List<User> users = entityManager.createStoredProcedureQuery("GetUser")
  .registerStoredProcedureParameter("ipUserId", Integer.class, ParameterMode.IN)
  .setParameter("ipUserId", ipIdUser)
  .getResultList();