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

N

nobody

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){
}
 
V

Venkatesh

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top