How to access parameter values located inside objects being held inside a Vector

Discussion in 'Java' started by nobody, Apr 26, 2006.

  1. nobody

    nobody Guest

    Hi all;

    I am writing some code that will be part of a website that will have the
    ability to allow customers to place items into a shopping cart and then
    purchase them. The way that the shopping cart works is that a products page
    is displayed on the website that has 'add' buttons on it next to each
    product on the page. When the customer clicks the 'add' button next to a
    particular product to purchase it, the code performs a query of a products
    database table for the numeric product id linked to the 'add' button they
    clicked. This product id is used to located the associated item in the
    products table. When the chosen product is found, the query then loads the
    product information for it from the DB table columns into the shopping cart
    Vector as an object in the form of a result set string. This object contains
    the parameters that make up the product, i.e. product_id, product_quantity,
    date_ordered, ship_status and so on.

    In the code, when the customer is done shopping and clicks on a button
    labelled 'place order', I want the code to access the shopping cart Vector
    and extract each item object from the cart. I then want the code to be able
    to get from each item object, the parameters that make up each item as
    described above and take the values stored in each of the parameters and
    store them in variables. Finally I want to take those variables and write
    them to the orders DB table in the proper columns via a query. I have tried
    researching information on Vectors to see if I could find out a way to do
    this but the little information that I located did not give me a clear idea
    of how to go about writing the code, it only seemed to suggest a vague,
    rough idea as to how it might be done. I am relatively new to Java
    programming and so don't have any experience working with Vectors. I have
    enclosed the code below that shows how the items are being stored in the
    shopping cart Vector. Can anybody tell me how I could go about writing the
    code that will extract the items from the shopping cart as described above
    as a servlet? Any assistance you can give me will be greatly appreciated.

    Thank you!

    String productID = request.getParameter("productID");
    String qty = request.getParameter("qty");
    Vector cartlist = new Vector();
    try {
    java.sql.Connection conn =
    java.sql.DriverManager.getConnection("jdbc:eek:dbc:website", "", "");
    java.sql.Statement st = conn.createStatement();
    String query = "SELECT productID, productName, productCategory,
    productPrice from Products WHERE productID ='" + productID + "'";
    java.sql.ResultSet rs = st.executeQuery(query);
    cartlist.addElement(rs.getString("productID") + " " +
    rs.getString("productName") + " " +
    rs.getString("productCategory") + " " +
    rs.getString("productPrice") + " " + qty);
    } catch(SQLException sqlex){
    } catch(Exception e){
    nobody, Apr 26, 2006
    1. Advertisements

  2. Venkatesh

    Venkatesh Guest


    One good way is to have an interface called "Product" that has methods
    defined for db query, printing values and saving values into DB. Then u
    can have different classes corresponding to each of ur product
    implement this "Product" interface. Ur DB query, showing values and
    saving values can all be done by methods in each of these individual
    classes. In ur servlet u can have ShopingCart as a Vector of Products
    (Vector<Product> ). At the end u can call "save" method for each
    product in ur ShopingCart vector and the product would get saved to ur
    DB. Guess this would be a neater way of doing things for u.

    This is just a hint ... But, u can come up with a good design on these

    Venkatesh, Apr 26, 2006
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kristoffer Arfvidson
    Shiv Kumar
    Jan 21, 2004
  2. Replies:
    Feb 18, 2006
  3. Replies:
    Roedy Green
    Aug 18, 2007
  4. Tim Pollard
    Ray Costanzo [MVP]
    Dec 3, 2004
  5. Thomas Wilson
    Thomas Wilson
    Apr 14, 2010

Share This Page