Store a date in java with the SET and GETTERS methods

1

my question is how can I store a date in a JFrame having a jDateChooser to select that date, I use the SET and GETTERS methods. My attribute of class Producto is Date fechaVencimiento which has the methods

public Date getFechaVencimiento() {
        return fechaVencimiento;
    }

    public void setFechaVencimiento(Date fechaVencimiento) {
        this.fechaVencimiento = fechaVencimiento;
    }

In my MySQL database, the date field is of type DATE. I read that I need to convert from java.util.Date to java.sql.Date . which I do in the following way:

java.sql.Date fecha_Vencimiento = new java.sql.Date(fechaVencimiento.getTime());

and in my save method I pass it as a parameter:

    public void guardar() {
        try {
            Connection miComando = AdministradorConfiguracion.obtenerComandoMySql();
            CallableStatement insertarProducto = miComando.prepareCall("call insertar_Producto(?,?,?,?,?,?,?,?,?,?)");
            insertarProducto.setString(1, producto);
            insertarProducto.setDouble(2, cantidad);
            insertarProducto.setDouble(3, precioCosto);
            insertarProducto.setDouble(4, precioVenta);
            insertarProducto.setString(5, marca);
            insertarProducto.setString(6, descripcion);
            insertarProducto.setInt(7, idcategoria);
            insertarProducto.setInt(8, idproveedor);
            >>insertarProducto.setDate(9, fecha_Vencimiento);<<
            if (this.image != null) {
                insertarProducto.setBinaryStream(10, image);
            }

            if (this.idproducto != 0) {
                CallableStatement modificarProducto = miComando.prepareCall("call modificar_producto(?,?,?,?,?,?,?,?,?,?,?)");
                modificarProducto.setInt(1, idproducto);
                modificarProducto.setString(2, producto);
                modificarProducto.setDouble(3, cantidad);
                modificarProducto.setDouble(4, precioCosto);
                modificarProducto.setDouble(5, precioVenta);
                modificarProducto.setString(6, marca);
                modificarProducto.setString(7, descripcion);
                modificarProducto.setInt(8, idcategoria);
                modificarProducto.setInt(9, idproveedor);
                >>modificarProducto.setDate(10, fecha_Vencimiento);<<
                if (this.image != null) {
                    insertarProducto.setBinaryStream(11, image);
                }

                if (modificarProducto.executeUpdate() == 1) {
                    JOptionPane.showMessageDialog(null, "Producto modificado correctamente.", "Producto modificado", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "Ha ocurrido un error al modificar el producto.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            } else {
                this.idproducto = insertarProducto.executeUpdate();
                if (idproducto > 0) {
                    JOptionPane.showMessageDialog(null, "Producto guardado correctamente.", "Producto insertado", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "Ha ocurrido un error al guardar producto.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Error al intentar almacenar el producto:\n"
                    + e, "Error en la operación", JOptionPane.ERROR_MESSAGE);
        }
    }

In my JFrame on my save button I have the following:

producto.setProducto(jtxtProducto.getText());
        producto.setCantidad(Double.valueOf(jtxtCantidad.getText()));
        producto.setPrecioCosto(Double.valueOf(jtxtPrecioCosto.getText()));
        producto.setPrecioVenta(Double.valueOf(jtxtPrecioVenta.getText()));
        producto.setMarca(jtxtMarca.getText());
        producto.setDescripcion(jtxtDescripcion.getText());
        Categoria cboCat = (Categoria)jcboCategoria.getSelectedItem();
        int idCat = cboCat.idcategoria;
        producto.setIdcategoria(idCat);
        Proveedor cboProv = (Proveedor)jcboProveedores.getSelectedItem();
        int idProv = cboProv.idproveedor;
        producto.setIdproveedor(idProv);
        producto.setFechaVencimiento(jdcFechaVencimiento.getDate());

But when I run my JFrame I throw the exception java.lang.NullPointerException on line java.sql.Date fecha_Vencimiento = new java.sql.Date(fechaVencimiento.getTime()); of my class Producto

I would greatly appreciate your help. Thanks again.

    
asked by Gerardo Ferreyra 29.05.2017 в 04:24
source

1 answer

1

The solution was very simple, which was the following: Declare my attribute of type class String that would be private String fechaVencimiento; with its method get and set

public String getFechaVencimiento() {
        return fechaVencimiento;
    }

    public void setFechaVencimiento(String fechaVencimiento) {
        this.fechaVencimiento = fechaVencimiento;
}

Then in my save () method I made insertarProducto.setString(9, fechaVencimiento); in my JFrame use a jDateChooser and inside my save button place the following :

I got the year

int año = jdcFechaVencimiento.getCalendar().get(Calendar.YEAR);

I got the month

int mes = jdcFechaVencimiento.getCalendar().get(Calendar.MONTH);

I got the day

int dia = jdcFechaVencimiento.getCalendar().get(Calendar.DAY_OF_MONTH);

Then convert to String the entire year, month and day variables to String

String fecha = año+"/"+mes+"/"+dia;

Finally I call my set method and I pass the String date.

producto.setFechaVencimiento(fecha);

I hope someone will find it useful. Thanks.

    
answered by 04.06.2017 в 21:07