简单购物车项目(gouwuche) - 阳是六時起
余生好长 你好难忘 变优秀 等你
作者 时间 分类 学习 浏览 1278 评论

网站后台代码结构
1.png

网站前端代码结构
2.png

数据库结构
3.png

商品表
211.png

购物车表
12.png


jdbcDAO (数据库封装)

package cn.limif.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class jdbcDAO {
private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
public PreparedStatement getPs() {
return ps;
}
public void setPs(PreparedStatement ps) {
this.ps = ps;
}
public ResultSet getRs() {
return rs;
}
public void setRs(ResultSet rs) {
this.rs = rs;
}
}


getConn.java(链接数据库)

package cn.limif.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class getConn {
Connection conn = null ;
PreparedStatement ps = null;
ResultSet rs = null;
String DBDriver = "com.mysql.jdbc.Driver";
String DBUrl = "jdbc:mysql://localhost:3306/gouwuche?useUnicode=true&characterEncoding=UTF-8";
String DBName = "root";
String DBPassword = "";
public Connection getConnection(){
try {
Class.forName(DBDriver);
} catch (ClassNotFoundException e) {
System.out.println("驱动加载失败");
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(DBUrl, DBName, DBPassword);
} catch (SQLException e) {
System.out.println("连接数据库失败");
e.printStackTrace();
}
return conn;
}
public PreparedStatement getPs(String sql){
try {
ps = conn.prepareStatement(sql);
} catch (SQLException e) {
System.out.println("创建声明失败");
e.printStackTrace();
}
return ps;
}
public void closeAll(Connection conn, PreparedStatement ps, ResultSet rs){
if(rs !=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
closeAll(conn, ps);
}
public void closeAll(Connection conn, PreparedStatement ps){
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}


spDAO.java(商品封装)

package cn.limif.daoImpl;

public class spDAO {
int bid;
String bname;
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public spDAO(int bid, String bname) {
this.bid = bid;
this.bname = bname;
}
public spDAO() {
}
}


sp.java(读取商品信息)

package cn.limif.daoImpl;

import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import cn.limif.jdbc.getConn;
import cn.limif.daoImpl.spDAO;;
public class sp {

public List spinfo(){
List spList = new ArrayList();
spDAO spd = new spDAO();
jdbcDAO jd = new jdbcDAO();
getConn gc = new getConn();
String sql = "select * from shangpin";
jd.setConn(gc.getConnection());
jd.setPs(gc.getPs(sql));
try {
jd.setRs(jd.getPs().executeQuery());
while(jd.getRs().next()){
spd.setBid(jd.getRs().getInt("id"));
spd.setBname(jd.getRs().getString("bname"));
spDAO sparr = new spDAO(spd.getBid(), spd.getBname());
spList.add(sparr);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
gc.closeAll(jd.getConn(), jd.getPs(), jd.getRs());
}
return spList;
}
}


gwcDAO.java(购物车封装)

package cn.limif.daoImpl;

public class gwcDAO {
int gid;
String gname;
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public gwcDAO () {
}
public gwcDAO(int gid, String gname) {
this.gid = gid;
this.gname = gname;
}
}


gwc.java(读取购物车信息)

package cn.limif.daoImpl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.limif.jdbc.getConn;

public class gwc {
public List gwcinfo(){
List gwList = new ArrayList();
gwcDAO gd = new gwcDAO();
jdbcDAO jd = new jdbcDAO();
getConn gc = new getConn();
String sql = "select * from gouwuche";
jd.setConn(gc.getConnection());
jd.setPs(gc.getPs(sql));
try {
jd.setRs(jd.getPs().executeQuery());
while(jd.getRs().next()){
gd.setGid(jd.getRs().getInt("gid"));
gd.setGname(jd.getRs().getString("gname"));
gwcDAO gwArr = new gwcDAO(gd.getGid(), gd.getGname());
gwList.add(gwArr);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
gc.closeAll(jd.getConn(), jd.getPs(), jd.getRs());
}
return gwList;
}
}


choose.jsp(商品页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@page import="java.util.List"%>
<%@page import="cn.limif.daoImpl.sp"%>
<%@page import="cn.limif.daoImpl.spDAO"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
%>
<title>商品列表</title>
</head>
<body>
<div>
<form action="addShopCart" method="post">

<% sp sp1 = new sp(); List<spDAO> sp1List = sp1.spinfo(); for(spDAO sp2List: sp1List) { %> <% } %>
商品编号 商品名称
<input type="checkbox" name="item" value="<%=sp2List.getBname() %>" onclick="checkboxOnclick(this)" ><%=sp2List.getBid() %> <%=sp2List.getBname() %>
<input type="submit" value="加入购物车" onclick="checkbox()"> 我的购物车

</form>
<script>

function checkboxOnclick(checkbox){

if ( checkbox.checked == true){
alert("感谢选择【"+ checkbox.value + "】")

}else{

}
}

function checkbox()
{
var str=document.getElementsByName("item");
var objarray=str.length;
var chestr="";
for (i=0;i<objarray;i++)
{
if(str[i].checked == true){
chestr+=str[i].value+" ";
}
}
if(chestr == ""){
alert("请选择内容")
return false;
}
else{
// alert("感谢选择【"+ chestr + "】")
}
}
</script>
</div>
</body>
</html>


ShoppingCart.jsp(购物车页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@page import="java.util.List"%>
<%@page import="cn.limif.daoImpl.gwc"%>
<%@page import="cn.limif.daoImpl.gwcDAO"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>我的购物车</title>
</head>
<body>
<div>

<% gwc gwc1 = new gwc(); List<gwcDAO> gwc1List = gwc1.gwcinfo(); for(gwcDAO gwc2List: gwc1List) { %> <% } %>
我的购物车
商品名称 操作
<%=gwc2List.getGname() %>

继续购物

</div>
</body>
</html>




微信公众号:妙月琵琶语
微信阅读


上一篇: 原来并不理想     |     下一篇: myeclipse 10.7.1 安装svn插件



添加新评论