用JSP和数据库实现购物车的源程序(2)
接上一篇文章。
<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">商品名称</td>
<td width="10%" height="8" align="left">市场价</td>
<td width="10%" height="8" align="left">优惠价</td>
<td width="10%" height="8" align="left">数量</td>
<td width="14%" height="8" align="left">小计</td>
<td width="12%" height="8" align="left">定金比例</td>
<td width="17%" height="8" align="left">定金小计</td>
<td width="17%" height="8" align="left">删除</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>总计</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>定金总计</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>结余</td>
<td width="36%" colspan="2" height="16"><div align="left">
<%=total-total_first%></td>
</tr>
</table>
</center></div>
<%
}else{
%>
<p align="center">购物车为空!</p>
<%
}
%>
数据库操作部分
程序用到两个表:
1 shop_cart表
cart_id int 购物车编号 自动编号
cart_shop_id nvarchar 商店编号
cart_product_id nvarchar 商品编号
cart_quantity int 商品数量
临时存放购物车数据
2 shop_product表
product_id int 商品编号 自动编号
shop_id nvarchar 商店编号
product_name nvarchar 商品名称
product_bb nvarchar 商品介绍
product_price int 市场价
product_discount int 优惠价
product_img img 图片
product_status nvarchar 状态
product_first int 定金比例
product_type nvanchar 商品类型
存放商品资料
使用bka.java制成的javabean:bka.class可以提供对数据库的操作。
另外,需在控制面板的系统DSN中注册bka.dsn,
从而可使JSP通过JDBC-ODBC来调用sql数据库。
在页面中调用javabean,基本上可采用以下方式:
<%@ 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!");
}
%>
注意应在使用后将rs关闭。
以下是bka.java的源程序。注意在使用前需用javac加以编译成为class
文件即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;
}
}
文章来源于领测软件测试网 https://www.ltesting.net/