简体   繁体   中英

Storing information about a user Spring MVC

I am looking for a way to store users information in a Spring MVC web application. For example I have this method:

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;
}

Through out the web application I am calling this method to get the usersId, I am just looking for a way to call this once, once called it stores it in a variable/place that I can access globally through the Web app?

Maybe on the homepage load I could have a != check to some object referencing this information, if it has not been created, it will be created. This will occur once until the next use.

Thanks

You could add this as a variable on the session

@RequestMapping(..)
public String controllerMethod(HttpSession session) {
    session.addAttribute("userid", getUserId());
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM