这是我的代码
private void UploadActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser Attach = new JFileChooser();
try {
if (Attach.showOpenDialog(Upload) == JFileChooser.APPROVE_OPTION) {
File ImageFile = Attach.getSelectedFile();
lbl_Image.setIcon(new ImageIcon(ImageFile.toString()));
lbl_Image.setHorizontalAlignment(JLabel.CENTER);
filename = ImageFile.getAbsolutePath();
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);
}
person_image = bos.toByteArray();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
然后是我的更新数据库代码
con = DriverManager.getConnection(Module.url, Module.username, Module.password);
String sql = "Update resume set Image = ?, FirstName = ? where ID = '" + ID.getText() + "'";
ps = con.prepareStatement(sql);
ps.setBytes(1, person_image);
ps.setString(2, WordUtils.capitalizeFully(Fname.getText()));
ps.executeUpdate();
我想知道,如果用户想要更新数据库中的数据并且他没有选择JFileChooser上的任何文件,那么我如何制作程序不应该更新数据库中的Image(Blob)字段。
因为在我的代码中,如果用户没有在JFileChooser上选择任何文件,则Image(Blob)字段将更新为NULL。
答案 0 :(得分:1)
你可以像这样检查你的person_image数组是否为
if (person_image != null) {
// Now do whatever you want to do !!
}
else{
throw new ImageNotSelectedCustomException(); // this is your custom exception
// you can also simply ignore it and do another piece of work
}
注意:请阅读一些basic tutorial on Java,因为检查Null并不是什么大问题,如果您可以自己编写这么多代码。