如何使用Java preparedStatement将JSON对象插入Postgres? [英] How can I Insert JSON object into Postgres using Java preparedStatement?
问题描述
我正在努力将JSON对象插入到postgres v9.4 DB中。我已将名为evtjson的列定义为类型 json
(不是 jsonb
)。
I’m struggling to insert a JSON object into my postgres v9.4 DB. I have defined the column called "evtjson" as type json
(not jsonb
).
我试图在Java(jdk1.8)中使用预准备语句将Json对象(使用JEE javax.json库构建)插入到列中,但是我一直遇到SQLException错误。
I am trying to use a prepared statement in Java (jdk1.8) to insert a Json object (built using JEE javax.json libraries) into the column, but I keep running into SQLException errors.
我使用以下方法创建JSON对象:
I create the JSON object using:
JsonObject mbrLogRec = Json.createObjectBuilder().build();
…
mbrLogRec = Json.createObjectBuilder()
.add("New MbrID", newId)
.build();
然后我将此对象作为参数传递给另一个方法,使用预准备语句将其写入数据库。 (以及其他几个字段)As:
Then I pass this object as a parameter to another method to write it to the DB using a prepared statement. (along with several other fields) As:
pStmt.setObject(11, dtlRec);
使用此方法,我收到以下错误:
Using this method, I receive the following error:
org.postgresql.util.PSQLException:未安装hstore扩展。
at org.postgresql.jdbc.PgPreparedStatement.setMap(PgPreparedStatement.java:553)
at org.postgresql.jdbc.PgPreparedStatement.setObject(PgPreparedStatement.java:1036)
org.postgresql.util.PSQLException: No hstore extension installed. at org.postgresql.jdbc.PgPreparedStatement.setMap(PgPreparedStatement.java:553) at org.postgresql.jdbc.PgPreparedStatement.setObject(PgPreparedStatement.java:1036)
我也尝试过:
pStmt.setString(11, dtlRec.toString());
pStmt.setObject(11, dtlRec.toString());
产生不同的错误:
事件JSON:{New MbrID:29}
Event JSON: {"New MbrID":29}
SQLException:错误:列evtjson的类型为json,但表达式类型字符变化
SQLException: ERROR: column "evtjson" is of type json but expression is of type character varying
提示:您需要重写或转换表达式。
Hint: You will need to rewrite or cast the expression.
<但是,至少这告诉我DB正在将列识别为JSON类型。
我确实尝试安装hstore扩展,但它告诉我它不是一个hstore对象。
But, at least this tells me that the DB is recognizing the column as type JSON. I did try installing the hstore extension, but it then told me that it was not an hstore object.
OracleDocs显示了许多设置方法的方法。 preparedStatement中的参数值,但如果有人知道答案,我宁愿不尝试所有这些。 ( http://docs.oracle.com/javase/8/docs/api /java/sql/PreparedStatement.html )这些也引用了一个额外的参数SQLType,但我找不到对它们的任何引用。
OracleDocs shows a number of various methods to set the parameter value in the preparedStatement, but I'd rather not try them all if someone knows the answer. (http://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html) These also reference an additional parameter, SQLType, but I can't find any reference to these.
我应该尝试 setAsciiStream
? CharacterStream
? CLOB?
Should I try setAsciiStream
? CharacterStream
? CLOB?
推荐答案
这种行为非常烦人,因为在SQL命令中用作文字字符串时,JSON字符串被接受没有问题。
This behaviour is quite annoying since JSON strings are accepted without problems when used as literal strings in SQL commands.
在postgres中已经有一个问题驱动程序Github存储库(即使问题似乎是服务器端处理)。
There is a already an issue for this in the postgres driver Github repository (even if the problem seems the be the serverside processing).
除了在sql字符串中使用强制转换(请参阅
@a_horse_with_no_name的答案) ,问题作者提供了两个额外的解决方案:
Besides using a cast (see answer of @a_horse_with_no_name) in the sql string, the issue author offers two additional solutions:
- 使用参数
stringtype = unspecified
在JDBC连接URL /选项中。
- Use a parameter
stringtype=unspecified
in the JDBC connection URL/options.
这告诉PostgreSQL所有text或varchar参数实际上是
未知类型,让它可以更自由地推断出它们的类型。
This tells PostgreSQL that all text or varchar parameters are actually of unknown type, letting it infer their types more freely.
- 将参数包装在<$ c中$ c> org.postgresql.util.PGobject :
- Wrap the parameter in a
org.postgresql.util.PGobject
:
PGobject jsonObject = new PGobject();
jsonObject.setType("json");
jsonObject.setValue(yourJsonString);
pstmt.setObject(11, jsonObject);
这篇关于如何使用Java preparedStatement将JSON对象插入Postgres?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!