Storing a Date value with JDBC and Postgres

Clash Royale CLAN TAG#URR8PPPStoring a Date value with JDBC and Postgres
I have made a formulary with NetBeans using java.
In that formulary I have a field JDateChooser. The problem is when I try to insert that value in Postgres database:
public void insertarDatos(Usuario u){
try {
String sql = "INSERT INTO usuario( dni, nombre, apellidos, correo, "
+ "telefono, usuario, clave, fecha, foto)n"
+ "VALUES (?, ?, ?, ?, ?, ?, ?, '"+u.getFecha()+"', ?);";
PreparedStatement ps=con.con.prepareStatement(sql);
ps.setString(1, u.getDni());
ps.setString(2, u.getNombre());
ps.setString(3, u.getApellidos());
ps.setString(4, u.getCorreo());
ps.setString(5, u.getTelefono());
ps.setString(6, u.getUsuario());
ps.setString(7, u.getClave());
ps.setBinaryStream(8, u.getFis(), u.getLongitudBytes());
boolean ejecucion=con.ejecutarSQL(ps);
if(ejecucion==true){
JOptionPane.showMessageDialog(null, "usuario correctamente"
+ "registrado");
}else if(ejecucion==false){
JOptionPane.showMessageDialog(null, "error insertar usuario");
}
} catch (Exception e) {
System.out.println("error al insertar: " + e);
}
}
Then Java gives me an error like this:
error al ejecutar: org.postgresql.util.PSQLException: ERROR: la sintaxis de entrada no es válida para tipo date: «null»
However if I do the query in Postgres editor it works fine.
LocalDate
setObject()
java.sql.Date
setDate()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Do NOT pass dates (or numbers) as Strings. Pass an instance of
LocalDate(usingsetObject()) or at least an instance ofjava.sql.DateusingsetDate()– a_horse_with_no_name
3 mins ago