Show a date that entered in JText from Java and that is stored in SQL

0

I would not be able to enter a date of birth in my program to fill out a file of a client and save it in the sql database from xampp, I could do it as a string but obviously it does not take the bars "/ " I want to directly load the date and save it in SQL. My code where I load the data are the following:

public void actionPerformed(ActionEvent arg0) {             
    try {                   
            Class.forName("com.mysql.jdbc.Driver");
            java.sql.Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/tienda","root","");                         
            String nombre = textField_1.getText();
            int precio = Integer.parseInt(textField_2.getText());
            String descripcion = textArea.getText();

            JTextField textField_3 = new JTextField();
            SimpleDateFormat form = new SimpleDateFormat("dd/MM/yyyy");                 
            String query = "INSERT INTO productos (nombre,precio,descripcion) values('"+nombre+"','"+precio+"','"+descripcion+"')";

            Statement s = conexion.createStatement();
            s.executeUpdate(query);

            JOptionPane.showMessageDialog(null, "Nombre agregado");

        } catch (Exception e) {                 
            JOptionPane.showMessageDialog(null, "Error");
            // TODO: handle exception
        }           
    }
});
    
asked by Matias 06.10.2018 в 18:53
source

1 answer

0

the field in the table you could leave it as DATETIME, and in Java:

SimpleDateFormat format = new SimpleDateFormat ("dd 'from' MMMM 'from' yyyy", new Locale ("ES")); java.util.Date currentDate = new Date (); String date = format.format (currentDate).

    
answered by 06.10.2018 в 21:19