java - 将 Blob 保存/读取到 Oracle DB JAVA
问题描述
我想将 Oracle DB 中的图像保存为 BLOB。
尝试执行此操作时,我收到此错误消息
java.sql.SQLException: ORA-01465: invalid hex number
我的代码片段:
...
Blob blob = con.createBlob();
blob.setBytes(1, myByteArray); // blob = oracle.sql.BLOB@7543e69b
String query = "update pictures set pic = \'" + blob + "\' where txt = \'Photo1\'";
statement.executeQuery(query);
...
接下来我尝试了一个新版本,其中我只在数据库中的“oracle.sql.BLOB@”后面插入了十六进制数字(7543e69b)。
那行得通,在读出 blob 后,我又得到了一个 blob 对象。
但将此 blob 对象转换为图标后,无法再显示图像。但是从 blob 转换为图标后,图像不再显示在 GUI 中。并且在调试的时候,图片的大小是,比如width: -1, height: -1, ....
此代码片段:
...
Blob blob = con.createBlob();
blob.setBytes(1, myByteArray); // blob = oracle.sql.BLOB@7543e69b
String picture = blob + "";
picture = picture.substring(16); // picture = 7543e69b
String query = "update pictures set pic = \'" + picture + "\' where txt = \'Photo1\'";
statement.executeQuery(query);
...
Blob a = rs.getBlob(4);
ImageIcon imageIcon = null;
try {
byte[] bArray = a.getBytes((long) 1, (int) a.length());
imageIcon = new ImageIcon(bArray);
} catch (Exception ex) {
...
}
photo.setIcon(imageIcon);
这里出了什么问题?是不是存错了?
解决方案
推荐阅读
- r - 如何在 R 中使用 ggplot 绘制甘特图时避免段之间的重叠
- django - 使用 IIS 服务器在 Windows 上运行 Django
- linux - 如何在 GNU/Linux 中不使用 Adobe Acrobat 删除 PDF/A 标记
- postgresql - 如何从事务块中的 INSERT 语句返回值?
- python - OutOfBoundsDatetime:越界纳秒时间戳
- cakephp-3.0 - CakePHP 3.8。安全组件 - FormHelper end() 方法
- c# - 使用 C# 和 Npgsql 返回 PostgreSQL 数据库上新插入行的 ID?
- android - 如何在 Firestore Android 中获取文档 ID
- java - DynamoDBMapper scanPage 不返回任何结果
- sql - 时间戳现在显示当前时间戳的所有字段