繁体   English   中英

java.sql.SQLRecoverableException:无法读取10个字符的CLOB数据

[英]java.sql.SQLRecoverableException: 10 char of CLOB data cannot be read

我正在尝试将xml文件保存到数据库,但是当我尝试为CallableStatement设置CLOB时,出现此异常:

java.sql.SQLRecoverableException: 10 char of CLOB data cannot be read

当我在Notepad ++中浏览文件并显示所有符号时,看不到任何特殊字符。 数据库的编码为AL32UTF8。 我如何找到这10个无法阅读的字符? 绝对不是人们认为的第十个字符,因为文件中的第十个字符是“ s”。 第一个字符串:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

程式码片段:

CallableStatement cs = con.prepareCall("{ call scenario.add(?,?,?,?,?) }");
cs.registerOutParameter(1, Types.INTEGER);
cs.setLong(2, cfg.getScenarioId());
cs.setString(3, cfg.getType());
cs.setClob(4, new InputStreamReader(new FileInputStream(file), "UTF-8"), (int) file.length());
cs.setString(5, cfg.getFileName());

应用程序在setClob上崩溃(如果使用setCharacterStream(),也会发生同样的情况)。

好吧,我已经找到了解决方案。 我仍然不明白CLOB有什么问题,但是所有数据都完美地保存为对象。 我使用此函数来获取文件内容:

private String getContent(File file) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(new FileInputStream(file), writer, "UTF-8");
    String result = writer.toString();
    return result;
}

然后将结果字符串另存为对象:

CallableStatement cs = con.prepareCall("{ call scenario.add(?,?,?,?,?) }");
cs.registerOutParameter(1, Types.INTEGER);
cs.setLong(2, cfg.getScenarioId());
cs.setString(3, cfg.getType());
String fileContent = getContent(file);
cs.setObject(4, fileContent);
cs.setString(5, cfg.getFileName());

而且有效!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM