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);
// the line below is throwing class cast exception as i have mentionde below
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;
}
This is 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.
while retriving blob i am getting following error:
Exception in thread "main" java.lang.ClassCastException:
org.hsqldb.jdbc.JDBCBlobClient cannot be cast to com.mysql.jdbc.Blob