如何知道 JFileChooser 是否为空,如果它为空则不更新数据库

How to Know if JFileChooser is null and if its null dont Update the database

这是我的代码

    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 上选择任何文件,我该如何制作程序,数据库中的图像(Blob)字段不应该已更新。

因为在我的代码中,如果用户未在 JFileChooser 上选择任何文件,Image(Blob) 字段将更新为 NULL。

您可以像这样检查 person_image 数组是否为 null

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 不是什么大问题 ,如果你能写自己写这么多代码。