user2297666
user2297666

Reputation: 321

Setting a particular date into a prepared statement

I'm trying to insert any given date into my prepared statement in this format(e.g. 11/08/1989, 04/15/1955)

Heres my code:

pstmnt = conn.prepareStatement("INSERT INTO user_info VALUES (?,?,?,?,?,?)");

pstmnt.setInt(1, max_id);
            pstmnt.setString(2, f_name);
            pstmnt.setString(3, l_name);
            pstmnt.setString(4, email);
            pstmnt.setString(5, username);
            pstmnt.setDate(6, ?);
            pstmnt.addBatch();

I'm not sure how to accomplish this, given a lot of the Date methods are deprecated.

Any ideas?

Upvotes: 1

Views: 588

Answers (2)

Jeremiah Orr
Jeremiah Orr

Reputation: 2630

Use SimpleDateFormat:

pstmt.setDate(6, new java.sql.Date(
    new SimpleDateFormat("MM/dd/yyyy").parse("11/08/1989")));

Upvotes: 0

AllTooSir
AllTooSir

Reputation: 49372

You need to pass the java.sql.Date object to the setDate() method .

pstmt.setDate(6, 
          new java.sql.Date(( new
                 SimpleDateFormat("MM/dd/yyyy").parse("11/08/1989")).getTime()));

Upvotes: 3

Related Questions