Retrieving Timestamp value from mysql database using Spring JDBC template

不打扰是莪最后的温柔 提交于 2019-12-11 07:19:06

问题


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

  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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!