Results 1 to 6 of 6
  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default 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?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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();

  3. #3
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Convert date to SQL

    Hi, thanks for the quick reply.
    I think I am very close, but something is wrong:

    Heres my code:
    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();
    I have a error on the setDate line...

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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()));

  5. #5
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Convert date to SQL

    Thanks for the help, I got it to work!
    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();
    And I had written a little typeo in the dateformat:
    My code:
    Java Code:
    SimpleDateFormat sdf = new SimpleDateFormat("dd.mm.yyyy");
    Corrected to this:
    Java Code:
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
    Works perfectly!

  6. #6
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Convert date to SQL

    New question: I also wants to display this date. How is that code?

Similar Threads

  1. Convert String to Date?
    By bochra in forum New To Java
    Replies: 4
    Last Post: 11-15-2010, 10:41 AM
  2. convert String date to Date
    By computerbum in forum New To Java
    Replies: 7
    Last Post: 09-18-2010, 03:26 PM
  3. how we can convert string to date?
    By cowboy2010 in forum New To Java
    Replies: 4
    Last Post: 08-25-2010, 08:41 PM
  4. Convert date from database
    By karq in forum New To Java
    Replies: 5
    Last Post: 07-13-2010, 08:44 AM
  5. how to convert date format
    By saran123 in forum New To Java
    Replies: 5
    Last Post: 10-16-2008, 06:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •