Results 1 to 4 of 4
  1. #1
    jmorris is offline Member
    Join Date
    Nov 2008
    Posts
    12
    Rep Power
    0

    Default Generating Dynamic Lists

    Hello, I am working on a school project. I want to generate a list of customers from a database and display it on the screen.

    I have code written to get the records from the database as follows:

    Java Code:
    public static Vector getAll() {
    		Vector customers = new Vector();
    		String query = "SELECT * FROM Customer ";
    		
    		try {
    			ResultSet rs = stmt.executeQuery(query);
    			boolean more = rs.next();
    			
    			while(more) {
    				String fname = rs.getString(2);
    				String lname = rs.getString(3);
    				String address = rs.getString(4);
    				String city = rs.getString(5);
    				String state = rs.getString(6);
    				String zipcode = rs.getString(7);
    				String phone = rs.getString(8);
    				
    				aCustomer = new Customer(fname, lname, address, city, state, zipcode, phone);
    				customers.addElement(aCustomer);
    				more = rs.next();
    			}
    		  rs.close();
    	  }
    	  catch (SQLException e)  {
    			System.out.println(e);
    		}
    		return customers;
    	}
    Assuming that code is correct, I should have a vector that contains all records in the database. Now I just want to generate a list from this vector that displays the customers first and last name. I'm not sure how to go about this, so could somebody please help??????

    Thanks

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Why not just use an ArrayList<Customer> that would store all your customer objects. Then just put some getter methods within your Customer class that would return the information you want.

  3. #3
    jmorris is offline Member
    Join Date
    Nov 2008
    Posts
    12
    Rep Power
    0

    Default

    Ok, but once I get the information I want, how do I add it to the list? Can I just use list.add('value')?

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    You could just create another ArrayList<String> or just a String[]. For the first way it would be list.add(firstName+", "+lastName) and for String[] I believe its the same command.

Similar Threads

  1. Stacks, lists...
    By little_polarbear in forum New To Java
    Replies: 7
    Last Post: 08-02-2008, 01:59 PM
  2. comparision between two lists
    By suprabha in forum Advanced Java
    Replies: 14
    Last Post: 08-01-2008, 02:49 PM
  3. 2 dimensional Lists
    By gapper in forum New To Java
    Replies: 4
    Last Post: 01-20-2008, 09:01 AM
  4. Tudu Lists 2.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 08-10-2007, 04:39 PM
  5. Tudu Lists 2.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-11-2007, 03:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •