Search code examples
javatimetimestampprepared-statement

pass LocalTime to PreparedStatement


I want to pass a java localtime to a PreparedStatement (java.sql.) like this:

ps.setTimestamp(id++, (localtime));

I do not know if setTimestamp is the right one or if I should use setTime or even another one. I did not find one which fits localtime directly so how can I convert it so that I can pass it to a Preparedstatement?


Solution

  • You can use java.time classes in PreparedStatement and ResultSet with the set/getObject methods since there are (currently) no specific methods for those types. The database driver needs to be recent enough to support those as well.

    ps.setObject(1, localtime);
    LocalTime loco = rs.getObject(1, LocalTime.class);
    

    The column type needs to be correct too of course.