Java 操作数类型冲突:varbinary与文本不兼容
我有一个文件上传控件,用户可以通过它将图像文件上传到数据库表 目前我正在使用RDBMS 1 PostgreSQL和2 SQL server 在文件的postgres数据库表列中,我使用datatypeas bytea 在文件的sql server数据库表列中,我使用数据类型作为文本 这是我的方法Java 操作数类型冲突:varbinary与文本不兼容,java,spring,jdbc,Java,Spring,Jdbc,我有一个文件上传控件,用户可以通过它将图像文件上传到数据库表 目前我正在使用RDBMS 1 PostgreSQL和2 SQL server 在文件的postgres数据库表列中,我使用datatypeas bytea 在文件的sql server数据库表列中,我使用数据类型作为文本 这是我的方法 public void uploadRentProofDeclaration(final MultipartFile declarationForm, final int rentPro
public void uploadRentProofDeclaration(final MultipartFile declarationForm,
final int rentProofInfoId, int year) {
String updateSql = "update plit_landlordinfo" + year
+ " set filename=?,declarationform=? where cid=?";
getJdbcTemplate().execute(updateSql, new PreparedStatementCallback() {
public Object doInPreparedStatement(
final PreparedStatement pSstatement) throws SQLException,
DataAccessException {
pSstatement.setString(1, declarationForm.getOriginalFilename());
try {
pSstatement.setBinaryStream(2, new ByteArrayInputStream(
declarationForm.getBytes()), declarationForm
.getBytes().length);
} catch (IOException e) {
e.printStackTrace();
}
pSstatement.setInt(3, rentProofInfoId);
pSstatement.execute();
return null;
}
});
}
这段代码在postgresql和oracle中运行良好,但当我尝试在sql server中上载图像文件时,我遇到了此错误
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [update plit_landlordinfo2011 set filename=?,declarationform=? where cid=?]; Operand type clash: varbinary is incompatible with text; nested exception is java.sql.SQLException: Operand type clash: varbinary is incompatible with text
谁能帮我找出哪里做错了,解决方法是什么。字段类型为text sql server中的是数据类型为text因此请使用setString方法而不是setBinaryStream或将字段类型更改为image或Varbinary我已通过在sql server中将数据类型更改为image解决了此问题。