使用Spring JDBC模板从mysql数据库中获取Timestamp值 [英] Retrieving Timestamp value from mysql database using Spring JDBC template

查看:381
本文介绍了使用Spring JDBC模板从mysql数据库中获取Timestamp值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于Spring的Java项目中实现密码恢复功能.这是我的方法

I am implementing a password recovery function in a Java project based on Spring. This is my methodology for that

  1. 用户点击了忘记密码的链接
  2. 在下一个屏幕中,用户输入用于注册其帐户的电子邮件地址
  3. 系统生成一个UUID令牌,并将其与用户输入的电子邮件地址一起保存在数据库表中.此外,到期时间以Timestamp
  4. 保存在数据库中
  5. 一封包含重置密码链接的电子邮件会发送给用户. (电子邮件中包含UUID令牌)
  6. 当用户单击电子邮件中的链接时,他/她将被重定向到密码重置页面.
  7. 在该页面中,系统会使用UUID令牌将用户的电子邮件地址自动设置为文本字段. 在这里,我需要检查到期时间是否到期.为此,我需要将当前时间的Timestamp value与到期时间的Timestamp value(使用UUID token.
  8. 从数据库中获取)进行比较
  1. User clicks forgot password link
  2. In the next screen, user enter his email address which has been used to register his/her account
  3. System generates a UUIDtoken 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 a Timestamp value
  4. An email including a link to reset password is sent to user. (UUID token is included in the e mail)
  5. When user clicks the link in the email, he/she is redirected to password reset page.
  6. 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 the Timestamp value of current time with the Timestamp value of the expiry time which is taken from the database using UUID 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?

推荐答案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆