我有一个普通的PreparedStatement
,它在BLOB列中插入了FileItem fileItem
:
ps.setBinaryStream(1, fileItem.getInputStream(), (int) fileItem.getSize());
我可以在FileItem
到NamedParameterJdbcTemplate
之间输入问题:
我尝试过Types.BLOB
:
MapSqlParameterSource paramSource = new MapSqlParameterSource();
paramSource.addValue("blob", fileItem.getInputStream(), Types.BLOB);
jdbcTemplate.update(INSERT_FILE, paramSource);
但是有例外
SQL state [99999]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type
Caused by: java.sql.SQLException: Invalid column type
at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:10503)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:9974)
at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:10581)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:249)
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setObject(HikariProxyPreparedStatement.java)
at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:469)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:241)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:156)
at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.setValues(PreparedStatementCreatorFactory.java:292)
at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:244)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:633)
参数jdbcTemplate
与BLOB file
的错误:
jdbcTemplate.update(INSERT_FILE, file);
类似地失败,但使用了不同的参数,例如File
和InputStream
我正在尝试保存(二进制)图像文件
答案 0 :(得分:0)
仅当我将参数类型更改为byte[]
时才起作用:
byte[] blob
并在插入中使用它:
jdbcTemplate.update(INSERT_FILE, blob);