aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Converting date format of JXDatePicker to date type of mysql for storing it in database In java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Converting date format of JXDatePicker to date type of mysql for storing it in database In java" Watch "Converting date format of JXDatePicker to date type of mysql for storing it in database In java" New topic
Author

Converting date format of JXDatePicker to date type of mysql for storing it in database In java

Mayur Saparia
Greenhorn

Joined: Feb 13, 2013
Posts: 19

I have used JXDatePicker in my java application and I want to store its value in a MySQL database. The database value to be stored is of date type.


If I print this value it gives the following output:

Feb 05 00:00:00 IST 2013

I want to store the date type value in mysql with the format: `yyyy-mm-dd`
How can I do it?

Eclipse gives me the following error when I try to store the values:

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


Thanks In advance

Regards
Mayur.
rammohan banegire
Greenhorn

Joined: Sep 17, 2013
Posts: 1
package swings;

import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jdesktop.swingx.JXDatePicker;

public class DatePicker extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
final JXDatePicker dodc = new JXDatePicker();
final SimpleDateFormat formater = new SimpleDateFormat("yyyy/MM/dd");
Date datedodc;
void datePicker(){


Container content = getContentPane();
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()));
}
});
}
public static void main(String[] args) {
DatePicker p = new DatePicker();
p.datePicker();
p.setLocation(200, 75);
p.setResizable(false);
p.setSize(800,600);
p.setVisible(true);
}

}



here i am getting output,whatever date we are selecting in the in the JXDatefield. like 2013/09/17. and for your mysql error check the syntax of mysql in your java application.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Converting date format of JXDatePicker to date type of mysql for storing it in database In java