-2

I have one Jframe with 8 JComboboxes and textfield...when i pressed submit button got Datatype mismatch error

try
                {


                     String Query="replace into enquiry (`Enquiry No`,`Client`,`User`,`Purchase`,`Sales`,"
                            + "`Date`,`Technical Data`,"
                            + "`Product`,`Remarks`,`Amount`,`Userboss`,`PurchaserBoss`,`SalesBoss`,`month`,`Pagency`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

                    PreparedStatement pst=conn.prepareStatement(Query);

                    pst.setString(1, textField.getText());
                    String c=client.getSelectedItem().toString();
                    pst.setString(2, c);
                    String u=user.getSelectedItem().toString();
                    pst.setString(3, u);
                    String p=purchase.getSelectedItem().toString();
                    pst.setString(4,p);
                    String se=sengg.getSelectedItem().toString();
                    pst.setString(5, se);
                    pst.setString(6, ed.getText());
                    pst.setString(7, td.getText());
                    String pd=prod.getSelectedItem().toString();
                    pst.setString(8, pd);
                    pst.setString(9, remark.getText());
                    pst.setString(10, amt.getText());
                    String ub=uboss.getSelectedItem().toString();
                    pst.setString(11, ub);
                    String pb=pboss.getSelectedItem().toString();
                    pst.setString(12, pb);
                    String sb=sboss.getSelectedItem().toString();
                    pst.setString(13, sb);
                    pst.setString(14, qmonth.getText());
                    String pa1=pa.getSelectedItem().toString();
                    pst.setString(15, pa1);
                    pst.execute();


                }

how to solve this error?

SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
G.S
  • 21
  • 1
  • 9
  • on which line you get the error? – Tushar Sharma Mar 14 '17 at 07:26
  • 1
    post stack trace – SatyaTNV Mar 14 '17 at 07:26
  • i guess the columns `Date`,`Technical Data` are date columns, so you have to set Dates here not Strings – Jens Mar 14 '17 at 07:28
  • @Tushar sir, pst.execute(); – G.S Mar 14 '17 at 07:28
  • @Jens.. pst.setString(6, ed.getText());..is for date..and here I set today's date in another method..here just want to submit this in table – G.S Mar 14 '17 at 07:30
  • Well, the error tell you that you pass a `String` to the field `Technical Data` using `setString` but this is a `Date` so you new to pass a `Date` instance with `setDate` – AxelH Mar 14 '17 at 07:31
  • @G.S For that you have to use `setDate()` and not `setString()` – Jens Mar 14 '17 at 07:32
  • public void showDate() { Calendar c=new GregorianCalendar(); int day=c.get(Calendar.DAY_OF_MONTH); int month=c.get(Calendar.MONTH); int year=c.get(Calendar.YEAR); ed.setText(+day+"/"+(month+1)+"/"+year); }................................I used this for today's date – G.S Mar 14 '17 at 07:38
  • I tried setDate() but it gives error... – G.S Mar 14 '17 at 07:40
  • @G.S see the edit about your comment. you don't need to create the String if you simply want the current Date – AxelH Mar 14 '17 at 07:44

1 Answers1

0

You are passing a String to a Date column using PreparedStatement.setString(int index, String x).

You have a PreparedStatement.setDate(int index, java.sql.Date date) method for Date column.

Simply parse the String you have and pass this java.util.Date instance to the method.

PS : java.sql.Date is a subclass of java.util.Date.

EDIT : In a comment, you said you simply get the current Date with new GregorianCalendar() to build the String. So you can simply pass new Date() to get the current date.

java.util.Date()

Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

AxelH
  • 14,325
  • 2
  • 25
  • 55