I want to insert from java in postgresql.
By bringing (and casting) a java.util.Date
to java.sql.Date
, it works fine, but at the time of inserting it, it marks me this:
ERROR: the column "fechab1" is of type date but the expression is type character varying Hint: You will need to rewrite the expression or Apply a type conversion.
However, as I said, I had already applied the data type conversion. What could I have wrong? I leave an example of how I have my conversions:
Code with PreparedStatament
ps=con.prepareStatament("insert into tabla (fecha1, fecha2) values (?,?)");
ps.setDate(1,castFechas(dts.getFecha1().getTime()));
ps.setDate(2,castFechas(dts.getFecha2().getTime()));
rs=ps.executeUpdate();
//dts.getFecha1() es un Java.util.Calendar
Converter:
private Date castFechas(java.util.Date fecha) {
String fechastr=sf.format(fecha.getTime());
System.out.println("fechastr: "+fechastr);
java.util.Date fechaJU=null;
java.sql.Date fechasql=null;
try {
fechaJU=sf.parse(fechastr);
fechasql= new java.sql.Date(fechaJU.getTime());
} catch (ParseException e) {
System.out.println("error: "+e.getMessage());
e.printStackTrace();
}
return fechasql;
}