更新以mysql blob格式保存的图像

时间:2012-09-24 12:05:25

标签: java mysql image blob

在我的程序中,用户可以通过java程序将图像保存在mysql中。它将显示在Jlabel中。图像插入部分工作正常。图像插入并正确显示。但是当用户尝试更新图像时图像没有正确更新。更新后,当我去检查Mysql时,图像不存在。有一些垃圾价值。任何人都可以帮助我吗?

//Method to get the image from the file chooser
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();
    filename = f.getAbsolutePath();
    jTextField1.setText(filename);

    try {
        File image = new File(filename);
        FileInputStream fis = new FileInputStream(image);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];

        for (int readNum; (readNum = fis.read(buf)) != -1;) {
            bos.write(buf, 0, readNum);
        }
        menu_image_new = bos.toByteArray();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
} 

// Save button click event where the update query run
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    try {
        Connection con = DB_Con.getConnection();
        String sql = "update menu set Menu_Image='" + menu_image_new + "' where Menu_Code='" + FoodMenu_FOA.clicked + "'";
        PreparedStatement st = (PreparedStatement) con.prepareStatement(sql);
        st.executeUpdate();
        JOptionPane.showMessageDialog(null, "Image Updated Successfully");
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
} 

//Method to display image in Jlabel
public void set_Icon_menu() {
    try {
        int row = menu_table.getSelectedRow();
        Connection con = DB_Con.getConnection();
        ResultSet rs;
        String menu_id = menu_table.getModel().getValueAt(row, 0).toString();
        String sql = "select Menu_Image from menu where Menu_Code='" + menu_id + "'";
        PreparedStatement st = (PreparedStatement) con.prepareStatement(sql);
        rs = st.executeQuery();
        if (rs.next()) {
            byte[] imagedata = rs.getBytes("Menu_Image");
            format = new ImageIcon(imagedata);
            menu_label.setIcon(format);

        }
    } catch (Exception e) {
    }
}

4 个答案:

答案 0 :(得分:0)

我不知道如何在Java库中执行此操作,但您应该使用带有参数的SQL查询(可能是setBinaryStream或setBlob)而不是查找SQL字符串。这可能会解决你的问题...

发现这可能会对您有所帮助:

PreparedStatement on Java and probability to set parameters

http://www.herongyang.com/JDBC/MySQL-PreparedStatement-Parameters.html

和此:

http://www.jdbc-tutorial.com/jdbc-prepared-statements.htm

答案 1 :(得分:0)

PreparedStatement stmt = con.preparestatement("update menu set Menu_Image= ? where Menu_Code= ?");
stmt.setString(1, menu_image_new);
stmt.setBlob(2, FoodMenu_FOA.clicked);

这将创建一个准备好的声明,请完全确定它是否能解决您的问题

答案 2 :(得分:0)

我遇到了同样的问题,首先需要列中的明确blob格式。 通过将Menu_Image设置为null

// Save button click event where the update query run

     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
   PreparedStatement st;
 try {
    Connection con = DB_Con.getConnection();
 String sql = "update menu set Menu_Image= null where Menu_Code='" + 
   FoodMenu_FOA.clicked + "'";
    st = (PreparedStatement) con.prepareStatement(sql);
    st.executeUpdate();
    } catch (Exception e) {
      JOptionPane.showMessageDialog(null, e);
   } 

 // As your are using prepare statement set 

    try {
      Connection con = DB_Con.getConnection();
        String sql = "update menu set Menu_Image= ? where Menu_Code='" + 
      FoodMenu_FOA.clicked + "'";
        st = (PreparedStatement) con.prepareStatement(sql);
      st.executeUpdate();
      JOptionPane.showMessageDialog(null, "Image Updated Successfully");
    } catch (Exception e) {
      JOptionPane.showMessageDialog(null, e);
       }
 }

答案 3 :(得分:-1)

您可以尝试:

try {
    String update = "update menu set IMAGE=? where Menu_Code='" + 
        edit_idnumber.getText() + "'";
    pst = conn.prepareStatement(update);
    pst.setBytes(1, menu_image_new);
    pst.execute();  
} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
}