0

I cant see why it doesn't work. Its Java Insert button. Error is in InputStream line. 2 imports added. Stream import java.io.FileInputStream; import java.io.InputStream;

InputStream img = new FileInputStream(new File(ImgPath)); error unreported exception FileNotFoundException; must be caught or declared

    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");
        }
    } 
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52

1 Answers1

1

Update the catch block to this :

catch (SQLException | FileNotFoundException ex) {
   JOptionPane.showMessageDialog(null, ex.getMessage());
}
Anish B.
  • 9,111
  • 3
  • 21
  • 41
  • 1
    Yes it helped but there was another error Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Date – Tomasz Barański Feb 16 '20 at 17:49
  • Code has no error but when i compile, im adding data to all and push button insert - Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Date at java.base/java.text.DateFormat.format(DateFormat.java:338) at java.base/java.text.Format.format(Format.java:158) at java_project_1_2.Main_Window.Btn_InsertActionPerformed(Main_Window.java:41... etc – Tomasz Barański Feb 16 '20 at 18:17
  • 1
    @TomaszBarański Check this : https://stackoverflow.com/questions/18614836/using-setdate-in-preparedstatement – Anish B. Feb 16 '20 at 18:25
  • 1
    @TomaszBarański Please ask only one question at a time and do not ask for further help in the comments. – Joakim Danielson Feb 16 '20 at 18:27
  • I have tried to add it over here not sure if it should be over there but it didn't work same error- 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); ps.setDate(3, java.sql.Date.valueOf("txt_AddDate.getDate()")); – Tomasz Barański Feb 16 '20 at 18:28
  • 1
    @TomaszBarański Please if you are facing other problem in the code. Raise a new question. I have already answered to the your question. – Anish B. Feb 16 '20 at 18:28