JdbcTemplate 插入 XmlType 在较大尺寸上失败
JdbcTemplate insert XmlType failed on larger sizes
我无法使用 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
如何将大 XML 文档插入 XML 类型列?
详情:
我正在使用数据库 Oracle 12.1,Spring 4.3,ojdbc7 12.1
您可以创建XML键入Java代码然后添加到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);
现在它必须与 XML 大小超过 4 KB
(1 MB
已测试)甚至更大的内容一起使用。
我无法使用 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
如何将大 XML 文档插入 XML 类型列?
详情: 我正在使用数据库 Oracle 12.1,Spring 4.3,ojdbc7 12.1
您可以创建XML键入Java代码然后添加到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);
现在它必须与 XML 大小超过 4 KB
(1 MB
已测试)甚至更大的内容一起使用。