JSP implements shopping programs

Source: Internet
Author: User

Below is the source of my shopping cart Using JSP and database. Program :

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>

clear a shopping cart








border =" 1 "cellpadding =" 2 "cellspacing =" 0 "Width = "100%" bordercolorlight = "# ffb468" bordercolordark = "# ffffff" bgcolor = "# ffb468">





bordercolorlight = "# ffb468" bordercolordark = "# ffffff" Height = "40">

product name
market price
discount price
quantity
subtotal
deposit ratio
deposit subtotal
Delete

<%
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 ));
%>


<% = product_name %>
<% = product_price %>
<% = product_discount %>
" size = "3" value = "<% = cart_quantity %>">
<% = product_d Iscount * cart_quantity %>
<% = product_first + "%" %>
<% = product_first * product_discount * cart_quantity/100.0%>

<%
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);
}< br> catch (sqlexception ex) {
system. err. println ("aq.exe cutequery:" + ex. getmessage ();
}< br> Return Rs;
}< BR >}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.