The following is the source program of my shopping cart Using JSP and database:
Note:
1. The re-billing part is not ready yet. Please do it yourself!
2. the next version uses session.
// Shop_cart.jsp
<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page session = "true" %>
<% @ Page language = "java" import = "java. SQL. *" %>
<Jsp: useBean id = "bka" scope = "page" class = "shop. bka"/>
<%
String product_type;
String action;
Int product_id;
Int curpage;
// Item type
If (request. getParameter ("product_type") = null ){
Product_type = "all ";
} Else {
Product_type = request. getParameter ("product_type ");
}
// Page number and item type parameters, which can be returned to the last shopping page when "continue shopping"
If (request. getParameter ("curpage") = null ){
Curpage = 1;
} Else {
Curpage = java. lang. Integer. parseInt (request. getParameter ("curpage "));
}
// Action
If (request. getParameter ("action") = null ){
Action = "view ";
} Else {
Action = request. getParameter ("action ");
}
// Product ID
If (request. getParameter ("product_id") = null ){
Product_id = 0;
} Else {
Product_id = java. lang. Integer. parseInt (request. getParameter ("product_id "));
}
Int bbb;
Bbb = 1;
Integer num = new Integer (bbb );
// Store ID
Session. putValue ("shop_id", num );
// Customer username
Session. putValue ("guest_name", "asp2001 ");
String guest_name = (String) session. getValue ("guest_name ");
Integer shop_id = (Integer) session. getValue ("shop_id ");
Java. lang. String SQL;
Java. SQL. ResultSet rs;
If (action. compareTo ("add") = 0 ){
SQL = "select cart_quantity from shop_cart where cart_shop_id =" + shop_id
+ "And cart_guest_id = '" + guest_name + "' and cart_product_id =" + product_id;
Rs = bka.exe cuteQuery (SQL );
If (rs. next ()){
Int cart_quantity;
Cart_quantity = java. lang. Integer. parseInt (rs. getString ("cart_quantity") + 1;
SQL = "update shop_cart set cart_quantity =" + cart_quantity + "where cart_shop_id ="
+ Shop_id + "and cart_guest_id = '" + guest_name + "' and cart_product_id =" + product_id;
Rs = bka.exe cuteQuery (SQL );}
Else
{
SQL = "insert into shop_cart (cart_shop_id, cart_guest_id, cart_product_id, cart_quantity) values ('" + shop_id + "','"
+ Guest_name + "','" + product_id + "', 1 )";
Rs = bka.exe cuteQuery (SQL );
}
}
If (action. compareTo ("clear") = 0 ){
SQL = "delete from shop_cart where cart_shop_id =" + shop_id + "and cart_guest_id = '" + guest_name + "'";
Rs = bka.exe cuteQuery (SQL );
}
If (action. compareTo ("delete") = 0 ){
SQL = "delete from shop_cart where cart_shop_id =" + shop_id + "and cart_guest_id = '"
+ Guest_name + "'and cart_product_id =" + product_id;
Rs = bka.exe cuteQuery (SQL );
}
%>
<Div align = "center"> <center>
<Table border = "0" cellpadding = "0" cellspacing = "0" width = "610"
Height = "2">
<Tr>
<Td> <form method = "POST"
Action = "shop_cart.jsp? Action = update & product_type = <% = product_type %> & curp
Age = <% = curpage %> ">
<Table border = "0" cellpadding = "0" cellspacing = "0" width = "610"
Height = "2">
<Tr>
<Td width = "122" height = "7"> <div align = "center"> <center> <table
Border = "1" cellpadding = "2" cellspacing = "0" width = "100%" bordercolorlight = "# FFB468"
Bordercolordark = "# FFFFFF" bgcolor = "# FFB468">
<Tr>
& Lt; td width = "100%" & gt; & lt; div
Align = "center"> <center> <p> shopping cart </td>
</Tr>
</Table>
</Center> </div> </td>
<Td width = "122" height = "7" style = "border: medium"
Align = "center"> <div align = "center"> <center> <table
Border = "1" cellpadding = "2" cellspacing = "0" width = "100%"
Bordercolorlight = "# FFB468"
Bordercolordark = "# FFFFFF" bgcolor = "# FFB468">
<Tr>
<Td width = "100%"> <div align = "center"> <center> <p> <
Href = "shop_list.jsp? Shop_id = <% = shop_id %> & product_type = <% = product_type %>
& Curpage = <% = curpage %> "> continue shopping </a> </td>
</Tr>
</Table>
</Center> </div> </td>
<Td width = "122" height = "7" style = "border: medium"
Align = "center"> <div align = "center"> <center> <table
Border = "1" cellpadding = "2" cellspacing = "0" width = "100%"
Bordercolorlight = "# FFB468"
Bordercolordark = "# FFFFFF" bgcolor = "# FFB468">
<Tr>
<Td width = "100%"> <div align = "center"> <center> <p> <
Href = "javascript: document. forms [0]. submit ()"> recharge </a> </td>
</Tr>
</Table>
</Center> </div> </td>
<Td width = "122" height = "7" style = "border: medium"
Align = "center"> <div align = "center"> <center> <table
Border = "1" cellpadding = "2" cellspacing = "0" width = "100%"
Bordercolorlight = "# FFB468"
Bordercolordark = "# FFFFFF" bgcolor = "# FFB468">
<Tr>
<Td width = "100%"> <div align = "center"> <center> <p>
<A href = "shop_cart.jsp? Action = clear & product_type = <% = product_type %> & curpage = <% = curpage %> "> clear a shopping cart </a> </td>
</Tr>
</Table>
</Center> </div> </td>
<Td width = "122" height = "7" style = "border: medium" align = "center"> <div align = "center"> <center> <table
Border = "1" cellpadding = "2" cellspacing = "0" width = "100%" bordercolorlight = "# FFB468"
Bordercolordark = "# FFFFFF" bgcolor = "# FFB468">
<Tr>
<Td width = "100%"> <div align = "center"> <center> <p> <a href = "shop_order.asp"> confirm purchase </a> </td>
</Tr>
</Table>
</Center> </div> </td>
</Tr>
<Tr align = "center">
<Td width = "610" height = "1" colspan = "5"> <div align = "center"> <center>
<Table border = "1" cellpadding = "2" cellspacing = "0" width = "100%" bgcolor = "# FDFEE2"
Bordercolorlight = "# FFB468" bordercolordark = "# FFFFFF" height = "40">
<Tr>
<Td width = "20%" height = "8" align = "left"> item name </td>
<Td width = "10%" height = "8" align = "left"> market price </td>
<Td width = "10%" height = "8" align = "left"> discount price </td>
<Td width = "10%" height = "8" align = "left"> quantity </td>
<Td width = "14%" height = "8" align = "left"> subtotal </td>
<Td width = "12%" height = "8" align = "left"> deposit ratio </td>
<Td width = "17%" height = "8" align = "left"> deposit subtotal </td>
<Td width = "17%" height = "8" align = "left"> Delete </td>
</Tr>
<%
SQL = "select shop_product.product_id, shop_product.product_name, shop_product.product_price,
Shop_product.product_discount, region, shop_product.product_first from shop_cart, shop_product where region = "+ shop_id +" and region = '"+ guest_name +"' and shop_cart.cart_product_id = shop_product.product_id ";
Rs = bka.exe cuteQuery (SQL );
Int total;
Int total_first;
Total = 0;
Total_first = 0;
String product_name;
Int product_price;
Int product_discount;
Int product_first;
Int cart_quantity;
If (rs. next ()){
While (rs. next ()){
Product_id = java. lang. Integer. parseInt (rs. getString (1 ));
Product_name = rs. getString (2 );
Product_price = java. lang. Integer. parseInt (rs. getString (3 ));
Product_discount = java. lang. Integer. parseInt (rs. getString (4 ));
Cart_quantity = java. lang. Integer. parseInt (rs. getString (5 ));
Product_first = java. lang. Integer. parseInt (rs. getString (6 ));
%>
<Tr>
<Td width = "10%" height = "1" align = "left"> <% = product_name %> </td>
<Td width = "10%" height = "1" align = "left"> <% = product_price %> </td>
<Td width = "10%" height = "1" align = "left"> <% = product_discount %> </td>
<Td width = "10%" height = "1" align = "left"> <input type = "text" name = "<% =" t "+ product_id %>" size = "3" value = "<% = cart_quantity %>"> </td>
<Td width = "14%" height = "1" align = "left"> <% = product_discount * cart_quantity %> </td>
<Td width = "12%" height = "1" align = "left"> <% = product_first + "%" %> </td>
<Td width = "17%" height = "1" align = "left"> <% = product_first * product_discount * cart_quantity/100.0%> </td>
<Td width = "17%" height = "1"> <div align = "center"> <center> <p> <a href = "shop_cart.jsp? Action = delete & product_id = <% = product_id %> "> delete </a> </td>
</Tr>
<%
Total = total + product_discount * cart_quantity;
Total_first = total_first + product_discount * cart_quantity * product_first/100;
}
%>
<Tr align = "center">
<Td width = "72%" colspan = "6" height = "16"> <div align = "right"> <p> total </td>
<Td width = "36%" colspan = "2" height = "16"> <div align = "left"> <% = total %> </td>
</Tr>
<Tr align = "center">
<Td width = "72%" colspan = "6" height = "16"> <div align = "right"> <p> total deposit </td>
<Td width = "36%" colspan = "2" height = "16"> <div align = "left"> <% = total_first %> </td>
</Tr>
<Tr align = "center">
<Td width = "72%" colspan = "6" height = "16"> <div align = "right"> <p> balance </td>
<Td width = "36%" colspan = "2" height = "16"> <div align = "left"> <% = total-total_first %> </td>
</Tr>
</Table>
</Center> </div>
<%
} Else {
%>
<P align = "center"> the shopping cart is empty! </P>
<%
}
%>
Database Operations
The program uses two tables:
1 shop_cart table
Cart_id int: Automatic ID of the shopping cart number
Cart_shop_id nvarchar store no.
Cart_product_id nvarchar Product NO.
Cart_quantity int commodity quantity
Temporarily store shopping cart data
2 shop_product table
Product_id int product No.: Automatic No.
Shop_id nvarchar store no.
Product_name nvarchar Product Name
Product_bb nvarchar Product Introduction
Product_price int market price
Product_discount int discount price
Product_img img Image
Product_status nvarchar status
Product_first int deposit rate
Product_type nvanchar product type
Store Product Information
The javabean: bka. class made of bka. java provides database operations.
In addition, you must register bka. DSN in the system dsn of the control panel,
So that JSP through the JDBC-ODBC to call sqldatabase.
To call javabean on a page, you can use the following methods:
<% @ Page language = "java" import = "java. SQL. *" %>
<Jsp: useBean id = "RegisterBean" scope = "page" class = "shop. bka"/>
<%
String SQL = "select * from xxx ";
ResultSet rs = RegisterBean.exe cuteQuery (SQL );
If (rs. next ()){
Rs. close ();
RegisterBean. closeStmt ();
Session. putValue ("register_message", "duplicate name found! ");
}
%>
Note that the rs should be disabled after use.
The following is the source program of bka. java. Note that you need to use javac to compile it into a class file, that is, a javabean.
-- Shop/bka. java --
Package shop;
Import java. SQL .*;
Public class bka {
String sDBDriver = "sun. jdbc. odbc. JdbcOdbcDriver ";
String sConnStr = "jdbc: odbc: bka ";
Connection conn = null;
ResultSet rs = null;
Public bka (){
Try {
Class. forName (sDBDriver );
}
Catch (java. lang. ClassNotFoundException e ){
System. err. println ("bka ():" + e. getMessage ());
}
}
Public ResultSet executeQuery (String SQL ){
Rs = null;
Try {
Conn = DriverManager. getConnection (sConnStr, "xxx", "yyy ");
Statement stmt = conn. createStatement ();
Rs = stmt.exe cuteQuery (SQL );
}
Catch (SQLException ex ){
System. err. println ("aq.exe cuteQuery:" + ex. getMessage ());
}
Return rs;
}
}