我在postgres数据库中做了以下用户定义的类型:
create type var_data as
(
var1 varchar,
var2 varchar,
var3 character varying[]
);
我想在postgres中调用一个名为get_var
的函数,该函数将返回该udt。我想获取存储在返回的udt中的信息。我编写了以下Java代码来连接到db,然后解析结果。
public Var getVar(int number) throws SQLException
{
try (Connection connection = dataSource().getConnection()
) {
connection.setAutoCommit(false);
CallableStatement callableStatement = connection.prepareCall("{? = call get_var(?) }");
//callableStatement.registerOutParameter(1, Types.OTHER); <-- This didn't work
//The following was what I saw online
callableStatement.registerOutParameter(1, Types.STRUCT, "var_data");
callableStatement.setInt(2, number);
logger.info("About to execute getVar");
callableStatement.execute();
logger.info("Got data back from the db, about to parse it");
ResultSet rs = (ResultSet) callableStatement.getObject(1);
Var var = null;
while (rs.next()){
String var1= rs.getString("var1");
String var2= rs.getString("var2");
String[] var3= (String[]) rs.getArray("var3").getArray();
var = new Var(var1, var2, var3);
}
connection.setAutoCommit(true);
return var;
}
}
调用此Java代码时,出现以下异常:“尚未实现org.postgresql.jdbc.PgCallableStatement.registerOutParameter(int,int,String)方法。”
所以我的问题是,java中是否有一种方法可以调用返回postgres用户定义类型的函数?如果是,请您告诉我该怎么做?
(而且,一旦我用Java表示它,解析udt的正确方法是什么?)