0

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 been trying to get this working but am having no luck. I also get the helpful error message when I go to compile the class.

"Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method setDate(int, Date) in the type PreparedStatement is not applicable for the arguments (int, Date)"

Eh WTF?

Here is the offending code.

for(int i = 0; i < flights.size(); i++){

    String[] details = flight[i].toString().split(":"); 
    DateFormat formatter ; 
    formatter = new SimpleDateFormat("ddMMyyyy");
    Date date = formatter.parse(details[1]); 

    PreparedStatement pstmt = conn.prepareStatement(insertsql);
    pstmt.setString(1, details[0]);
    pstmt.setDate(2, date);
    pstmt.setString(3, details[2] + "00");
    pstmt.setString(4, details[3]);
    pstmt.setString(5, details[4]);
    pstmt.setString(6, details[5]);
    pstmt.setString(7, details[6]);
    pstmt.setString(8, details[7]);
    pstmt.setString(9, details[8]);
    pstmt.executeUpdate();

}
5
  • 2
    In my experience, try not to create DateFormat objects on a regular basis. They're really expensive time-wise to create. Try creating it outside your loop and reusing it. You'll save a lot of time. Commented Aug 26, 2009 at 17:15
  • @BrentNash Your comment is correct. But let's add a reminder than [SimpleDateFormat] is not thread-safe. So be sure you do create fresh instance in the context of a new thread. Or, even better, use the new java.time package rather than the old date-time classes. Commented Jul 3, 2015 at 19:17
  • @BasilBourque That original comment was a long time ago. If you asked me today, I would just tell everyone to use JodaTime for everything. Commented Jul 3, 2015 at 20:57
  • @BrentNash Sure, I understand. I'm really speaking to the 8,000 other people reading this, not you. Commented Jul 3, 2015 at 23:33
  • The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. Also, the Home Page of Joda-Time reads: Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project. Check this answer and this answer to learn how to use java.time API with JDBC. Commented Jun 12, 2021 at 19:54

3 Answers 3

8

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...)

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

1

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"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.