我不明白为什么它不起作用。
其Java插入按钮。输入流中存在错误。添加了2个进口。流导入java.io.FileInputStream;导入java.io.InputStream;

InputStream img =新FileInputStream(新File(ImgPath));错误未报告的异常FileNotFoundException;必须被抓住或宣布

    private void Btn_InsertActionPerformed(java.awt.event.ActionEvent evt) {

        if (checkInputs() && ImgPath != null) {
            try {
                Connection con = getConnection();
                PreparedStatement ps = con.prepareStatement("INSERT INTO products(name,price,add_date,image"
                        + "value(?,?,?,?) ");
                ps.setString(1, "txt_name.getText()");
                ps.setString(2, "txt_price.getText()");
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String addDate = dateFormat.format("txt_AddDate.getDate()");
                ps.setString(3, addDate);

                InputStream img = new FileInputStream(new File(ImgPath));
                ps.setBlob(4, img);
                ps.executeUpdate();
                JOptionPane.showMessageDialog(null, "Data ");

            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }

        }
        else {
        JOptionPane.showMessageDialog(null, "One or More Filed Are Empty");
        }
    }

最佳答案

将catch块更新为:

catch (SQLException | FileNotFoundException ex) {
   JOptionPane.showMessageDialog(null, ex.getMessage());
}

08-08 00:55
查看更多