Re: Inserting JSON via Java PreparedStatment - Mailing list pgsql-general

From David G. Johnston
Subject Re: Inserting JSON via Java PreparedStatment
Date
Msg-id CAKFQuwbZ-4_W2V0jSrrEkTRz62xJTum+Ld8QReyBXe_9g-A2mA@mail.gmail.com
Whole thread Raw
In response to Inserting JSON via Java PreparedStatment  (Curt Huffman <curt.huffman@gmail.com>)
Responses Re: Inserting JSON via Java PreparedStatment  (rob stone <floriparob@gmail.com>)
List pgsql-general

pStmt.setString(11, dtlRec.toString());
pStmt.setObject(11, dtlRec.toString());

Which produce a different error:

Event JSON: {"New MbrID":29}

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.


​Take the hint, literally.  You never did show the SQL but usually the least complex way to solve this is to indeed transfer the data as a string/text and then instruction PostgreSQL to convert (i.e., cast) it to json.

SELECT (?)::json;  <-- that ? parameter is seen as text; then you convert it.  The parentheses should be optional but I use them to emphasize the point.

​then​

pStmt.setString(1, dtlRec.toString());​

David J.

pgsql-general by date:

Previous
From: MOLINA BRAVO FELIPE DE JESUS
Date:
Subject: query reboot pgsql 9.5.1
Next
From: Adrian Klaver
Date:
Subject: Re: Inserting JSON via Java PreparedStatment