How to insert a LocalDateTime (Date with time) into a MySyQL database

0

I have a database created with phpMyAdmin, and in a "reservations" table I have a field that asks me for the specific date and time of a reservation. Now the problem is in which at the time of inserting the reservation, I do not take the date, when I do and I get an error:

PreparedStatement ps = conn.prepareStatement("INSERT INTO reserva (nombre, dni, fecha, id_mesa, vigente) VALUES (?, ?, ?, ?, 1)", Statement.RETURN_GENERATED_KEYS);
            LocalDateTime ldt = reserva.getFecha();
            ps.setDate(3, ldt);

He does not let me set it up like this

PreparedStatement ps = conn.prepareStatement("INSERT INTO reserva (nombre, dni, fecha, id_mesa, vigente) VALUES (?, ?, ?, ?, 1)", Statement.RETURN_GENERATED_KEYS);
        LocalDateTime ldt = reserva.getFecha();
        ps.setString(3, ldt.getYear()+"-"+ldt.getDayOfMonth()+"-"+ldt.getDayOfMonth()+" "+ldt.getHour()+":"+ldt.getMinute()+":00");

With the rest of the data is perfect, how can I make a specific date with the database?

Greetings and thanks.

    
asked by Pablo Palma 26.10.2018 в 17:32
source

1 answer

0

You can try the following, I hope it helps:

// LocalDateTime fechaEspecifica = LocalDateTime.of(AÑO, MES, DIA, HORA, MINUTO, SEGUNDO);
LocalDateTime fechaEspecifica = LocalDateTime.of(2014, Month.JANUARY, 1, 10, 10, 30);
    
answered by 29.10.2018 в 22:40