Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java Open Source > Struts
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Struts section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 16th, 2006, 06:57 AM
Registered User
 
Join Date: Jun 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to xaitienle
Default Insert database values into a Hashmap collection

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"); return user;

}
...

bui-anh-khoa
 
Old June 19th, 2006, 06:04 AM
Registered User
 
Join Date: Jun 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to xaitienle
Default

I will make it more clear.

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!



bui-anh-khoa





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to insert values in tables using php?? asalaam SQL Server 2000 1 March 27th, 2007 11:23 AM
Generic collection population from database micmit C# 2005 7 September 19th, 2006 05:53 AM
To sort a hashmap using its keys and/or values Mr. Ram Java Basics 1 June 1st, 2006 11:19 PM
insert multiple checkbox values in to database [email protected] Pro JSP 0 March 29th, 2006 08:23 AM
How t o insert multiple values qazi_nomi Access ASP 2 May 3rd, 2005 12:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.