- This topic has 4 replies, 2 voices, and was last updated 18 years, 4 months ago by Gabriel Khoa Bui.
-
AuthorPosts
-
Gabriel Khoa BuiMemberWhen 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;}
…
Haris PecoMemberXaitienle,
I don’t sure if this is related with MyEclipse.
However, what is error ? Have you error log ?Best regards
Gabriel Khoa BuiMemberI 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!
Haris PecoMemberYour code looks good.Try check value rs.getString(1) … before put in Map
Best
Gabriel Khoa BuiMemberYeah!. 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!
-
AuthorPosts