Postgresql -en Date but the expression is character variable- error inserting?

0

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;

}
    
asked by J.Carlos 31.12.2018 в 18:41
source

1 answer

0

Hello once you have the desired format for your date, you must assign it in ps.

ps.setDate (1, java.sql.Date.valueOf (castFechas (dts.getDate1 (). getTime ()))); in your case.

I give you an example method, where a search is done by registration_date and if there are results, it loads it into a table.

public void FiltrarVenta(){

dtm.setRowCount(0);
Date hoy= Reportepordia.hoy.getDate();
SimpleDateFormat formato = new SimpleDateFormat("yyyy-M-d");
String fechaSelec = formato.format(hoy);

Sql="SELECT f.n_factura_v, f.fecha_registro,\n" +
"d.catidad,d.preciou,d.total,\n" +
"p.nombre\n" +
"  FROM factura_venta f\n" +
"  inner join detalle_venta as d on d.n_factura_v=f.n_factura_v\n" +
"  inner join producto as p on p.id_producto=d.id_producto\n" +
"  where estado_factura and fecha_registro=?";
  boolean bandera=false;
    try {
        pst=ConexionDB.getDBcon().prepareStatement(Sql);
        pst.setDate(1,java.sql.Date.valueOf(fechaSelec));
        rst=pst.executeQuery();
        while (rst.next()) {                
        dtm.addRow(new Object[]{
         rst.getInt("n_factura_v"),rst.getString("nombre"),rst.getInt("preciou"),rst.getInt("catidad"),rst.getInt("total")
                ,rst.getDate("fecha_registro")
        });
       bandera=true;
        }
         SumarTotalVenta(fechaSelec);

    if(bandera==false){   
 JOptionPane.showMessageDialog(null,"No Existen Registros","Atencion",JOptionPane.ERROR_MESSAGE);               
}

    } catch (SQLException e) {
        System.err.println("ERROR AL RECUPERAR " +e.getMessage());

    }

}
    
answered by 03.01.2019 в 17:28