问题
I am implementing a password recovery function in a Java project based on Spring. This is my methodology for that
- User clicks forgot password link
- In the next screen, user enter his email address which has been used to register his/her account
- System generates a
UUID
token and it is saved in a database table together with the email address user entered. In addition the expiry time is saved in database as aTimestamp
value - An email including a link to reset password is sent to user. (UUID token is included in the e mail)
- When user clicks the link in the email, he/she is redirected to password reset page.
- In that page, user's email address is automatically set in to the text field by the system using the
UUID
token. Here I need to check whether the expiry time is due. For that I need to compare theTimestamp value
of current time with theTimestamp value
of the expiry time which is taken from the database usingUUID token.
I used this code segment to retrieve the timestamp
value of the expiry time.
@Override
public String checkValidityOfToken(UUID token) {
System.out.println("INFO:token in Login Dao Impl = "+token);
java.sql.Timestamp ex_time;
try{
String sql = "SELECT expiray_time FROM recover_password WHERE token = "+token;
ex_time = getJdbcTemplate().queryForObject(sql, java.sql.Timestamp);
System.out.println("INFO: first total = "+ex_time);
}catch(Exception exx){
System.out.println("error while taking saved time count for a matching token "+exx);
}
}
Although the java.sql.Timestamp
at queryForObject()
is suggested by Eclipse when I hit ctrl+space
, Eclipse shows an error there. Why is that.
What is the correct code segment for this task.
Then I used this,
String sql = "SELECT expiray_time FROM recover_password WHERE token = ?";
ex_time = getJdbcTemplate().queryForObject( sql, new Object[] { token }, java.sql.Timestamp);
In this case also, the java.sql.Timestamp
is not recognized. The class which includes above method extends `SimpleJdbcDaoSupport.
OR
Do I use a wrong way of checking the validity of the password reset link? If so What is a good way to implement it?
回答1:
The queryForObject(String, Class) method takes Class instance as its second parameter. Therefore the correct syntax is this:
getJdbcTemplate().queryForObject(sql, java.sql.Timestamp.class);
The code you are using is not valid Java code, which is the reason why it doesn't compile.
来源:https://stackoverflow.com/questions/27330856/retrieving-timestamp-value-from-mysql-database-using-spring-jdbc-template