0

Quiero insertar desde java en postgresql. Al traer (y castear) una java.util.Date a java.sql.Date, funciona bien, pero a la hora de insertar me marca esto:

ERROR: la columna «fechab1» es de tipo date pero la expresión es de tipo character varying Hint: Necesitará reescribir la expresión o aplicarle una conversión de tipo.

Sin embargo, y como dije, ya había aplicado la conversión de tipo de datos. ¿Qué podría tener mal? dejo un ejemplo de como tengo mis conversiones:


Codigo con 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

Conversor:

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;

}

1 respuesta 1

0

Hola una vez que tengas el formato deseado de tu fecha lo debes asignar así en el ps.

ps.setDate(1,java.sql.Date.valueOf(castFechas(dts.getFecha1().getTime()))); en tu caso.

te facilito un método de ejemplo,en donde se realiza una búsqueda por fecha_registro y si existen resultados lo carga en una tabla.

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());

    }

}
1
  • Gracias, ya habia encontrado que era el problema jaja, era un dato que estaba mal, pero igual se aagradece
    – J.Carlos
    el 4 ene. 2019 a las 23:01

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.