Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Is it possible to set entries from a database into an ArrayList?

  1. #1
    the31streaper is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Is it possible to set entries from a database into an ArrayList?

    So, basically, what I'd like for this to do is display the entirety of the catalog (currently, there are 48 entries) from my items database and make it possible to select an item, which will then be stored in a cart. My problem currently is that I don't know how to select an item from that catalog, though I'm having a few other issues as well. There are other pieces of code that I can post if it helps.


    This is from my main menu.



    Java Code:
    	System.out.println("Welcome to Imaginaryplacethatsellscoolstuff.com!");
    		System.out.println("1. Browse Catalog");
    		System.out.println("2. Register");
    		System.out.println("3. Log in");
    		System.out.println("4. Forgot Password/Username");
    		System.out.println("5. Quit");
    		System.out.print("\nEnter your choice: ");
    		choice = sc.numInput();
    		{
    			// Choose an option
    			switch (choice) {
    
    			case 1:

    And this is from my Catalog class.

    J
    Java Code:
    	{
    		try {
    			// Connects to the items database
    
    			Class.forName("com.mysql.jdbc.Driver");
    			conn = DriverManager.getConnection(
    					"jdbc:mysql://localhost:3306/items", "root", "root");
    			// Connects to the users database
    
    			String sql = ("SELECT idItems, Itemname,Itemprice, Itemrating, ItemGenre FROM items");
    			PreparedStatement prest = conn.prepareStatement(sql);
    			prest.executeQuery(sql);
    
    			ResultSet rs = prest.getResultSet();
    
    			System.out.println("B = Blu-ray, D = DVD, 3DB = 3D Blu-ray \n");
    
    			while (rs.next()) {
    
    				int idItemsVal = rs.getInt("idItems");
    				String itemNameVal = rs.getString("Itemname");
    				int itemPriceVal = rs.getInt("Itemprice");
    				String itemRatingVal = rs.getString("ItemRating");
    				String itemGenreVal = rs.getString("ItemGenre");
    				System.out.println("" + idItemsVal + ". " + itemNameVal + ",\n"
    						+ itemGenreVal + "\n" + itemRatingVal + "\n$"
    						+ itemPriceVal + "\n");
    			} // Gets the int and String values for each item and prints them
    				// out.
    
    			rs.close();
    			prest.close();
    
    		}
    
    		catch (ClassNotFoundException | SQLException e) {
    			e.printStackTrace();
    
    		}
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,415
    Rep Power
    20

    Default Re: Is it possible to set entries from a database into an ArrayList?

    Create a class that encapsulates the fields of interest, then construct instances of that class while iterating over the ResultSet and add them to a List.

    db
    quad64bit likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 11
    Last Post: 08-08-2011, 04:01 PM
  2. removing repeated entries in arraylist
    By ankit1801 in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 06:34 AM
  3. update arraylist to mysql database
    By sks in forum JDBC
    Replies: 11
    Last Post: 04-01-2011, 08:26 AM
  4. How can i store ArrayList objects in Access database
    By frankycool in forum Advanced Java
    Replies: 3
    Last Post: 11-04-2009, 06:55 AM
  5. Not able to delete MSSql database table entries
    By wickedrahul9 in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2008, 12:14 PM

Tags for this Thread

Posting Permissions

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