我正在尝试在数据库中插入和更新已经是 blob 格式的图像。
public static boolean alterarLogo(int id, Blob logo) {
String sql = "update conta set conta_logo = " + logo + " where conta_id = " + id;
try (Connection conn = ConexaoDBGeral.abre()) {
if (conn != null) {
try (Statement ps = conn.createStatement()) {
ps.executeUpdate(sql);
return true;
}
}
} catch (SQLException ex) {
Logger.getLogger(ContaDAO.class.getName())
.log(Level.SEVERE, null, ex);
}
return false;
}
上面的代码抛出异常:“不正确的限定名称(太多点名称):javax.sql.rowset.serialblob”。在 Db 中,相关列的类型为“oid”。
我获取 Blob 的代码:
int idEmpresa=-1;
SerialBlob blob = null;
byte[] contents;
try {
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (!item.isFormField()) {
// Process form file field (input type="file").
String fieldName = item.getFieldName();
String fileName = FilenameUtils.getName(item.getName());
InputStream file = item.getInputStream();
contents = IOUtils.toByteArray(file);
try {
blob = new SerialBlob(contents);
} catch (SQLException ex) {
Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
}
ContaDAO.alterarLogo(idEmpresa, blob);
}
}
} catch (FileUploadException e) {
我正在使用 Java、Servlet 和 JSP 以及 PostGres。错误在哪里?
最佳答案
仅部分答案。
您将生成如下 SQL 语句:
String sql = "update conta set conta_logo = " + logo +
" where conta_id = " + id;
由于 logo
被声明为 Blob
,因此您将在生成 SQL 时调用 Blob.toString()
。 Blob
的 javadoc 没有指定 toString()
方法将执行的操作,但它不太可能将 Blob
呈现为有意义的内容在 SQL 语言中。根据您看到的错误消息,我的猜测是它实际上使用 Object::toString()
渲染对象,其中将包含 Blob
的实际类名称您当时正在使用的实例。
关于java - blob提交到DB时出现什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56582756/