Buz*_*tti 7 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();
Run Code Online (Sandbox Code Playgroud)
抛出此异常:
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
Run Code Online (Sandbox Code Playgroud)
请注意,从psql中,删除使用字符串:
mydb=# DELETE FROM foo where ctid = '(0,43)';
DELETE 1
Run Code Online (Sandbox Code Playgroud)
JDBC PreparedStatement中tid的正确类型/编码是什么?我试过setRowId()(抛出ava.sql.SQLFeatureNotSupportedException:方法org.postgresql.jdbc4.Jdbc4PreparedStatement.setRowId(int,RowId)尚未实现.)和setBytes()(throws ...运算符不存在:tid =字节)
解决了!您必须手动创建 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1065 次 |
| 最近记录: |