Storing a Date value with JDBC and Postgres

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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





Do NOT pass dates (or numbers) as Strings. Pass an instance of LocalDate (using setObject()) or at least an instance of java.sql.Date using setDate()
– a_horse_with_no_name
3 mins ago




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.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived