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 {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    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);
    while(rs.next()){
    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
    1. Advertising

  2. nobody

    Venkatesh Guest

    Hi,

    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
    lines.

    -Venkatesh
     
    Venkatesh, Apr 26, 2006
    #2
    1. Advertising

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
    Replies:
    7
    Views:
    604
    Shiv Kumar
    Jan 21, 2004
  2. Replies:
    8
    Views:
    1,986
    Csaba
    Feb 18, 2006
  3. Replies:
    13
    Views:
    1,722
    Roedy Green
    Aug 18, 2007
  4. Tim Pollard
    Replies:
    8
    Views:
    165
    Ray Costanzo [MVP]
    Dec 3, 2004
  5. Thomas Wilson
    Replies:
    3
    Views:
    130
    Thomas Wilson
    Apr 14, 2010
Loading...

Share This Page