将CLOB插入Oracle数据库

ken*_*dds 4 java oracle jdbc insert clob

我的问题是:ORA-01704: string literal too long在使用CLOBs 插入(或在查询中执行任何操作)时,如何解决错误?

我想要这样的查询:

INSERT ALL
   INTO mytable VALUES ('clob1')
   INTO mytable VALUES ('clob2') --some of these clobs are more than 4000 characters...
   INTO mytable VALUES ('clob3')
SELECT * FROM dual;
Run Code Online (Sandbox Code Playgroud)

当我尝试使用实际值时,虽然我ORA-01704: string literal too long回来了.这很明显,但是如何插入clobs(或者使用clob执行任何语句)?

我试过看过这个问题,但我认为它没有我想要的东西.我拥有的clob在a中List<String>,我遍历它们来做出声明.我的代码如下:

private void insertQueries(String tempTableName) throws FileNotFoundException, DataException, SQLException, IOException {
String preQuery = "  into " + tempTableName + " values ('";
String postQuery = "')" + StringHelper.newline;
StringBuilder inserts = new StringBuilder("insert all" + StringHelper.newline);
List<String> readQueries = getDomoQueries();
for (String query : readQueries) {
  inserts.append(preQuery).append(query).append(postQuery);
}
inserts.append("select * from dual;");

DatabaseController.getInstance().executeQuery(databaseConnectionURL, inserts.toString());
Run Code Online (Sandbox Code Playgroud)

}

public ResultSet executeQuery(String connection, String query) throws DataException, SQLException {
  Connection conn = ConnectionPool.getInstance().get(connection);
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery(query);
  conn.commit();
  ConnectionPool.getInstance().release(conn);
  return rs;
}
Run Code Online (Sandbox Code Playgroud)

a_h*_*ame 7

你正在走向复杂的道路.

对列表中的每个clob使用PreparedStatement和addBatch():

String sql = "insert  into " + tempTableName + " values (?)";
PreparedStatement stmt = connection.prepareStatement(sql);
for (String query : readQueries) {
  stmt.setCharacterStream(1, new StringReader(query), query.lenght());
  stmt.addBatch();
}
stmt.exececuteBatch();
Run Code Online (Sandbox Code Playgroud)

没有弄乱转换字符串,文字的长度没有问题,不需要创建临时clobs.而且大多数可能与使用单个INSERT ALL语句一样快.

如果您使用的是当前驱动程序(> 10.2),那么我认为setCharacterStream()调用和Reader的创建也不是必需的.一个简单的setString(1, query)意志很可能也会起作用.