JSP to implement shopping program

Time:2022-6-10

The following is the source program of the shopping cart I made with JSP and database:

be careful:
1. The re billing part has not been completed yet. Let’s do it ourselves!
2. The next version will use 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;

//Commodity type
if (request.getParameter(“product_type”)==null){
product_type=”all”;
}else{
product_type=request.getParameter(“product_type”);
}

//Page number and product type parameters, you can return 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”);
}

//Item No
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>
<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>
<td width=”100%”><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><a

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><a

Href= “javascript: document.forms[0].submit()” > re billing</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\u type=<%=product\u type%>&curpage=<%=curpage%>” > empty the 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” > Product Name</td>
<td width= “10%” height= “8” align= “left” > market price</td>
<td width= “10%” height= “8” align= “left” > preferential 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 proportion</td>
<td width= “17%” height= “8” align= “left” > sub total of deposit</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,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_id=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>
<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 operation part

The program uses two tables:
1 shop_ Cart table
cart_ Id int cart number auto number
cart_ shop_ ID nvarchar store number
cart_ product_ ID nvarchar article number
cart_ Quantity int quantity of goods
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
product_ IMG img picture
product_ Status nvarchar status
product_ First int deposit proportion
product_ Type nvanchar item type
Store commodity data

Use bka JavaBean made of Java: bka Class can provide operations on the database.

In addition, BKA needs to be registered in the system DSN of the control panel dsn,
This enables JSP to call SQL database through jdbc-odbc.
Basically, the following methods can be used to call JavaBeans in the page:

<%@ 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 that RS should be turned off after use.

Here is bka Java source program. Note that you need javac to compile it into a class file, namely 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;
}
}