JSP realizes shopping program _jsp programming

Source: Internet
Author: User
Tags dsn getmessage odbc stmt
Here are my JSP and database to do the shopping cart source program:

Attention:
1, the recosting part has not been done, everyone to do it yourself!
2, the next version will be done with 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;

Type of product
if (Request.getparameter ("Product_type") ==null) {
Product_type= "All";
}else{
Product_type=request.getparameter ("Product_type");
}

Page number and commodity type parameters, which can be returned to the last shopping item 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 number
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 number
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.executequery (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.executequery (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.executequery (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.executequery (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.executequery (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>
&LT;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%" ><div

align= "center" ><center><p> Shopping Cart </td>
</tr>
</table>
</center></div></td>
&LT;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>
&LT;TD width= "100%" ><div align= "center" ><center><p><a

Href= "shop_list.jsp?shop_id=<%=shop_id%>&product_type=<%=product_type%>

&curpage=<%=curpage%> "> Continue shopping </a></td>
</tr>
</table>
</center></div></td>
&LT;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>
&LT;TD width= "100%" ><div align= "center" ><center><p><a

Href= "Javascript:document.forms[0].submit ()" > re-billing </a></td>
</tr>
</table>
</center></div></td>
&LT;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>
&LT;TD width= "100%" ><div align= "center" ><center><p>

<a href= "Shop_cart.jsp?action=clear&product_type=<%=product_type%>&curpage=<%=curpage%>" > Empty Shopping Cart </a></td>
</tr>
</table>
</center></div></td>
&LT;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>
&LT;TD width= "100%" ><div align= "center" ><center><p><a href= "shop_order.asp" > Confirm purchase </a ></td>
</tr>
</table>
</center></div></td>
</tr>
&LT;TR align= "center" >
&LT;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= ">"
<tr>
&LT;TD width= "20%" height= "8" align= "left" > Product name </td>
&LT;TD width= "10%" height= "8" align= "Left" > Market price </td>
&LT;TD width= "10%" height= "8" align= "left" > Preferential price </td>
&LT;TD width= "10%" height= "8" align= "left" > Quantity </td>
&LT;TD width= "14%" height= "8" align= "left" > Subtotal </td>
&LT;TD width= "12%" height= "8" align= "left" > Deposit ratio </td>
&LT;TD width= "17%" height= "8" align= "left" > Deposit Subtotal </td>
&LT;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,shop_cart.cart_quantity,shop_product.product_first from Shop_cart,shop_product where shop_cart.cart_shop_id= "+ shop_id +" and shop_cart.cart_guest_id= ' "+ guest_name +" ' and shop_cart.cart_product_i D=SHOP_PRODUCT.PRODUCT_ID ";
rs = bka.executequery (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>
&LT;TD width= "10%" height= "1" align= "left" ><%=product_name%></td>
&LT;TD width= "10%" height= "1" align= "left" ><%=product_price%></td>
&LT;TD width= "10%" height= "1" align= "left" ><%=product_discount%></td>
&LT;TD width= "10%" height= "1" align= "left" ><input type= "text" name= "<%=" T "+ product_id%>" size= "3" value= " <%=cart_quantity%> "></td>
&LT;TD width= "14%" height= "1" align= "left" ><%=product_discount*cart_quantity%></td>
&LT;TD width= "12%" height= "1" align= "left" ><%=product_first + "%"%></td>
&LT;TD width= "17%" height= "1" align= "left" ><%=product_first*product_discount*cart_quantity/100.0%></ Td>
&LT;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;
}
%>

&LT;TR align= "center" >
&LT;TD width= "72%" colspan= "6" height= "" ><div align= "right" ><p> total </td>
&LT;TD width= "36%" colspan= "2" height= "" ><div align= "left" ><%=total%></td>
</tr>
&LT;TR align= "center" >
&LT;TD width= "72%" colspan= "6" height= "" ><div align= "right" ><p> deposit total </td>
&LT;TD width= "36%" colspan= "2" height= "" ><div align= "left" ><%=total_first%></td>
</tr>
&LT;TR align= "center" >
&LT;TD width= "72%" colspan= "6" height= "" ><div align= "right" ><p> balance </td>
&LT;TD width= "36%" colspan= "2" height= "" ><div align= "left" ><%=total-total_first%></td>
</tr>
</table>
</center></div>

<%
}else{
%>
<p align= "center" > Shopping cart is empty!</p>
<%
}
%>


Database Operations Section

The program uses two tables:
1 Shop_cart Table
cart_id int Shopping Cart number Auto number
cart_shop_id nvarchar Store number
cart_product_id nvarchar Commodity number
cart_quantity int Goods Quantity
Temporary storage of shopping cart data

2 shop_product Table
product_id int Item Number Auto number
shop_id nvarchar Store number
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 State
Product_first int deposit ratio
Product_type Nvanchar Commodity Type
Store commodity Information

Javabean:bka.class made with Bka.java can provide operations on the database.

In addition, you need to register BKA.DSN in the System DSN in the Control Panel,
This allows the JSP to invoke the SQL database through JDBC-ODBC.
Calling JavaBean on a page can basically take the following ways:


<%@ page language= "java" import= "java.sql.*"%>
<jsp:usebean id= "Registerbean" scope= "page" class= "Shop.bka"/>
<%
String sql= "select * from XXX";
ResultSet rs = registerbean.executequery (SQL);
if (Rs.next ()) {
Rs.close ();
Registerbean.closestmt ();
Session.putvalue ("Register_message", "Duplicate name found!");
}
%>
Note the RS should be closed after use.


The following is the source program for Bka.java. Note that you need javac to compile the class file before use 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.executequery (SQL);
}
catch (SQLException ex) {
System.err.println ("Aq.executequery:" + ex.getmessage ());
}
Return RS;
}
}

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.