JDBCTemplate / NamedParameterJdbcTemplate无法插入FileItem / Blob

问题描述 投票:0回答:1

我有一个正常的PreparedStatement插入FileItem fileItem到BLOB列:

ps.setBinaryStream(1, fileItem.getInputStream(), (int) fileItem.getSize());

我可以输入FileItemNamedParameterJdbcTemplate的问题:

我尝试使用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);

使用FileInputStream等不同参数失败

我正在尝试保存(二进制)图像文件

java blob jdbctemplate named-parameters column-types
1个回答
0
投票

它仅在我将参数类型更改为byte[]时才起作用:

 byte[] blob

并在插入中使用它:

jdbcTemplate.update(INSERT_FILE, blob);
© www.soinside.com 2019 - 2024. All rights reserved.