我无法使用JDBC驱动程序和Spring JdbcTemplate向Oracle XMLType列插入很大的XML。
字符串不小,因此我创建了CLOB
并将其通过插入命令传递给XMLTYPE(?)
:
String insertSql = "INSERT INTO tab (xmlcol) VALUES (XMLTYPE(?))";
int[] types = new int[] {Types.CLOB};
SqlLobValue xmlLob = new SqlLobValue(xmlString);
Object[] params = new Object[] {xmlLob};
int status = jdbcTemplate.update(insertSql, params, types);
当xmlString
很小时(例如2 KB
),一切都可以,但是当450 KB
较大时(例如SQLException
)ORA-01461: can bind a LONG value only for insert into a LONG column
会出现以下消息:
should
如何将大型XML文档插入XMLType列?
详细信息: 我正在使用数据库Oracle 12.1,Spring 4.3,ojdbc7 12.1
答案 0 :(得分:1)
您可以使用Java代码创建XMLType,然后将其添加到PreparedStatement
参数中,
有一个Uploading XML into XMLTYPE column in database with Spring JDBCTemplate的示例:
//Next, we have to wrap the byte array in an InputStream to accepted InputStream is = new ByteArrayInputStream(t.getObject().getBytes()); //Then, instantiate an XMLType object by using native OracleConnection and InputStream of the byte array object final XMLType xmldoc = new XMLType(conn, is); ... ps.setObject(3, xmldoc);
编辑-解决方案详细信息:
将依赖项添加到pom.xml
中,以提供:
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>xdb6</artifactId>
<version>12.1.0.2</version>
</dependency>
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>xmlparserv2</artifactId>
<version>12.1.0.2</version>
</dependency>
将普通的Connection
对象提取并解包到OracleConnection
中,并使用它来创建XMLType
,如上面链接的文章所述。也无需创建可以由jdbcTemplate
执行的准备好的语句:
String xmlDocument = "<xml>...<very_large>...</xml>"
XMLType xmlType = new XMLType(conn, xmlDocument);
int status = jdbcTemplate.update(insertSql, xmlType);
现在,它必须能够使用大于4 KB
(已测试1 MB
)或更大的XML内容。