Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Query database to retrieve a column for all menu Items in an ArrayList

  1. #1
    Member
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Query database to retrieve a column for all menu Items in an ArrayList

    Having trouble figuring out the code in order to find the price column for all items in a ArrayList

    so far i have:
    private double calculateSubtotal()
    {
     
       try
          {
     
              myConnection = DriverManager.getConnection(DATABASE_URL);
              myStatement = myConnection.createStatement();
              myResultSet = myStatement.executeQuery("SELECT price FROM menu");
          }
          return 0;
    instance variable code below for ArrayList
    private ArrayList billItems = new ArrayList();


    I am not sure if i am going about this the right way or not? if additional info is needed please ask


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Query database to retrieve a column for all menu Items in an ArrayList

    How is the ArrayList related to the data base? The ArrayList contains menu items which correspond to a specific column of a data base? If so, query the data base by the menu name or the place in the column, but you'll need to describe the relationship before a better answer can be given.

  3. #3
    Member
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Query database to retrieve a column for all menu Items in an ArrayList

    Quote Originally Posted by GregBrannon View Post
    How is the ArrayList related to the data base? The ArrayList contains menu items which correspond to a specific column of a data base? If so, query the data base by the menu name or the place in the column, but you'll need to describe the relationship before a better answer can be given.
    This is the rest of the question: This method should then calculate the total price of all the items in the ArrayList and return this value as a double.

    Probably doesn't help?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Query database to retrieve a column for all menu Items in an ArrayList

    If that's your question, it helps a lot. To sum the contents of the ArrayList, iterate it, parse the contents to numbers that can be added, and add them as you iterate. You will likely find assistance in the ArrayList and Integer or Double documentation. Come back when you have more specific questions about how to do what I've described.

  5. #5
    Member
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Query database to retrieve a column for all menu Items in an ArrayList

    Quote Originally Posted by GregBrannon View Post
    If that's your question, it helps a lot. To sum the contents of the ArrayList, iterate it, parse the contents to numbers that can be added, and add them as you iterate. You will likely find assistance in the ArrayList and Integer or Double documentation. Come back when you have more specific questions about how to do what I've described.
    This is what i have done:
    private double calculateSubtotal()
       {
          double total = 0;
          String[] items = billItems;
     
          try
          {
              for(int i = 0; i < items.length; i++ )
            {
              myResultSet = myStatement.executeQuery("SELECT price FROM " +
                      "Menu WHERE name = '" +(String)items[i] + "'");
            }
              if(myResultSet.next() == true)
              {
                  total += myResultSet.getDouble("Price");
              } // end of for loop
          catch ( SQLException sqlException )
          {
             sqlException.printStackTrace();
             System.exit( 1 );
          } // end catch
          }  
     
       } // end method calculateSubtotal

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Query database to retrieve a column for all menu Items in an ArrayList

    And how's that working for you? Is the following statement giving the desired result?

    String[] items = billItems;

    Why are you trying to do that? What type of objects exist (if any) in the ArrayList billItems? If you can answer the last, we can work to iterate those objects and add them together.

  7. #7
    Member
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Query database to retrieve a column for all menu Items in an ArrayList

    Quote Originally Posted by GregBrannon View Post
    And how's that working for you? Is the following statement giving the desired result?

    String[] items = billItems;

    Why are you trying to do that? What type of objects exist (if any) in the ArrayList billItems? If you can answer the last, we can work to iterate those objects and add them together.
    Its alright, i have it working now, thanks

Similar Threads

  1. menubars and menu items
    By runkerr in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 5th, 2013, 10:00 PM
  2. how to remove items of an arraylist
    By crossit in forum Java Theory & Questions
    Replies: 7
    Last Post: December 18th, 2012, 12:14 PM
  3. Java to HTML, enable user to enter a number of menu items
    By @passat in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 3rd, 2012, 07:42 AM
  4. Need some help with removing and finding items from an ArrayList
    By bankston13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 30th, 2012, 08:51 PM
  5. Is it possible to use card layout for menu and menu items?
    By jai2rul in forum AWT / Java Swing
    Replies: 0
    Last Post: April 11th, 2011, 08:19 AM

Tags for this Thread