# 基于javaWeb实现的购物商城设计
## 1.项目简介
### 1.1总体结构
**src文件**
![](https://www.writebug.com/myres/static/uploads/2021/10/24/cc436ed35175b18871ad236e95ea3786.writebug)
>model:定义的是商品类Goodselement和用户类Member
>tools:定义CheckCode验证码处理的servlet,Chstr字符转换处理,connDB连接数据库
>dao:实现数据库用户表的查询和插入操作
**数据库文件**
两个sql server的数据库文件,直接用sql server manager的导入可以添加数据库
![](https://www.writebug.com/myres/static/uploads/2021/10/24/89fe8a11aa0773a6cb0513b1638242ae.writebug)
**前端页面**
![](https://www.writebug.com/myres/static/uploads/2021/10/24/447ddb7aed74228597b5a09412dd055f.writebug)
>login:登录界面
>register:注册界面
>cart:购物车界面
>common:网页上下栏目
>goodsDetail:商品详情
>leftHotGoods:左边栏显示热门商品
>relateGoods:下边栏显示相关商品
>agreement.html:网站注册协议
>index:主页面
### 1.2主页面
![](https://www.writebug.com/myres/static/uploads/2021/10/24/231dd8ecbc3ccd2fb211a894c6f2545b.writebug)
**最新商品显示**
![](https://www.writebug.com/myres/static/uploads/2021/10/24/bbbd573ad20e8ccb10ce6fd6d5221ce9.writebug)
**打折商品显示**
![](https://www.writebug.com/myres/static/uploads/2021/10/24/79611fb1694658e1670bbcc9d224e83e.writebug)
**热门商品显示**
![](https://www.writebug.com/myres/static/uploads/2021/10/24/640d0af9685aee2e9520acaab9d71660.writebug)
### 1.3商品详情页面
![](https://www.writebug.com/myres/static/uploads/2021/10/24/d044970152acee598baae7acdd81387d.writebug)
### 1.4购物车
![](https://www.writebug.com/myres/static/uploads/2021/10/24/4723130646bd21e503c718325744956f.writebug)
### 1.5登录和注册
**登录**<br>
![](https://www.writebug.com/myres/static/uploads/2021/10/24/783b3e772600221bb24dadb975673f7d.writebug)<br>
**注册**<br>
![](https://www.writebug.com/myres/static/uploads/2021/10/24/4c2423669d1af79248aca90810b000f9.writebug)
## 2.数据库连接
数据库采用的是sql server数据库,连接数据库用到的方法写进一个类connDB当中(类放在com.tools当中),每次需要访问数据库时,实例化这个类,并调用它封装的方法。数据库是已经在sql server创建好的db_shop
成员变量:
```java
public Connection conn = null; // 数据库连接对象
public Statement stmt = null; // statement对象,用于执行Sql语句
public ResultSet rs = null; // 结果对象集
// 驱动类的类名
private static String dbClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String dbUrl = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=db_shop";
private static String dbUser = "sa"; // 登录sql的用户名
private static String dbPwd = "****"; // 登录sql的密码
```
### 2.1创建连接
```java
/*
* 创建与数据库的连接
*/
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(dbClassName).newInstance(); // 装在数据库驱动
// 获取数据库连接对象
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
} catch (Exception e) {
e.printStackTrace();
}
if (conn == null) {
System.err.println("DbConnectionManger.getConnection():" + dbClassName + "\r\n" + dbUrl + "\r\n" + dbUser
+ "/" + dbPwd);
} else {
System.out.println("数据库连接成功");
}
return conn;
}
```
### 2.2更新数据
```java
/*
* 更新数据功能
*/
public int executeUpdate(String sql) {
int result = 0; // 更新数据的记录条数
try {
conn = getConnection(); // 获取数据库连接
// 创建用于执行SQL语句的statement对象
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
result = stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO: handle exception
result = 0;
e.printStackTrace(); // 输出异常信息
}
try {
stmt.close();
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return result;
}
```
### 2.3查询数据
```java
/*
* 根据指定的sql语句查询数据
*/
public ResultSet executeQuery(String sql) {
try {
conn = getConnection(); // 获取数据库连接
// 创建用于执行SQL语句的statement对象
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql); // 执行SQL语句
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return rs;
}
```
### 2.4关闭数据库连接
```java
/*
* 关闭数据库
*/
public void close() {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace(System.err);
}
}
```
### 2.5测试一下
写好之后,需要将sql server的jdbc连接jar包导入,这里用到**sqljdbc42.jar**,然后写一个junit测试方法单独试一下
```java
@Test
public void test() {
if (getConnection() != null) {
System.out.println("测试连接成功");
}
}
```
## 3.商品显示
### 3.1数据库
后台数据库用到了sql server,建立了商品表tb_goods和商品分类表tb_subType
**商品分类表:**
![](https://www.writebug.com/myres/static/uploads/2021/10/24/6358cdee2f3788f900ca7a90147f322e.writebug)
**商品表:**
![](https://www.writebug.com/myres/static/uploads/2021/10/24/25848a944c0e1e5a15dea1ddc098515a.writebug)
### 3.2获得最新商品
```java
/* 最新上架商品信息 */
ResultSet rs_new = conn.executeQuery(
"select top 12 t1.ID, t1.GoodsName,t1.price,t1.picture,t2.TypeName "
+"from tb_goods t1,tb_subType t2 where t1.typeID=t2.ID and "
+"t1.newGoods=1 order by t1.INTime desc"); //查询最新上架商品信息
int new_ID=0; //保存最新上架商品的id变量
String new_goodsname=""; //保存最新上架商品名称的变量
float new_nowprice=0; //保存最新上架商品的价格变量
String new_picture=""; //保存最新上架商品的图片变量
String typeName=""; //保存最新上架商品的分类变量
```
前端页面循环显示
```html
<!-- 循环显示最新上架商品 :添加12条商品信息-->
<%
while(rs_new.next()){
new_ID=rs_new.getInt(1); //最新上架商品的id
new_goodsname=rs_new.getString(2); //最新上架商品的名称
new_nowprice=rs_new.getFloat(3); //最新上架商品当前价格
new_picture=rs_new.getString(4); //最新上架商品的图片
typeName=rs_new.getString(5); //最新上架商品的类别
%>
<!--循环显示的html代码-->
<%}%>
```
![](https://www.writebug.com/myres/static/uploads/2021/10/24/e9fc78391f4b9494a1972ea220bfd42c.writebug)
### 3.2获得打折商品
```java
ResultSet rs_sale = conn.executeQuery(
"select top 12 t1.ID, t1.GoodsName,t1.price,t1.nowPrice,t1.picture,t2.TypeName "
+"from tb_goods t1,tb_subType t2 where t1.typeID=t2.ID and t1.sale=1 "
+"order by t1.INTime desc"); //查询打折商品信息
int sale_ID=0; //保存打折商品的id变量
String s_goodsname=""; //保存打折商品名称的变量
float s_nowprice=0; //保存打�
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
login:登录界面 register:注册界面 cart:购物车界面 common:网页上下栏目 goodsDetail:商品详情 leftHotGoods:左边栏显示热门商品 relateGoods:下边栏显示相关商品 agreement.html:网站注册协议 model:定义的是商品类Goodselement和用户类Member tools:定义CheckCode验证码处理的servlet,Chstr字符转换处理,connDB连接数据库 dao:实现数据库用户表的查询和插入操作
资源推荐
资源详情
资源评论
收起资源包目录
基于JavaWeb+SQL Server设计实现的购物商城【100012813】 (400个子文件)
CheckCode.class 5KB
ConnDB.class 3KB
MemberDaoImpl.class 3KB
Member.class 3KB
ChStr.class 863B
Goodselement.class 338B
MemberDao.class 186B
.classpath 964B
org.eclipse.wst.common.component 456B
org.eclipse.wst.jsdt.ui.superType.container 49B
mr-01.css 52KB
样式说明(以Green皮肤为例).css 4KB
jbox.css 4KB
jbox.css 4KB
jbox.css 4KB
jbox.css 4KB
jbox.css 4KB
jbox.css 4KB
样式说明(以Default皮肤为例).css 3KB
jbox.css 3KB
jbox.css 3KB
jbox.css 3KB
jbox.css 3KB
jbox.css 3KB
jbox.css 3KB
jbox.css 3KB
jbox.css 3KB
jbox.css 3KB
jbox.css 3KB
jbox.css 3KB
jbox.css 3KB
jbox.css 3KB
jbox.css 2KB
fontawesome-webfont.eot 75KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-content-loading.gif 11KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-loading3.gif 9KB
jbox-content-loading.gif 6KB
jbox-content-loading.gif 6KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading2.gif 4KB
jbox-loading1.gif 3KB
jbox-loading1.gif 3KB
jbox-loading1.gif 3KB
jbox-loading1.gif 3KB
jbox-loading1.gif 3KB
jbox-loading1.gif 3KB
共 400 条
- 1
- 2
- 3
- 4
资源评论
神仙别闹
- 粉丝: 2674
- 资源: 7640
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 湘江小说在线阅读网站的设计与实现(部署视频)-kaic.mp4
- 基于C++使用OpenCV 2.4.9做点多目标卡尔曼跟踪源码.zip
- 基于python实现二维碎片拼接复原源码+ppt说明+项目说明.zip
- 基于OpenCV的小案例C++源码边缘角点轮廓图像分割增强拼接目标检测颜色直方图抠图人脸识别等.zip
- 纯python3和numpy实现的符号计算深度学习框架.zip
- 微信小程序 - 涂鸦源码.zip
- 基于C# winform+mysql人事工资管理系统源码+sql文件+运行说明.zip
- 基于Java web搭建的一个简易博客网站源码.rar
- 基于Java web搭建的一个简易博客网站源码.zip
- 基于C#+MySql实现的药品销售库存信息管理系统源码+数据库+项目说明.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功