facebook

Insert database values into a Hashmap

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #253540 Reply

    When I used this one:
    users.put(“emchibiet”, new User(“emchibiet”, “kiemchutchao”, new String[] { ADMIN_ROLE }));
    I worked fine. But when I wanna insert database values into a Hashmap collection. Please help. Thanks!
    I didn’t work out. Here is my code:.


    import java.util.HashMap;
    import java.util.Map;
    import deth.MSConnectionSQL;

    public final class SecurityServiceImpl implements SecurityService
    {
    private Map users;
    //private static final String ADMIN_ROLE = “administrator”; //$NON-NLS-1$
    private Connection conn;
    private ResultSet rs;
    private PreparedStatement pre;

    public SecurityServiceImpl()
    {
    MSConnectionSQL bean = new MSConnectionSQL();

    if(bean != null)
    {
    try
    {
    // Open the connection.
    conn = bean.getConnection();
    StringBuffer sql = new StringBuffer();
    sql.append(“SELECT * FROM Users”);
    pre = conn.prepareStatement(sql.toString());
    rs = pre.executeQuery();

    if(rs.next())
    {
    users = new HashMap();
    /*
    users.put(“emchibiet”, new User(“emchibiet”, “kiemchutchao”, new String[] { ADMIN_ROLE }));//$NON-NLS-1$
    */
    users.put(rs.getString(1), new User(rs.getString(2), rs.getString(3), new String[] { rs.getString(4) }));
    }


    public User authenticate(String username, String password)
    throws Exception {
    User user = (User) users.get(username);
    if (user == null)
    throw new Exception(“Unknown user”); //$NON-NLS-1$
    boolean passwordIsValid = user.passwordMatch(password);
    if (!passwordIsValid)
    throw new Exception(“Invalid password”); //$NON-NLS-1$
    return user;

    }

    #253542 Reply

    Haris Peco
    Member

    Xaitienle,

    I don’t sure if this is related with MyEclipse.
    However, what is error ? Have you error log ?

    Best regards

    #253619 Reply

    I wanna add the values of ResultSet to a HashMap.
    users.put(string key, Object object);
    – object is user object of User class : User (String username, String password, String[] roles)

    When I checked user’s login as below:
    users.put(“emchibiet”, new User(“emchibiet”, “kiemchutchao”, new String[] { ADMIN_ROLE }));
    Then everything was alright.

    But in reality, the values must come from database. Therefore, I did:
    users.put(rs.getString(1), new User(rs.getString(2), rs.getString(3), new String[] { rs.getString(4) }));
    It took no error. But Map named users that had no values. So the authenticate method returns empty and throw an exception.

    I heard somewhre it must be converted ResultSet to Map.

    Thanks!

    #253620 Reply

    Haris Peco
    Member

    Your code looks good.Try check value rs.getString(1) … before put in Map

    Best

    #254377 Reply

    Yeah!. I checked it again. Instead of using rs.getString(1);, I used rs.getString(2);
    And the most important thing is that I correct if(rs.next) with while(rs.next). The first one just return a single value so I didn’t pass to authentication. Everything worked out.

    Another thing, I am using Struts 1.2 and I add the ‘<roles=”administrator, manager”>’ into struts-config. Here my code:
    …..

    User user = service.authenticate(username, password);
    HttpSession session = request.getSession();
    session.setAttribute(user);

    In the target JSP page, I found session (user), but found no role. Therefore, my CustomRequestProcessor would return false, and showed no page. I can solve this problem in a hard-coded, but it was imcomfortable. I hope your help. Thanks!

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: Insert database values into a Hashmap

You must be logged in to post in the forum log in