Discussions

General J2EE: Inserting java.sql.Date type variable to database

  1. Inserting java.sql.Date type variable to database (4 messages)

    Hi to all,
    I have a PostgreSql database table which contains field of type date .I want to insert the date in the format of “dd-MMM-YYYY”. I am using the preparedStatement to insert the date field as well as other fields to the table.Please help me.
    Regards,
     Hyson
  2. you can insert the date as

    pstmt.setDate(new java.util.Date(person.getBirthDate().getTime()));

    where, person is a reference to a class Person.
    The date will be inserted in yyyy-mm-dd format.

    you can use the sql date formatting options for outputting it the way u like
  3. Thanks umar ali for your reply and help. I have tried that way but there is no date formating functions are avail for the class java.sql.Date which are compactable with the pst.setDate() method (where pst is the reference to the type java.sql.PreparedStatement).The pstmt.setDate(1,date) method only accept java.sql.Date type arguments.So I didn’t got the expected result
  4. The date formatting functions are not in java.sql. package but your database vendors provide the formatting functions for outputting the date. For ex.

    SELECT BIRTHDATE(TO_DATE("DD-MM-YYYY")) FROM USER;

    Check ur database vendor manual for details
  5. thanks[ Go to top ]

    thanks for your reply .I will try