java - java中将JXDatePicker的日期格式转换为mysql的日期类型存储到数据库中

标签 java jdbc

我在我的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(此处不包括声明和初始化)输入,作为格式正确的字符串date_to_store输出以存储在mySQL中。

关于java - java中将JXDatePicker的日期格式转换为mysql的日期类型存储到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15077455/

相关文章:

Java:BufferedReader readLine() 中的 IO 异常有什么用?

java - 如何删除java borderlayout中南北面板之间的间隙

java - 在 Eclipse 中开发时无法从 Google App Engine 连接到 Google Cloud SQL 实例

分配给泛型类型的 Java 原始类型值运行时 getClss() 方法错误

java - 来自接收到的字符串的android UTF8编码

java - 为什么我收到 : [Oracle][ODBC][Ora]ORA-00904: invalid identifier

java - 用户 'root' 的 JDBC 访问被拒绝 @'localhost'

java - 在 cassandra 中设置 TTL 后无法运行选择查询

java - 在所有可达顶点中找到最有值(value)的顶点

java - 如何使用 Java 通过 LAN 连接到 Access 数据库?