我試圖在HSQLDB中添加一個圖像作爲BLOB。但是在表格中,我發現類似「:hsqll:jdbc:client @ f3t5h」。我的Java代碼下面有什麼問題嗎?在HSQLDB中添加圖像作爲BLOB,但像「:hsqll:jdbc:client @ f3t5h」結果在表中
我的檢索代碼就像我通常爲其他數據庫所做的那樣。爲什麼它不適用於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
類型的,很明顯。
爲什麼你不添加圖像的位置,而不是圖像的數據庫? – kinkajou
雖然在回顧blob我得到以下錯誤:線程「主」java.lang.ClassCastException異常:org.hsqldb.jdbc.JDBCBlobClient不能轉換爲com.mysql.jdbc.Blob –
我看到你的驅動程序是不同的組織。 hsqldb.jdbc.JDBCBlobClient和com.mysql.jdbc.Blob是不同的。看到你的連接參數。所以你使用相同的hsql數據庫的驅動程序。 – kinkajou