简体   繁体   中英

JDBCTemplate/NamedParameterJdbcTemplate failed to insert FileItem/Blob

I have a normal PreparedStatement that insert FileItem fileItem to BLOB column:

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

The problem I can enter FileItem to NamedParameterJdbcTemplate :

I tried with Types.BLOB :

MapSqlParameterSource paramSource = new MapSqlParameterSource();
paramSource.addValue("blob",  fileItem.getInputStream(), Types.BLOB);
jdbcTemplate.update(INSERT_FILE, paramSource);

But got exception

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)

Same error with jdbcTemplate with parameter BLOB file :

jdbcTemplate.update(INSERT_FILE, file);

Failed similarly with different parameters as File and InputStream

I'm trying to save a (binary) image file

It's work only when I change parameter type to byte[] :

 byte[] blob

And use it in insert:

jdbcTemplate.update(INSERT_FILE, blob);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM