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, ...);
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()));
Do Like
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
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 :)