我正在尝试调用一个Postgres例程,该例程将自定义对象类型作为参数。create type person_type as( first varchar, second varchar, is_real boolean);我的例程(存储过程):create function person_routine(person person_type)returns voidlanguage plpgsqlas $$BEGIN INSERT INTO person(first, second, is_real) VALUES (person.first,person.second,person.is_real);END;$$;然后,我尝试创建一个 Java 类来表示自定义类型:import java.sql.SQLData;import java.sql.SQLException;import java.sql.SQLInput;import java.sql.SQLOutput;public class PersonType implements SQLData {public String first;public String second;public boolean is_real;private String sql_type;@Overridepublic String getSQLTypeName() throws SQLException { return sql_type;}@Overridepublic void readSQL(SQLInput stream, String typeName) throws SQLException { sql_type = typeName; second = stream.readString(); first = stream.readString(); is_real = stream.readBoolean();}@Overridepublic void writeSQL(SQLOutput stream) throws SQLException { stream.writeString(first); stream.writeBoolean(is_real); stream.writeString(second);}}
1 回答
芜湖不芜
TA贡献1796条经验 获得超7个赞
所以,这是关于使用PGobject()。这就是我实现它的方式,它似乎工作得很好。
PGobject person = new PGobject();
person.setType("person_type");
person.setValue(String.format("(%s,%s,%s)","something","something","FALSE"));
query.setObject(1, person);
添加回答
举报
0/150
提交
取消