Problem getting date with jDateChooser

0

I have a jDateChooser from which I want to obtain the date but it is impossible for me to recover it I have the following fragment: Date date = jDateChooser_caducidad.getDate (); in theory it should work but what it collects from me from jDateChooser is a null and for more than I look for I do not find soucion, someone has passed it and knows how to solve it. Thank you The code is as follows

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.Date;

 // metodo para añadir los articulos junto con los datos
    public void insertaAriticulos() throws ParseException {

        //Cargamos conexión
        Conexion conectar = new Conexion();
        //Pasamos el objeto Connection 

        Connection connection = conectar.getConection();
        //Creamos un objeto de la clase Statement
        PreparedStatement pstm;

        //Creamos la sentencia sql.
        String sql = "INSERT INTO articulos (nombre_articulo, categoria, fecha_caducidad) VALUES (?,?,?)";

        // capturamos los datos en variables
        String nombre = jTextField_nombre.getText();
        String categoria = jComboBox_categoria.getSelectedItem().toString();
        // pasos para obtener la fecha actual
         fecha1 = jDateChooser_caducidad.getDate();
        //Ejecutamos la sentencia sql
        try {
            //Establecemos la comunicación con la BD
            pstm = connection.prepareStatement(sql);
            //Le pasamos al objeto de ResultSet el resultado de ejecutar la sentencia sql
            pstm.setString(1, nombre);
            pstm.setString(2, categoria);
            pstm.setDate(3, (java.sql.Date) fecha1);

            //hacemos la actualizacion
            int actualizacion = pstm.executeUpdate();
            // comprobamos que se guardan correctamente
            if (actualizacion > 0) {
                JOptionPane.showMessageDialog(null, "Los datos se guardaron correctamente");
                // vaciamos los campos
                jTextField_nombre.setText("");
                // pasamos el foco al nombre de nuevo
                jTextField_nombre.requestFocus();

            }

            //Cerramos las conexiones.
            pstm.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error al guardar.");
        }
    }
    
asked by jose22 02.11.2018 в 11:43
source

0 answers