在我的程序中,用户可以通过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) {
}
}
我不知道如何在 Java 库中执行此操作,但您应该使用带参数的 SQL 查询(可能是 setBinaryStream 或 setBlob),而不是连接 SQL 字符串。这可能会解决你的问题...
发现这个可能对你有帮助:
Java上的PreparedStatement以及设置参数的概率
http://www.herongyang.com/JDBC/MySQL-PreparedStatement-Parameters.html
还有这个:
PreparedStatement stmt = con.preparestatement("update menu set Menu_Image= ? where Menu_Code= ?");
stmt.setString(1, menu_image_new);
stmt.setBlob(2, FoodMenu_FOA.clicked);
这将创建一个准备好的声明,请完全确定它是否能解决您的问题
我遇到了同样的问题,您首先需要在列中清除 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);
}
}
您可以尝试:
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);
}