Question

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()));
Was it helpful?

Solution

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)).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top