简体   繁体   中英

PreparedStatement and CURDATE() MySQL function

What is the correct way to use CURDATE() mysql function in PreparedStatements? Table column is DATE type and inserted value in table must be like 1988-07-29.

ps.setDate(5, ...);

There is none. You either create current date in Java or prepare the statement with CURDATE()

Do Like:

SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
String fd = f.format(new Date());
Date date = f.parse(fd);
preparedStatement.setTimestamp(index, new Timestamp(date.getTime()));

喜欢

preparedStatement.setTimestamp(5, new Timestamp(new Date().getTime()));

You can do it in two ways one is from java as below Using java.sql.Date

eg: Using java.sql.Date :

java.sql.Date date = java.sql.Date.valueOf("2013-09-04");

If you want to insert the current date:

ps.setDate(2, new java.sql.Date(System.currentTimeMillis()));

Using java.sql.Timestamp :

ps.setTimestamp(2, new java.sql.Timestamp(System.currentTimeMillis()));

Another way is from SQL through database support operations like now() for MySQL or current_timestamp() for PostgreSQL etc just have a look on below example

String sql = "INSERT INTO user (email, creationdate) VALUES (?, now())";//for MySQL

for more details Use this link (one of my favourite author).

Happy to Help you :)

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