我尝试使用spring jdbc 3.0将byte []插入到oracle.sql.BLOB列中。我得到以下例外:
java.lang.ClassCastException: [B cannot be cast to oracle.sql.BLOB| at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:8752)| at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8286)| at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:8868)| at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:240)| at org.apache.commons.dbcp.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:166)| at org.apache.commons.dbcp.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:166)| at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:356)| at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:216)| at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:144)| at org.springframework.jdbc.core.ArgTypePreparedStatementSetter.doSetValue(ArgTypePreparedStatementSetter.java:97)| at org.springframework.jdbc.core.ArgTypePreparedStatementSetter.setValues(ArgTypePreparedStatementSetter.java:79)| at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:815)| at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:811)| at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:586)|at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:811)| at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:867)| at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:871)| at org.springframework.jdbc.core.simple.AbstractJdbcInsert.executeInsertInternal(AbstractJdbcInsert.java:366)| at org.springframework.jdbc.core.simple.AbstractJdbcInsert.doExecute(AbstractJdbcInsert.java:356)| at org.springframework.jdbc.core.simple.SimpleJdbcInsert.execute(SimpleJdbcInsert.java:118)| ....
byte []是从ByteBuffer对象构造的。
byte[] byte_array = byte_buffer.array();
我也尝试插入一个java.sql.Blob类型,但这也有类似的例外:
java.lang.ClassCastException: javax.sql.rowset.serial.SerialBlob cannot be cast to oracle.sql.BLOB| at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:8752)| at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8286)| at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:8868)| at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:240)| at org.apache.commons.dbcp.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:166)| at org.apache.commons.dbcp.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:166)| at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:356)| at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:216)| at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:144) ....
java.sql.Blob也是从ByteBuffer对象创建的。
Blob blob = new SerialBlob(byte_buffer.array());
看起来StatementCreatorUtils不像处理CLOB或NCLOB那样处理java.sql.Blob。有解决办法吗?或者我应该使用不同的java sql类型?
答案 0 :(得分:0)
让类型为BINARY
而不是BLOB
(Types.BINARY
)
我用这种方式成功执行
答案 1 :(得分:0)
你可能想看一下Spring的SqlLobValue
。将byte[]
值附加到查询参数映射中,如下所示:
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("blobParam", new SqlLobValue(byteArrayValue, lobHandler), Types.BLOB);
其中lobHandler
是LobHandler
bean(即org.springframework.jdbc.support.lob.DefaultLobHandler
)。