简体   繁体   中英

Setting a particular date into a prepared statement

I'm trying to insert any given date into my prepared statement in this format(eg 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?

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

Use SimpleDateFormat :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM