Link to home
Start Free TrialLog in
Avatar of Bette Lamore
Bette LamoreFlag for United States of America

asked on

Problem with createCustomerBean code

My code shows no compilation errors yet that nefarious run-time is lurking...  I created a createCustomer to my CustomerBean code and it runs and returns all my customers in the test main method yet when I present a customer name that is already in my customer list, it should return an error and instead ignores that. here is my code:
package bettelamore;

import java.sql.*;
import java.util.*;

/**
 * <p>
 * This is the Java bean used to get and display information about customers for
 * our web based store.
 * 
 * @see DBBeanBase
 */
public class CustomerBean extends DBBeanBase
{
	/**
	 * <p>
	 * Default constructor, creates an empty {@link ItemBean}.
	 */
	public CustomerBean()
	{
		super();
	}

	/**
	 * <p>
	 * Copy constructor. This constructor creates a {@link CustomerBean} object
	 * that is initially populated with a copy of the {@link Map} specified as a
	 * parameter to this constructor. All {@link java.util.Map Maps} should
	 * implement a copy constructor.
	 * 
	 * @param copyThisMap
	 *            the {@link Map} used to initialize this {@link CustomerBean}
	 *            object.
	 */
	public CustomerBean(Map copyThisMap)
	{
		super(copyThisMap);
	}

	/**
	 * <p>
	 * This constructor creates an {@link CustomerBean} object and populates it
	 * with the fields in a {@link java.sql.ResultSet}.
	 * 
	 * @param rs
	 *            The {@link java.sql.ResultSet} object used to populate this
	 *            class with information about an item.
	 * 
	 * @throws java.sql.SQLException
	 *             if there is an error iterating the {@link java.sql.ResultSet}
	 *             .
	 */
	protected CustomerBean(ResultSet rs) throws SQLException
	{
		super(rs);
	}

	/**
	 * <p>
	 * This constructor creates an {@link CustomerBean} object associated with
	 * the specified username. If there is no record in the data source with the
	 * specified username, an exception is thrown.
	 * 
	 * @param connection
	 *            The {@link java.sql.Connection} object used to execute the
	 *            select statement that will retrieve the item information.
	 * @param susername
	 *            specifies the username used to populate this object.
	 * 
	 * @throws java.sql.SQLException
	 *             if there is an error selecting the customer information.
	 * @throws java.lang.Exception
	 *             if the username is invalid.
	 */
	public CustomerBean(Connection connection, String susername) throws SQLException, Exception
	{
		super();
		String strSQL = "SELECT * FROM customer WHERE username = '" + susername + "'";
		Statement statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(strSQL);
		if (rs.next())
		{
			populateFromResultSet(rs);
		}
		else
		{
			throw new Exception("Invalid username: " + susername);
		}
	}

	/**
	 * <p>
	 * This method returns the username.
	 * 
	 * @return the username.
	 */
	public String getUsername()
	{
		return get("username").toString();
	}

	/**
	 * <p>
	 * This method returns the name of the customer's password.
	 * 
	 * @return the name of the customer's password.
	 */
	public String getPassword()
	{
		return get("PASSWORD").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's address.
	 * 
	 * @return the customer's address.
	 */
	public String getAddress()
	{
		return get("ADDRESS").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's city.
	 * 
	 * @return the customer's city.
	 */
	public String getCity()
	{
		return get("CITY").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's state.
	 * 
	 * @return the customer's state.
	 */
	public String getState()
	{
		return get("STATE").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's zipcode.
	 * 
	 * @return the customer's zipcode.
	 */
	public String getZip()
	{
		return get("ZIP").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's email address.
	 * 
	 * @return the customer's email address.
	 */
	public String getEmail()
	{
		return get("ADDRESS").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's phone number.
	 * 
	 * @return the customer's phone number.
	 */
	public String getPhone()
	{
		return get("PHONE").toString();
	}

	/**
	 * <p>
	 * This method returns a textual representation of this {@link CustomerBean}.
	 * 
	 * @return a textual representation of this {@link CustomerBean}.
	 */
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("username: " + getUsername() + "\n");
		sb.append("Password: " + getPassword() + "\n");
		sb.append("Address: " + getAddress() + "\n");
		sb.append("City: " + getCity().toString() + "\n");
		sb.append("State: " + getState().toString() + "\n");
		sb.append("Zip: " + getZip().toString() + "\n");
		sb.append("Phone: " + getPhone().toString() + "\n");
		sb.append("Email: " + getEmail().toString() + "\n");
		return sb.toString();
	}

	/**
	 * <p>
	 * This method returns a {@link java.util.Collection} of all of the
	 * customers in the data source. This is really the store's customer base.
	 * 
	 * @param connection
	 *            The {@link java.sql.Connection} object used to execute the
	 *            select statement that will retrieve the customers.
	 * 
	 * @return a {@link java.util.Collection} of all of the customers in the
	 *         data source.
	 * 
	 * @throws java.sql.SQLException
	 *             if there is an error selecting the customer.
	 */
	public static Collection getAllCustomers(Connection connection) throws SQLException
	{
		Collection col = new ArrayList();

		String strSQL = "SELECT * FROM customer";
		Statement statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(strSQL);
		while (rs.next())
		{
			col.add(new CustomerBean(rs));
		}
		return col;
	}

	public static CustomerBean createCustomer(Connection connection,
    		String username,
    		String password,
    		String address,
    		String city,
    		String state,
    		String zip,
    		String email,
    		String phone,
    		String creditcard) throws SQLException, Exception
    {
    	 	String susername = username;
			Object Customer;
			if ("susername" != "Customer.username")
	    	{
	    		Statement stmt = connection.createStatement();
	
	    		String strSQL = "INSERT INTO CUSTOMER (username, password, address, city, state, zip, email, phone, creditcard) " +
	    			"VALUES (" + username + ", " + password + ", " + address + ", " + city + ", " + state + ", " + zip + ", " +
	    					email + ", " + phone + ", " + creditcard + ")";
	
	    		stmt.executeUpdate(strSQL);
	    		return new CustomerBean(connection, username);
	    	}	
			else
			{
				return null;
			}
    	// check to see if a customer already exists with this username,
    	// if they do, return null, otherwise add the new customer and return
    	// a new CustomerBean for the new customer... }
		
    }

	/**
	 * <p>
	 * Test method used to test this class. This method creates an CustomerBean
	 * object using the specified DBURL and username. The contents of the
	 * CustomerBean are printed to the console. In addition, this method gets a
	 * {@link java.util.Collection} of all of the customers in the data source
	 * and prints them on the console.
	 * 
	 * <p>
	 * This test method assumes that the database is a MySQL database that
	 * contains an 'user1' account with a password 'user1'.
	 * 
	 * <pre>
	 *      usage: java DBBean [DBURL] [username]
	 * </pre>
	 * 
	 * @param args
	 *            an array of command line arguments. This test method takes two
	 *            parameters, one that specifies the DBURL and the other
	 *            specifies the username.
	 */

	public static void main(String[] args)
	{
		Connection connection = null;
		try
		{
			if (args.length != 2)
			{
				System.out.println("usage: java CustomerBean [DBURL] [username]");
				System.out.println("Example DBURL: jdbc:mysql://localhost:3306/WebStore");
				System.exit(0);
			}

			String strDBDriver = "com.mysql.jdbc.Driver";
			String strDBURL = args[0];
			String strDBUSER = "user1";
			String strDBPWD = "user1";

			Class.forName(strDBDriver);
			connection = DriverManager.getConnection(strDBURL, strDBUSER, strDBPWD);

			DBBeanBase customerBean = new CustomerBean(connection, args[1]);
			System.out.println("A Customer:\n" + customerBean);

			System.out.println("All Customers:\n" + CustomerBean.getAllCustomers(connection));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				connection.close();
			}
			catch (Exception e)
			{
			}
		}
	}
}

Open in new window


Thanks for any help
Bette
Avatar of Mick Barry
Mick Barry
Flag of Australia image

>                   if ("susername" != "Customer.username")

should use equals() to compare string


                  if (!"susername".equals("Customer.username"))
This is strange if statement:

if ("susername" != "Customer.username")
You probably don't want quotes

    if (!susername.equals("Customer.username"))
At least in one place you done' t want quotes
because this:

    if (!"susername".equals("Customer.username"))

will always be true.
and if think you actually mean to do this:

                  if (!susername.equals(Customer.getUsername()))

But that won't work as you haven't assigned customer to anything

To check if a customer already exists you are going to need to query the database
Or alternatively you can set unique constraint on username in the database table (probably a good idea anyway)
and then check the numbert of inserted records:

int inserted = stmt.executeUpdate(strSQL);
if(inserted != 1) {
Syatem.out.println("Not unique username or another error while inserting user record into database");
}

Avatar of Bette Lamore

ASKER

Sorry -- didn't see the replies -- I cancel my request to cancel -- someone has already seen my error and I will continue to accept solutions.
Thank you for the ones so far!
Bette
OK, I ran with the new code and it is not letting me create a new customer -- it is reverting to the exception that username doesn't exist when I put a new user into my argument. I guess I don't know what to enter in the run configuration arguments to let the program know that I am adding a record -- not trying to pull out a pre-existing record. Sorry to be so dense -- never used Beans before. I am using Eclipse.
Bette
Maybe you want to post a new code you are running now and also
post the exception stack trace
I get this code when I type in a new username -- which is what I should get if I was pulling out a name already in the datatbase. java.lang.Exception: Invalid username: woaJessie
      at bettelamore.CustomerBean.<init>(CustomerBean.java:87)
      at bettelamore.CustomerBean.main(CustomerBean.java:305)

so that part is OK -- what I don't know how to do is enter a new username by using the run configuration arguments??
Thanks for your prompt reply!!

revised code:

package bettelamore;

import java.sql.*;
import java.util.*;

/**
 * <p>
 * This is the Java bean used to get and display information about customers for
 * our web based store.
 * 
 * @see DBBeanBase
 */
public class CustomerBean extends DBBeanBase
{
	/**
	 * <p>
	 * Default constructor, creates an empty {@link ItemBean}.
	 */
	public CustomerBean()
	{
		super();
	}

	/**
	 * <p>
	 * Copy constructor. This constructor creates a {@link CustomerBean} object
	 * that is initially populated with a copy of the {@link Map} specified as a
	 * parameter to this constructor. All {@link java.util.Map Maps} should
	 * implement a copy constructor.
	 * 
	 * @param copyThisMap
	 *            the {@link Map} used to initialize this {@link CustomerBean}
	 *            object.
	 */
	public CustomerBean(Map copyThisMap)
	{
		super(copyThisMap);
	}

	/**
	 * <p>
	 * This constructor creates an {@link CustomerBean} object and populates it
	 * with the fields in a {@link java.sql.ResultSet}.
	 * 
	 * @param rs
	 *            The {@link java.sql.ResultSet} object used to populate this
	 *            class with information about an item.
	 * 
	 * @throws java.sql.SQLException
	 *             if there is an error iterating the {@link java.sql.ResultSet}
	 *             .
	 */
	protected CustomerBean(ResultSet rs) throws SQLException
	{
		super(rs);
	}

	/**
	 * <p>
	 * This constructor creates an {@link CustomerBean} object associated with
	 * the specified username. If there is no record in the data source with the
	 * specified username, an exception is thrown.
	 * 
	 * @param connection
	 *            The {@link java.sql.Connection} object used to execute the
	 *            select statement that will retrieve the item information.
	 * @param susername
	 *            specifies the username used to populate this object.
	 * 
	 * @throws java.sql.SQLException
	 *             if there is an error selecting the customer information.
	 * @throws java.lang.Exception
	 *             if the username is invalid.
	 */
	public CustomerBean(Connection connection, String susername) throws SQLException, Exception
	{
		super();
		String strSQL = "SELECT * FROM customer WHERE username = '" + susername + "'";
		Statement statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(strSQL);
		if (rs.next())
		{
			populateFromResultSet(rs);
		}
		else
		{
			throw new Exception("Invalid username: " + susername);
		}
	}

	/**
	 * <p>
	 * This method returns the username.
	 * 
	 * @return the username.
	 */
	public String getUsername()
	{
		return get("username").toString();
	}

	/**
	 * <p>
	 * This method returns the name of the customer's password.
	 * 
	 * @return the name of the customer's password.
	 */
	public String getPassword()
	{
		return get("PASSWORD").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's address.
	 * 
	 * @return the customer's address.
	 */
	public String getAddress()
	{
		return get("ADDRESS").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's city.
	 * 
	 * @return the customer's city.
	 */
	public String getCity()
	{
		return get("CITY").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's state.
	 * 
	 * @return the customer's state.
	 */
	public String getState()
	{
		return get("STATE").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's zipcode.
	 * 
	 * @return the customer's zipcode.
	 */
	public String getZip()
	{
		return get("ZIP").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's email address.
	 * 
	 * @return the customer's email address.
	 */
	public String getEmail()
	{
		return get("ADDRESS").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's phone number.
	 * 
	 * @return the customer's phone number.
	 */
	public String getPhone()
	{
		return get("PHONE").toString();
	}

	/**
	 * <p>
	 * This method returns a textual representation of this {@link CustomerBean}.
	 * 
	 * @return a textual representation of this {@link CustomerBean}.
	 */
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("username: " + getUsername() + "\n");
		sb.append("Password: " + getPassword() + "\n");
		sb.append("Address: " + getAddress() + "\n");
		sb.append("City: " + getCity().toString() + "\n");
		sb.append("State: " + getState().toString() + "\n");
		sb.append("Zip: " + getZip().toString() + "\n");
		sb.append("Phone: " + getPhone().toString() + "\n");
		sb.append("Email: " + getEmail().toString() + "\n");
		return sb.toString();
	}

	/**
	 * <p>
	 * This method returns a {@link java.util.Collection} of all of the
	 * customers in the data source. This is really the store's customer base.
	 * 
	 * @param connection
	 *            The {@link java.sql.Connection} object used to execute the
	 *            select statement that will retrieve the customers.
	 * 
	 * @return a {@link java.util.Collection} of all of the customers in the
	 *         data source.
	 * 
	 * @throws java.sql.SQLException
	 *             if there is an error selecting the customer.
	 */
	public static Collection getAllCustomers(Connection connection) throws SQLException
	{
		Collection col = new ArrayList();

		String strSQL = "SELECT * FROM customer";
		Statement statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(strSQL);
		while (rs.next())
		{
			col.add(new CustomerBean(rs));
		}
		return col;
	}

	public static CustomerBean createCustomer(Connection connection,
    		String username,
    		String password,
    		String address,
    		String city,
    		String state,
    		String zip,
    		String email,
    		String phone,
    		String creditcard) throws SQLException, Exception
    {
			
    		Statement stmt = connection.createStatement();
    		String strSQL = null;
			int inserted = stmt.executeUpdate(strSQL);
			if(inserted != 1) 
			{
				System.out.println("Not unique username or another error while inserting user record into database");
	    		strSQL = "INSERT INTO CUSTOMER (username, password, address, city, state, zip, email, phone, creditcard) " +
	    			"VALUES (" + username + ", " + password + ", " + address + ", " + city + ", " + state + ", " + zip + ", " +
	    					email + ", " + phone + ", " + creditcard + ")";
	
	    		stmt.executeUpdate(strSQL);
	    		return new CustomerBean(connection, username);
	    	}	
			else
			{
				return null;
			}
    	// check to see if a customer already exists with this username,
    	// if they do, return null, otherwise add the new customer and return
    	// a new CustomerBean for the new customer... }
		
    }

	/**
	 * <p>
	 * Test method used to test this class. This method creates an CustomerBean
	 * object using the specified DBURL and username. The contents of the
	 * CustomerBean are printed to the console. In addition, this method gets a
	 * {@link java.util.Collection} of all of the customers in the data source
	 * and prints them on the console.
	 * 
	 * <p>
	 * This test method assumes that the database is a MySQL database that
	 * contains an 'user1' account with a password 'user1'.
	 * 
	 * <pre>
	 *      usage: java DBBean [DBURL] [username]
	 * </pre>
	 * 
	 * @param args
	 *            an array of command line arguments. This test method takes two
	 *            parameters, one that specifies the DBURL and the other
	 *            specifies the username.
	 */

	public static void main(String[] args)
	{
		Connection connection = null;
		try
		{
			if (args.length != 2)
			{
				System.out.println("usage: java CustomerBean [DBURL] [username]");
				System.out.println("Example DBURL: jdbc:mysql://localhost:3306/WebStore");
				System.exit(0);
			}

			String strDBDriver = "com.mysql.jdbc.Driver";
			String strDBURL = args[0];
			String strDBUSER = "user1";
			String strDBPWD = "user1";

			Class.forName(strDBDriver);
			connection = DriverManager.getConnection(strDBURL, strDBUSER, strDBPWD);

			DBBeanBase customerBean = new CustomerBean(connection, args[1]);
			System.out.println("A Customer:\n" + customerBean);

			System.out.println("All Customers:\n" + CustomerBean.getAllCustomers(connection));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				connection.close();
			}
			catch (Exception e)
			{
			}
		}
	}
}

Open in new window

But if you have new username rs.next() will be false
and it 'll throw Exception according to your code

throw new Exception("Invalid username: " + susername);


ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I added the code and now I am getting an exception if I add a username that is already in database. I am realizing I have 2 scenarios that are diametrically opposing and if I correct one situation, the other goes bad. How do I test for the pulling out a preexisting name and then test for adding a new user?

package bettelamore;

import java.sql.*;
import java.util.*;

/**
 * <p>
 * This is the Java bean used to get and display information about customers for
 * our web based store.
 * 
 * @see DBBeanBase
 */
public class CustomerBean extends DBBeanBase
{
	/**
	 * <p>
	 * Default constructor, creates an empty {@link ItemBean}.
	 */
	public CustomerBean()
	{
		super();
	}

	/**
	 * <p>
	 * Copy constructor. This constructor creates a {@link CustomerBean} object
	 * that is initially populated with a copy of the {@link Map} specified as a
	 * parameter to this constructor. All {@link java.util.Map Maps} should
	 * implement a copy constructor.
	 * 
	 * @param copyThisMap
	 *            the {@link Map} used to initialize this {@link CustomerBean}
	 *            object.
	 */
	public CustomerBean(Map copyThisMap)
	{
		super(copyThisMap);
	}

	/**
	 * <p>
	 * This constructor creates an {@link CustomerBean} object and populates it
	 * with the fields in a {@link java.sql.ResultSet}.
	 * 
	 * @param rs
	 *            The {@link java.sql.ResultSet} object used to populate this
	 *            class with information about an item.
	 * 
	 * @throws java.sql.SQLException
	 *             if there is an error iterating the {@link java.sql.ResultSet}
	 *             .
	 */
	protected CustomerBean(ResultSet rs) throws SQLException
	{
		super(rs);
	}

	/**
	 * <p>
	 * This constructor creates an {@link CustomerBean} object associated with
	 * the specified username. If there is no record in the data source with the
	 * specified username, an exception is thrown.
	 * 
	 * @param connection
	 *            The {@link java.sql.Connection} object used to execute the
	 *            select statement that will retrieve the item information.
	 * @param susername
	 *            specifies the username used to populate this object.
	 * 
	 * @throws java.sql.SQLException
	 *             if there is an error selecting the customer information.
	 * @throws java.lang.Exception
	 *             if the username is invalid.
	 */

	public CustomerBean(Connection connection, String susername) throws SQLException, Exception
	{
		super();
		String strSQL = "SELECT * FROM customer WHERE username = '" + susername + "'";
		Statement statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(strSQL);
		boolean found = false;
		while (rs.next())
		{
			found = true;
		}
		if (found)
		{
			
			throw new Exception("Invalid username: " + susername);
			// do somthing if username already exists
		}
		else
		{
			// do something if a new username
			populateFromResultSet(rs);
		}
	}

	/**
	 * <p>
	 * This method returns the username.
	 * 
	 * @return the username.
	 */
	public String getUsername()
	{
		return get("username").toString();
	}

	/**
	 * <p>
	 * This method returns the name of the customer's password.
	 * 
	 * @return the name of the customer's password.
	 */
	public String getPassword()
	{
		return get("PASSWORD").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's address.
	 * 
	 * @return the customer's address.
	 */
	public String getAddress()
	{
		return get("ADDRESS").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's city.
	 * 
	 * @return the customer's city.
	 */
	public String getCity()
	{
		return get("CITY").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's state.
	 * 
	 * @return the customer's state.
	 */
	public String getState()
	{
		return get("STATE").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's zipcode.
	 * 
	 * @return the customer's zipcode.
	 */
	public String getZip()
	{
		return get("ZIP").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's email address.
	 * 
	 * @return the customer's email address.
	 */
	public String getEmail()
	{
		return get("ADDRESS").toString();
	}

	/**
	 * <p>
	 * This method returns the customer's phone number.
	 * 
	 * @return the customer's phone number.
	 */
	public String getPhone()
	{
		return get("PHONE").toString();
	}

	/**
	 * <p>
	 * This method returns a textual representation of this {@link CustomerBean}.
	 * 
	 * @return a textual representation of this {@link CustomerBean}.
	 */
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("username: " + getUsername() + "\n");
		sb.append("Password: " + getPassword() + "\n");
		sb.append("Address: " + getAddress() + "\n");
		sb.append("City: " + getCity().toString() + "\n");
		sb.append("State: " + getState().toString() + "\n");
		sb.append("Zip: " + getZip().toString() + "\n");
		sb.append("Phone: " + getPhone().toString() + "\n");
		sb.append("Email: " + getEmail().toString() + "\n");
		return sb.toString();
	}

	/**
	 * <p>
	 * This method returns a {@link java.util.Collection} of all of the
	 * customers in the data source. This is really the store's customer base.
	 * 
	 * @param connection
	 *            The {@link java.sql.Connection} object used to execute the
	 *            select statement that will retrieve the customers.
	 * 
	 * @return a {@link java.util.Collection} of all of the customers in the
	 *         data source.
	 * 
	 * @throws java.sql.SQLException
	 *             if there is an error selecting the customer.
	 */
	public static Collection getAllCustomers(Connection connection) throws SQLException
	{
		Collection col = new ArrayList();

		String strSQL = "SELECT * FROM customer";
		Statement statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(strSQL);
		while (rs.next())
		{
			col.add(new CustomerBean(rs));
		}
		return col;
	}

	public static CustomerBean createCustomer(Connection connection, String username, String password, String address, String city, String state, String zip, String email, String phone, String creditcard) throws SQLException, Exception
	{

		Statement stmt = connection.createStatement();
		String strSQL = null;
		int inserted = stmt.executeUpdate(strSQL);
		if (inserted != 1)
		{
			System.out.println("Not unique username or another error while inserting user record into database");
			strSQL = "INSERT INTO CUSTOMER (username, password, address, city, state, zip, email, phone, creditcard) " + "VALUES (" + username + ", " + password + ", " + address + ", " + city + ", " + state + ", " + zip + ", " + email + ", " + phone + ", " + creditcard + ")";

			stmt.executeUpdate(strSQL);
			return new CustomerBean(connection, username);
		}
		else
		{
			return null;
		}
		// check to see if a customer already exists with this username,
		// if they do, return null, otherwise add the new customer and return
		// a new CustomerBean for the new customer... }

	}

	/**
	 * <p>
	 * Test method used to test this class. This method creates an CustomerBean
	 * object using the specified DBURL and username. The contents of the
	 * CustomerBean are printed to the console. In addition, this method gets a
	 * {@link java.util.Collection} of all of the customers in the data source
	 * and prints them on the console.
	 * 
	 * <p>
	 * This test method assumes that the database is a MySQL database that
	 * contains an 'user1' account with a password 'user1'.
	 * 
	 * <pre>
	 *      usage: java DBBean [DBURL] [username]
	 * </pre>
	 * 
	 * @param args
	 *            an array of command line arguments. This test method takes two
	 *            parameters, one that specifies the DBURL and the other
	 *            specifies the username.
	 */

	public static void main(String[] args)
	{
		Connection connection = null;
		try
		{
			if (args.length != 2)
			{
				System.out.println("usage: java CustomerBean [DBURL] [username]");
				System.out.println("Example DBURL: jdbc:mysql://localhost:3306/WebStore");
				System.exit(0);
			}

			String strDBDriver = "com.mysql.jdbc.Driver";
			String strDBURL = args[0];
			String strDBUSER = "user1";
			String strDBPWD = "user1";

			Class.forName(strDBDriver);
			connection = DriverManager.getConnection(strDBURL, strDBUSER, strDBPWD);

			DBBeanBase customerBean = new CustomerBean(connection, args[1]);
			System.out.println("A Customer:\n" + customerBean);

			System.out.println("All Customers:\n" + CustomerBean.getAllCustomers(connection));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				connection.close();
			}
			catch (Exception e)
			{
			}
		}
	}
}

Open in new window


But that's what you wanted - to throw exception
if the username  already exists in the databse - isn't that waht you need?

Yan really worked hard on this one. My mouse slipped and clicked on the last post not being helpful but that was an error. I beleive you've solved my problem -- it is just that I am having a hard time testing the additional code in Eclipse.
Thank you so much for your time.
Bette
Oh yan -- there are 2 parts to this program -- one is to extract info already in database, and one is to add info -- new user-- to database. I am having a tough time testing for both. Never had to do that before and I am using Eclipse Run Configuration with args for testing.
Well, test them stepwise - one part, another part,
I don't think there is anything  too complicated about it -  just like any other debugging.
I guess I was trying to figure out a way to test both scenarious at the same time -- working to hard on all this code for my store that I am a little brain dead at present :-)
Thanks for all your help!
Bette