postgres中ctid的JDBC类型是什么?

时间:2013-09-17 14:33:51

标签: java postgresql jdbc prepared-statement

我似乎无法在准备好的声明中设置正确的类型。这段代码:

String sql = "delete from foo where ctid = ?";
PreparedStatement deleteStmt = conn.prepareStatement( sql );
deleteStmt.setString(1, "(0,43)");  // select ctid from foo shows (0,43) exists....
int a = deleteStmt.executeUpdate();

抛出此异常:

org.postgresql.util.PSQLException: ERROR: operator does not exist: tid = character varying
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.   Position: 28

请注意,从psql中,删除使用字符串:

mydb=# DELETE FROM foo where ctid = '(0,43)';
DELETE 1

JDBC PreparedStatement中tid的正确类型/编码是什么?我试过setRowId()(抛出ava.sql.SQLFeatureNotSupportedException:方法org.postgresql.jdbc4.Jdbc4PreparedStatement.setRowId(int,RowId)尚未实现。)和setBytes()(throws ...运算符不存在:tid = byte)

2 个答案:

答案 0 :(得分:3)

解决!您必须手动创建PGO对象并设置类型和值,并将其作为对象传递给JDBC。这现在有效:

sql = "delete from foo where ctid = ?";
deleteStmt = conn.prepareStatement( sql );
org.postgresql.util.PGobject pgo = new org.postgresql.util.PGobject();
pgo.setType("tid");
pgo.setValue("(0,54)");  // value is a string as might be returned in select ctid from foo and then resultSet.getString(1);
deleteStmt.setObject(1, pgo);

int a = deleteStmt.executeUpdate();
System.out.println("delete returns " + a);

答案 1 :(得分:0)

旧话题,但我想补充一点写作

where ctid = ?::tid";

而不是

where ctid = ?::ctid";

做了这个伎俩。然后可以使用PreparedStatement.setString(x,"(10,3)")