Java: creating a date object from a string and inserting into MySQL

后端 未结 3 1116
花落未央
花落未央 2021-01-28 18:03

Anytime I have to handle dates/times in java it makes me sad

I\'m trying to parse a string and turn it into a date object to insert in a preparepared statement. I\'ve b

相关标签:
3条回答
  • 2021-01-28 18:05

    String to MySQL Date/Time

    import java.sql.Date;
    import java.sql.Time;
    
     statement.setDate(4, Date.valueOf("2009-08-26"));
     statement.setTime(5, Time.valueOf("12:04:08"));
    
    0 讨论(0)
  • 2021-01-28 18:13

    My guess is that you mixed java.util.Date and java.sql.Date ...

    0 讨论(0)
  • 2021-01-28 18:16

    PreparedStatement.setDate takes a java.sql.Date, not a java.util.Date.

    (Out of interest, how come you're not actually seeing this as a compile-time error? Your life will become a lot easier if you can resolve compilation failures without having to get to that point in a test run...)

    0 讨论(0)
提交回复
热议问题