0

I have a org.json.simple.JSONObject and I want to insert it into a clob type column in Oracle Database using PreparedStatement. How can I do that?

When I do preparedStatement.setObject(1, json_obj); I get the error : Can''t infer the SQL type in setObject for org.json.simple.JSONObject

16
  • Why not convert the JSONObject or a JSON String and insert the text? Commented Jun 20, 2018 at 0:42
  • The column type in database cannot be changed from clob to anyother type. When I try preparedStatement.setString(1, json_obj.toString()); I get SQL error: 1036 ORA-01036: illegal variable name/number Commented Jun 20, 2018 at 0:49
  • Well, you should be using toJSONString instead of toString so you get the actual JSON structure Commented Jun 20, 2018 at 0:52
  • Also, this might help Commented Jun 20, 2018 at 0:54
  • toJSONString gives same SQL error: 1036 ORA-01036: illegal variable name/number. As suggested in above link tried oracle.sql.CLOB.createTemporary(conn, false, oracle.sql.CLOB.DURATION_SESSION); which gives org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to oracle.jdbc.OracleConnection. I believe there should be an easier way. Commented Jun 20, 2018 at 1:15

1 Answer 1

0

I put the json as a String and created this method, it worked for me:

public long  insertClob(String  id, String json ) {
    KeyHolder holder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {

    public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
        PreparedStatement ps = connection.prepareStatement("UPDATE tableWithClobColumn SET clobColumn=? WHERE id=?",
                Statement.RETURN_GENERATED_KEYS);
        Reader reader = new StringReader(json);
        ps.setClob(1, reader);
        ps.setString(2, "1911030604");

        return ps;
    }
}, holder);
return Long.parseLong("0");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.