我有一个PostgreSQL自定义类型:
CREATE TYPE seg_type AS
(
_timestamp bigint,
segment_type bigint,
area_type bigint,
);
我有一个Java类:
public class Segment
{
private Long ts;
private Long seg_type;
private Long area_type;
}
另外我有一个存储过程:
CREATE OR REPLACE FUNCTION SomeFunc(seg_type[])
我正在使用postgresql-9.3-1102.jdbc41.jar连接到我的数据库。我希望能够使用Segment []从Java代码调用存储过程,如下所示:
String query = new String("{call SomeFunc(?)}");
CallableStatement stmt = dbConn.prepareCall(query);
stmt.setObject(1, Segment[], Type);
如果这是正确的实现,那么我应该在Type中输入什么?还是实施不正确?
可能的解决方案如下:
在java
Segment类中创建一个函数,该函数以postgreSQL类型的格式创建一个字符串。
public class Segment
{
private Long ts;
private Long seg_type;
private Long area_type;
public pgType(){
return String.format("\"(%d,%d,%d)\"",
this.ts, this.seg_type, this.area_type)
}
}
注意:对于不同的数据类型,您可能需要以不同的方式设置字符串格式。
这将返回以下字符串"(ts, seg_type, area_type)"
,表示seg_type
postgres中的类型。
现在,您需要初始化Segment[]
数组以形成postgres array
。
StringBuilder segTypeArray = new StringBuilder("{");
segTypeArray.append(segments.[0].getDBValue());
for(int i = 1; i < segments.length; i++){
segTypeArray.append("," + list.get(i).getDBValue());
}
segTypeArray.append("}");
这将创建一个postgres array
的seg_type
类型{"(ts, seg_type, area_type)","(ts, seg_type, area_type)",.....}
创建查询字符串时,请确保强制转换为seg_type []
String query = new String("{call SomeFunc(?::seg_type[])}");
CallableStatement stmt = dbConn.prepareCall(query);
stmt.setObject(1, segTypeArray.toString());
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句