Sundar Ganapathy
Sundar Ganapathy

Reputation: 65

Accepting a date from user in java and writing it to postgresql

I need to write to a column of data type Date in postgresql from a java application. In which datatype should I accept the input so that I can use it to insert into Postgresql?

Upvotes: 0

Views: 123

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 338564

LocalDate

As of JDBC 4.2, the modern way to store a date-only value without time-of-day and without time zone is the LocalDate class.

LocalDate ld = LocalDate.of( 2019 , Month.JANUARY , 23 ) ;

This maps to a DATE type in Postgres.

myPreparedStatement.setObject( … , ld ) ;

Retrieval.

LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;

The legacy class java.sql.Date class is now legacy, and should never be used because of its severe design flaws.

Table of date-time types in Java (both legacy & modern) and in standard SQL.

Upvotes: 3

Related Questions