[英]Converting date format of JXDatePicker to date type of mysql for storing it in database In java
我在Java應用程序中使用了JXDatePicker,我想將其值存儲在MySQL數據庫中。 要存儲的數據庫值是日期類型。
這是我的代碼:
final JXDatePicker dodc = new JXDatePicker(System.currentTimeMillis());
final SimpleDateFormat formater = new SimpleDateFormat("yyyy/MM/dd");
Date datedodc;
dodc.setBounds(250, 260, 150, 20);
content.add(dodc);
dodc.setForeground(BLACK);
dodc.setBackground(WHITE);
dodc.setFont(new Font("Arial", Font.BOLD, 12));
dodc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
datedodc = dodc.getDate();
System.out.println(formater.format(dodc.getDate()));
}
});
如果我打印此值,它將給出以下輸出:
Feb 05 00:00:00 IST 2013
我想將日期類型值存儲在mysql中,格式為: yyyy-mm-dd
我該怎么做?
當我嘗試存儲值時,Eclipse給了我以下錯誤:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Feb 05 00:00:00 IST 2013,Mon Feb 18 00:00:00 IST 2013,'Gujju Enterprise',4,'faaf' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2934)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1616)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1708)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3249)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1350)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1266)
at inwardRecord.save(inwardRecord.java:379)
at inwardRecord.actionPerformed(inwardRecord.java:304)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Feb 05 00:00:00 IST 2013,Mon Feb 18 00:00:00 IST 2013,'Gujju Enterprise',4,'faaf' at line 1
嘗試:
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
jXDatePicker1.setFormats(dateFormat);
DateFormat sysDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String date_to_store = sysDate.format(jXDatePicker1.getDate()).toString();
這將為您提供jXDatePicker1
(此處未包括聲明和jXDatePicker1
)輸入, date_to_store
輸入為格式正確的String date_to_store
輸出,以存儲在mySQL中。
嘗試這個:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
dateString = format.format(date.getDate());
試試這個方法:
int selectedYear = (int) datePicker.getModel().getYear();
int selectedMonth = (int) datePicker.getModel().getMonth();
int selectedDay = (int) datePicker.getModel().getDay();
String selectedDate = selectedYear +"-"+selectedMonth+"-"+selectedDay;
現在,您可以將selectedDate
存儲在數據庫中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.