0
我正在寻找一种将用户信息存储在Spring MVC Web应用程序中的方法。例如,我有这样的方法:存储有关用户的信息Spring MVC
public int getUserId() throws NumberFormatException, SQLException{
//Create the connection and the statement
Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement();
//Result set for getting/executing the query
ResultSet rs = statement.executeQuery("SELECT user_id FROM users WHERE username='UserHere'");
while(rs.next()){
return Integer.parseInt(rs.getString("user_id"));
}
//Return the results
return -1;
}
通过了Web应用程序,我调用此方法来获取usersId,我只是在寻找一种方式来调用一次这个,一度被称为它存储在一个变量/我可以通过Web应用程序访问全球吗?
也许在主页上加载我可以有一个!=检查引用这个信息的某个对象,如果它没有被创建,它将被创建。这将发生一次,直到下次使用。
感谢
无关提示使用'JdbcTemplate'而不是试图用一个简单的'DataSource'等工作二读的参考指南章添加为一个变量关于缓存。 –