首页 > 解决方案 > Java:如何将 JSON 对象插入到 CLOB 到 oracle 数据库中

问题描述

我有一个org.json.simple.JSONObject并且我想使用 PreparedStatement 将它插入到 Oracle 数据库中的 clob 类型列中。我怎样才能做到这一点?

当我这样做时,preparedStatement.setObject(1, json_obj);我收到错误:无法在 setObject 中推断 org.json.simple.JSONObject 的 SQL 类型

标签: javajsonoracleprepared-statementclob

解决方案


我将 json 作为字符串并创建了这个方法,它对我有用:

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");
}

推荐阅读