'get a date using a JDateChooser and save it into mysql database

design view of the table <code>LETTER</code>

I need to read a date from a JDateChooser and save it into a MYSQL database table using the ActionPerformed method of a JButton.

There are three JDateChoosers called LETTER_DATE , RECEIVED_DATE , SENDING_DATE in this JInternalFrameForm called LETTER .

Below is the code of the ActionPerformed method of the JButton SAVE which will save the data into the MYSQL table.

private void SAVEActionPerformed(java.awt.event.ActionEvent evt) {                                     
                   try{
                   Statement s=DB.getConnection().createStatement();        

                    s.executeUpdate("insert into LETTER values('"+LETTER_ID.getText()+"','"+LETTER_CATEGORY.getSelectedItem()+"','"+LETTER_DATE.getDate()+"','"+HEADING.getText()+"','"+SECTION_ID.getSelectedItem()+"','"+SUBJECT_ID.getSelectedItem()+"','"+INSTITUTE_ID.getSelectedItem()+"','"+NEW_INSTITUTE.getText()+"','"+SENDERS_ID.getSelectedItem()+"','"+NEW_SENDER.getText()+"','"+RECEIVED_DATE.getDate()+"','"+RECEIVERS_ID.getSelectedItem()+"','"+NEW_RECEIVER.getText()+"','"+SENDING_DATE.getDate()+"')");
                    JOptionPane.showMessageDialog(this, "Saved");
                }
                catch(Exception ex){
                    ex.printStackTrace();
        }
            } 

When I clicked the JButton to save the data, it gives the following stacktrace.

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: 'Mon Dec 04 14:45:36 PST 2017' for column 'LETTER_DATE' at row 1
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4118)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2788)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1816)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1730)
    at Viewer.LETTER.SAVEActionPerformed(LETTER.java:280)
    at Viewer.LETTER.access$000(LETTER.java:16)
    at Viewer.LETTER$1.actionPerformed(LETTER.java:99)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6516)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6281)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4872)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
    at java.awt.EventQueue.access$300(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:706)
    at java.awt.EventQueue$3.run(EventQueue.java:704)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.awt.EventQueue$4.run(EventQueue.java:718)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Can anyone please help me to figure out the problem here and correct the datetime value for column ['LETTER_DATE'][1] at row 1?



Solution 1:[1]

Well, you should be using getDateFormatString() instead of getDate(). For instance:

s.executeUpdate("insert into LETTER values('"+LETTER_ID.getText()+"','"+LETTER_CATEGORY.getSelectedItem()+"','"+LETTER_DATE.getDateFormatString()+"','"+HEADING.getText()+"','"+SECTION_ID.getSelectedItem()+"','"+SUBJECT_ID.getSelectedItem()+"','"+INSTITUTE_ID.getSelectedItem()+"','"+NEW_INSTITUTE.getText()+"','"+SENDERS_ID.getSelectedItem()+"','"+NEW_SENDER.getText()+"','"+RECEIVED_DATE.getDateFormatString()+"','"+RECEIVERS_ID.getSelectedItem()+"','"+NEW_RECEIVER.getText()+"','"+SENDING_DATE.getDateFormatString()+"')");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hassan Suriya