Results 1 to 6 of 6
Thread: Convert date to SQL
- 02-25-2012, 08:49 AM #1
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Convert date to SQL
I have a MySQL table with a date field.
This has the format "yyyy-mm-dd"
I want to convert this from a text field in my program.
I would like the users to type the date into the textfield in this format: "dd.mm.yyyy".
How can i convert this format to the MySQL format so I can insert it into the table?
- 02-25-2012, 09:18 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Convert date to SQL
Its simple easy if you are using PreparedStatements and SimpleDateFormat, example:
PreparedStatement date = connection.prepareStatement("INSERT INTO YOURTABLE (YOURCOLUMN) VALUES (?)");
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
date.setDate(1, new Date(formatter.parse("YOUR USER INPUT").getTime())); // java.sql.Date
date.executeUpdate();
- 02-25-2012, 09:42 AM #3
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Re: Convert date to SQL
Hi, thanks for the quick reply.
I think I am very close, but something is wrong:
Heres my code:
I have a error on the setDate line...Java Code:String firstname; firstname = textFirstName.getText(); String lastname; lastname = textLastName.getText(); String address; address = textAddress.getText(); String zip; zip = textZip.getText(); String city; city = textCity.getText(); String phone; phone = textPhone.getText(); String mobile; mobile = textMobile.getText(); String dato; dato = textDate.getText(); SimpleDateFormat sdf = new SimpleDateFormat("dd.mm.yyyy"); Connection connection = Connector.getConnection(); PreparedStatement ps = null; String query = "INSERT INTO ContactTable(FirstName, LastName, Address, Zip, City, Phone, MobilePhone, Date)" +" VALUES(?,?,?,?,?,?,?,?)"; try { ps = connection.prepareStatement(query); ps.setString(1, firstname); ps.setString(2, lastname); ps.setString(3, address); ps.setString(4, zip); ps.setString(5, city); ps.setString(6, phone); ps.setString(7, mobile); ps.setDate(8, new Date(sdf.parse(dato).getTime())); ps.executeUpdate();
- 02-25-2012, 09:47 AM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Convert date to SQL
What does the error message say?
Dou you have seen my comment at that line?
ps.setDate(8, new java.sql.Date(sdf.parse(dato).getTime()));
- 02-25-2012, 10:03 AM #5
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Re: Convert date to SQL
Thanks for the help, I got it to work!
.gif)
And I had written a little typeo in the dateformat:Java Code:try { ps.setDate(8, new java.sql.Date(sdf.parse(dato).getTime())); } catch (ParseException ex) { Logger.getLogger(ContactForm.class.getName()).log(Level.SEVERE, null, ex); } ps.executeUpdate();
My code:
Corrected to this:Java Code:SimpleDateFormat sdf = new SimpleDateFormat("dd.mm.yyyy");
Works perfectly!Java Code:SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
- 02-25-2012, 10:16 AM #6
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Similar Threads
-
Convert String to Date?
By bochra in forum New To JavaReplies: 4Last Post: 11-15-2010, 10:41 AM -
convert String date to Date
By computerbum in forum New To JavaReplies: 7Last Post: 09-18-2010, 03:26 PM -
how we can convert string to date?
By cowboy2010 in forum New To JavaReplies: 4Last Post: 08-25-2010, 08:41 PM -
Convert date from database
By karq in forum New To JavaReplies: 5Last Post: 07-13-2010, 08:44 AM -
how to convert date format
By saran123 in forum New To JavaReplies: 5Last Post: 10-16-2008, 06:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks