2

I have a form which captures a date that the user input in a JFormattedTextField. Then the Date need to be stored in a database (postgresql) using PreparedStatement. I am having error messages at the line pStat.setDate(4, dob);.

 Date dob = (Date)ftxtDOB.getValue();
       String add = txtAddress.getText();
       String country = txtCountry.getText();
       try {
           Class.forName("org.postgresql.Driver");
           conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/postgres", "postgres","cisco");

           pStat = conn.prepareStatement("INSERT INTO customer_info VALUES (?,?,?,?,?,?)");
           pStat.setString(1, id);
           pStat.setString(2, surname);
           pStat.setString(3, fName);
           pStat.setDate(4, dob);
       }catch(Exception e){

       }

Edit: I have this error message from the compiler.

no suitable method found for setDate(int,java.util.Date)
    method java.sql.PreparedStatement.setDate(int,java.sql.Date,java.util.Calendar) is not applicable
      (actual and formal argument lists differ in length)
    method java.sql.PreparedStatement.setDate(int,java.sql.Date) is not applicable
      (actual argument java.util.Date cannot be converted to java.sql.Date by method invocation conversion)

Edit: SOLVED, I used:

    pStat.setDate(4, new java.sql.Date(dob.getTime()));
2
  • And what error messages might that be? You should really fill out that catch clause... at least put a e.printStackTrace() in there. Commented Feb 27, 2012 at 17:11
  • And the error is? Have you read the error message? It's supposed to help you, and is not some random gibberish that can be ignored. NEVER ignore exceptions. Commented Feb 27, 2012 at 17:12

1 Answer 1

7

What error message?

Guessing that it's actually a compiler error message, are you sure you are using java.sql.Date and not java.util.Date?

Edit: As you edited question, yes you will need new java.sql.Date(date.getTime()) or something (data handling in Java is a mess! (at the moment)).

Sign up to request clarification or add additional context in comments.

1 Comment

I used pStat.setDate(4, new java.sql.Date(dob.getTime())); and it worked. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.