使用Spring JDBC模板从mysql数据库中获取Timestamp值 [英] Retrieving Timestamp value from mysql database using Spring JDBC template
问题描述
我正在基于Spring的Java项目中实现密码恢复功能.这是我的方法
I am implementing a password recovery function in a Java project based on Spring. This is my methodology for that
- 用户点击了忘记密码的链接
- 在下一个屏幕中,用户输入用于注册其帐户的电子邮件地址
- 系统生成一个
UUID
令牌,并将其与用户输入的电子邮件地址一起保存在数据库表中.此外,到期时间以Timestamp
值 保存在数据库中
- 一封包含重置密码链接的电子邮件会发送给用户. (电子邮件中包含UUID令牌)
- 当用户单击电子邮件中的链接时,他/她将被重定向到密码重置页面.
- 在该页面中,系统会使用
UUID
令牌将用户的电子邮件地址自动设置为文本字段. 在这里,我需要检查到期时间是否到期.为此,我需要将当前时间的Timestamp value
与到期时间的Timestamp value
(使用UUID token.
从数据库中获取)进行比较
- 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.
我使用此代码段来检索到期时间的timestamp
值.
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);
}
}
尽管当我按ctrl+space
时Eclipse建议queryForObject()
处的java.sql.Timestamp
,但Eclipse在那里显示了一个错误.这是为什么.
此任务的正确代码段是什么.
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.
然后我用了这个
String sql = "SELECT expiray_time FROM recover_password WHERE token = ?";
ex_time = getJdbcTemplate().queryForObject( sql, new Object[] { token }, java.sql.Timestamp);
在这种情况下,也无法识别java.sql.Timestamp
.包含上述方法的类扩展了`SimpleJdbcDaoSupport.
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?
推荐答案
Class
实例作为其第二个参数.因此正确的语法是这样的:
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);
您使用的代码不是有效的Java代码,这就是它无法编译的原因.
The code you are using is not valid Java code, which is the reason why it doesn't compile.
这篇关于使用Spring JDBC模板从mysql数据库中获取Timestamp值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!