在 HSQLDB 中将图像添加为 BLOB,但类似于“:hsqll:jdbc:client@f3t5h”最终出现在表中
我正在尝试在 HSQLDB 中添加图像作为 BLOB。但在表中我发现类似“:hsqll:jdbc:client@f3t5h”的内容。我下面的Java代码有什么问题吗?
我的检索代码就像我通常为其他数据库所做的那样。为什么它不适用于 HSQLDB?
Statement stmt = conn.createStatement();
ResultSet results =stmt.executeQuery("SELECT * from productdetails");
while (results.next()) {
String code = (String) results.getObject(1);
String name = (String) results.getObjeenter code herect(2);
String price = Double.toString((Double) results.getObject(3));
int quantity = (Integer) results.getObject(4);
Boolean featured = (Boolean) results.getObject(5);
String desc = (String) results.getObject(6);
String imgPath = (String) results.getObject(7);
Blob aBlob = (Blob) results.getBlob(10);
byte[] allBytesInBlob = aBlob.getBytes(1, (int) aBlob.length());
try {
FileOutputStream fis =
(FileOutputStream) getOutputStream(allBytesInBlob);
} catch (IOException ex) {
Logger.getLogger(Helper.class.getName()).log(Level.SEVERE, null, ex);
}
}
public OutputStream getOutputStream(byte[] _data) throws IOException
{
OutputStream out = new ByteArrayOutputStream();
out.write(_data);
return out;
}
这是我的插入代码。
Connection conn = db.getConnection();
PreparedStatement pstmt;
String sql = "INSERT INTO PRODUCTDETAILS"
( PRODUCTCODE, NAME, PRICE, QUANTITY, FEATURED, DESCRPTION, IMAGEPATH,IMAGE )
VALUES ( ?, ?,? ,? ,? , ?, ?,?)";
pstmt = conn.prepareStatement(sql);
FileInputStream fis;
File image = new File(values.getImgPath());
fis = new FileInputStream(image);
pstmt.setBinaryStream(8, (InputStream) fis, (int) (image.length()));
pstmt.execute();
显然,image
列的类型是BLOB
。
I am trying to add an Image as BLOB in HSQLDB. But in the table I find something like ":hsqll:jdbc:client@f3t5h". Is anything wrong with my Java code below?
My retrieving code is like this that i usually do for other databases. Why doesn't it work for HSQLDB?
Statement stmt = conn.createStatement();
ResultSet results =stmt.executeQuery("SELECT * from productdetails");
while (results.next()) {
String code = (String) results.getObject(1);
String name = (String) results.getObjeenter code herect(2);
String price = Double.toString((Double) results.getObject(3));
int quantity = (Integer) results.getObject(4);
Boolean featured = (Boolean) results.getObject(5);
String desc = (String) results.getObject(6);
String imgPath = (String) results.getObject(7);
Blob aBlob = (Blob) results.getBlob(10);
byte[] allBytesInBlob = aBlob.getBytes(1, (int) aBlob.length());
try {
FileOutputStream fis =
(FileOutputStream) getOutputStream(allBytesInBlob);
} catch (IOException ex) {
Logger.getLogger(Helper.class.getName()).log(Level.SEVERE, null, ex);
}
}
public OutputStream getOutputStream(byte[] _data) throws IOException
{
OutputStream out = new ByteArrayOutputStream();
out.write(_data);
return out;
}
That's my inserting code.
Connection conn = db.getConnection();
PreparedStatement pstmt;
String sql = "INSERT INTO PRODUCTDETAILS"
( PRODUCTCODE, NAME, PRICE, QUANTITY, FEATURED, DESCRPTION, IMAGEPATH,IMAGE )
VALUES ( ?, ?,? ,? ,? , ?, ?,?)";
pstmt = conn.prepareStatement(sql);
FileInputStream fis;
File image = new File(values.getImgPath());
fis = new FileInputStream(image);
pstmt.setBinaryStream(8, (InputStream) fis, (int) (image.length()));
pstmt.execute();
The image
column is of type BLOB
, obviously.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 Java 代码对于 HSQLDB 2.2.x 是正确的。该斑点似乎已被插入。您用来检索 blob 的代码似乎不正确。您尚未报告此代码,但显然您没有使用 java.sql.Blob 方法来访问返回的 blob(org.hsqldb.jdbcJDBCBLOBClient 对象)。发布检索代码以进行更正。
如果您在 HSQLDB DatabaseManager 或其他一些工具中查看表数据,它可能会将 blob 显示为对象的地址,而不是显示二进制内容。
Your Java code is correct for HSQLDB 2.2.x. The blob seems to be inserted. It seems the code your are using to retrieve the blob is incorrect. You have not reported this code, but obviously you are not using the java.sql.Blob methods to access the returned blob (the org.hsqldb.jdbcJDBCBLOBClient object). Post retrieve code for correction.
If you look at the table data in HSQLDB DatabaseManager or some other tools, it may show the blob as the address of the object, instead of showing the binary contents.
您调用
aBlob.getBytes(10, aBlob.length())
。但是,
Blob.getBytes()
是要检索的第一个字节的索引(从 1 开始,就像 JDBC 中的所有索引一样)。因此,您忽略(即不获取)Blob
的前 9 个字节。而是传入1
。如果输出实际上如您所显示的那样,那么还有其他问题(可能是您插入 Blob 的方式),但这肯定是您应该解决的第一个问题。
You call
aBlob.getBytes(10, aBlob.length())
.However, the first argument of
Blob.getBytes()
is the index of the first byte to retrieve (1-based, as all indices in JDBC are). So you're ignoring (i.e. not fetching) the first 9 bytes of theBlob
. Pass in1
instead.If the output is actually as you showed, then something else is wrong as well (probably the way you insert the
Blob
), but that's certainly the first problem you should fix.Joachim:10 是列索引,而不是字节[索引]。
您得到的是标准的 Object.toString()。
“显然,图像列是 BLOB 类型。”。是的,在数据库中。你的桌子怎么样?
您的模型是否为此列返回通用 String.class?如果是这样,您应该检查对象本身,
你的数据在里面(对象不在乎它是你的母亲在低图片还是文本中)。
从 byteoutputstream 到 fileoutputstream 的很好的转换,在我的 java 中不起作用
Joachim: 10 is column index, not byte[index].
What you get is standard Object.toString().
„the image column is of type BLOB, obviously.”. Yeah, in db. How about your table?
Is your model return generic String.class for this column? If so, you shoul check the object itself,
your data is inside (and object don't care if it's your mother in low picture or text).
Nice cast for byteoutputstream to fileoutputstream, in my java did not work