H2数据库。使用html值更新CLOB字段时出错

时间:2018-12-03 06:08:51

标签: java html sql h2 clob

我使用以下代码更新CLOB字段。

final static String PAGE_BODY_UPDATE = "update BODYCONTENT set body = ? where bodycontentid = ?";

private static void updatePageContent(int pageID, String body) {
        Connection conn = getConnection();
        PreparedStatement st = conn.prepareStatement(PAGE_BODY_UPDATE);
        st.setInt(1, pageID);
        Clob myclob = conn.createClob();
        myclob.setString(1, body);
        st.setClob(2, myclob);
        st.executeUpdate();
}

但是它会引发错误。

org.h2.jdbc.JdbcSQLException: Data conversion error converting "<html><head><META http-equiv=""Content-Type"" content=""text/html; charset=utf-8""></head></html>"; SQL statement:
update BODYCONTENT set body = ? where bodycontentid = ? [22018-196]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    at org.h2.message.DbException.get(DbException.java:168)
    at org.h2.value.Value.convertTo(Value.java:996)
    at org.h2.value.ValueLobDb.convertTo(ValueLobDb.java:207)
    at org.h2.mvstore.db.MVPrimaryIndex.getKey(MVPrimaryIndex.java:351)
    at org.h2.mvstore.db.MVDelegateIndex.find(MVDelegateIndex.java:69)
    at org.h2.index.BaseIndex.find(BaseIndex.java:128)
    at org.h2.index.IndexCursor.find(IndexCursor.java:169)
    at org.h2.table.TableFilter.next(TableFilter.java:468)
    at org.h2.command.dml.Update.update(Update.java:102)
    at org.h2.command.CommandContainer.update(CommandContainer.java:101)
    at org.h2.command.Command.executeUpdate(Command.java:260)
    at org.h2.server.TcpServerThread.process(TcpServerThread.java:354)
    at org.h2.server.TcpServerThread.run(TcpServerThread.java:158)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NumberFormatException: For input string: "<html><head><META http-equiv="Content-Type" content="text/html; charset=utf-8"></head></html>"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:589)
    at java.lang.Long.parseLong(Long.java:631)
    at org.h2.value.Value.convertTo(Value.java:942)
    ... 12 more

我尝试使用这样的简单html

"<html><head></head><body></body></html>"

但是我仍然有一个错误。如何将html值上传到CLOB字段? 感谢回答!

1 个答案:

答案 0 :(得分:0)

您已颠倒了参数

尝试

 Clob myclob = conn.createClob();
 myclob.setString(1, body);
 st.setClob(1, myclob);
 st.setInt(2, pageID);