1

在我的程序中,用户可以通过 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

4 回答 4

0

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

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

Java上的PreparedStatement和设置参数的概率

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

和这个:

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

于 2012-09-24T12:36:35.897 回答
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);

这将创建一个准备好的语句,请注意完全确定它是否会解决您的问题

于 2012-09-24T12:51:06.207 回答
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);
       }
 }
于 2017-05-23T14:58:31.497 回答
-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);
}
于 2017-04-04T05:42:06.203 回答