1

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, ...);
andy007
  • 907
  • 1
  • 15
  • 41

4 Answers4

2

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

Mchl
  • 61,444
  • 9
  • 118
  • 120
2

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()));
Manoj Sharma
  • 596
  • 1
  • 6
  • 23
1

Do Like

preparedStatement.setTimestamp(5, new Timestamp(new Date().getTime()));
Siva Kumar
  • 1,983
  • 3
  • 14
  • 26
1

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

e.g.: 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 :)

Bhargav Modi
  • 2,605
  • 3
  • 29
  • 49