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