I am trying to program a database application with java & PostgreSQL. I have some rows with date data type. But i cant add any entries to the database with this code :
Date aDate = null;
aDate.setYear(1990);
aDate.setDate(01);
aDate.setMonth(05);
preparedStatement prep = connection.prepareStatement("insert
into exampletable values (?,?);");
prep.setDate(1, (java.sql.Date) aDate);
prep.setDate(2, (java.sql.Date) aDate);
How can i add a date in a postgreSQL row with queries in java?
It’s not clear whether or not this is your only problem, but this code is almost certainly not what you want:
NullPointerExceptionbecause you’re trying to dereferencenulljava.util.Dateis 1900-based for years)Dateis 0-based for monthsaDatetojava.sql.Datebut there’s no sign that it is ajava.sql.DateI would suggest:
java.util.Calendarjava.sql.Datelater on.For example: