如何在derby blob中保存toolkitimage

时间:2011-01-18 12:14:22

标签: java derby

我正在尝试将图像保存到Derby Db(JDBC),但它提供了异常

   void imageload () {
FileDialog fd = new FileDialog(MainFram.this,"Open", FileDialog.LOAD);
fd.show();
if(fd.getFile() == null){
//Label1.setText("You have not chosen any image files yet");
}
else{

String d = (fd.getDirectory() + fd.getFile());
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image1 = toolkit.getImage(d);`
Object saveImage=Image1;
insertImage(saveImage) //Db function

` 这是我的数据库功能

public void insertImage(String CUST_ID, Object saveImage) throws SQLException {
    try {
            Class.forName(driver);
            con = DriverManager.getConnection(connectionURL);

            } catch (SQLException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
            }      
     FileInputStream fis = new FileInputStream(saveImage);
    stm = con.prepareStatement("insert into customer (PHOTO ) values(?)); //it gives errorr here
    stm.setObject(5,saveImage);

    stm.executeUpdate();
}

1 个答案:

答案 0 :(得分:0)

void imageload () {
FileDialog fd = new FileDialog(MainFram.this,"Open", FileDialog.LOAD);
fd.show();
if(fd.getFile() == null){
//Label1.setText("You have not chosen any image files yet");
}
else{

String d = (fd.getDirectory() + fd.getFile());
//Toolkit toolkit = Toolkit.getDefaultToolkit();
//Image1 = toolkit.getImage(d);`
//Object saveImage=Image1;
insertImage(d) //Db function

然后

public void insertImage(String CUST_ID, String d) throws SQLException {
    try {
            Class.forName(driver);
            con = DriverManager.getConnection(connectionURL);

            } catch (SQLException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
            }
    File image = new File(d);      
    FileInputStream fis = new FileInputStream(image);
    stm = con.prepareStatement("insert into customer (PHOTO ) values(?)); //it gives errorr here
    stmt.setBinaryStream(5, fis, (int) image.length());
    //stm.setObject(5,saveImage);
    //stm.executeUpdate();
    stm.execute();
}