案例:商品浏览记录的实现

说明:
主要技术:使用 cookie 实现商品浏览记录;
项目模型:采用Model 1 (jsp + JavaBean);
步骤:
(1)DBHelper类实现数据库的连接操作;
(2)实体类;数据库表的映射
(3)业务逻辑类;DAO
(4)创建页面;

一、连接数据库:
(1)在WEB-INF的lib下导入jar包;
(2)DBHelper类
package com.mall.utils;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBHelper {

private static final String driver="com.mysql.jdbc.driver";//数据库驱动; 
//连接数据库的url地址
private static final String url="jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=UTF-8"; 
private static final String username="root"; //数据库用户名
private static final String password="root"; //数据库密码
private static Connection conn=null;//数据库连接对象

//静态代码块负责加载驱动;
static {
    try {
        Class.forName(driver);
    }catch(Exception ex){
        ex.printStackTrace();
    }

}
// 单例模式返回数据库连接对象
public static Connection getConnnection() throws Exception{
    // TODO Auto-generated method stub
    if (conn==null) {
        conn = DriverManager.getConnection(url,username,password);
        return conn;
    }
    return conn;
}
//连接测试:
public static void main(String[] args) {
    try {
        Connection conn = DBHelper.getConnnection();
        if (conn != null) {
            System.out.println("数据库连接成功!");
        }else {
            System.out.println("数据库连接异常!");
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}
二、创建数据库表:
items表:商品信息表
图片描述

三、目录结构及源代码:
图片描述

创建实体类:Goods.java;
package com.mall.beans;
//商品类
public class Goods {
private int id;
private String name;
private String city;//产地
private int price;
private int number;//库存
private String picture;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
}

业务逻辑类:GoodsDao.java;
package com.mall.dao;

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

import com.mall.beans.Goods;
import com.mall.utils.DBHelper;

public class GoodsDao {
//获得所有商品信息
public ArrayList getAllGoods(){
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
//商品集合
ArrayList goodsList = new ArrayList();
try {
conn = DBHelper.getConnnection();
String sql = "select * from goods;";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while(rs.next()) {
Goods goods = new Goods();
goods.setId(rs.getInt("id"));
goods.setName(rs.getString("name"));
goods.setCity(rs.getString("city"));
goods.setNumber(rs.getInt("number"));
goods.setPrice(rs.getInt("price"));
goods.setPicture(rs.getString("picture"));
goodsList.add(goods);
}
return goodsList;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} finally {
//释放数据集对象
if(rs != null) {
try {
rs.close();
rs = null;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//释放语句对象
if(stmt != null) {
try {
stmt.close();
stmt = null;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

    }
}
//根据商品编号获得商品资料;
public Goods getGoodsById(int id) {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        conn = DBHelper.getConnnection();
        String sql = "select * from goods where id=?;";
        stmt = conn.prepareStatement(sql);
        stmt.setInt(1, id);
        rs = stmt.executeQuery();
        if(rs.next()) {
            Goods goods = new Goods();
            goods.setId(rs.getInt("id"));
            goods.setName(rs.getString("name"));
            goods.setCity(rs.getString("city"));
            goods.setNumber(rs.getInt("number"));
            goods.setPrice(rs.getInt("price"));
            goods.setPicture(rs.getString("picture"));
            return goods;
        }else {
            return null;
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    } finally {
        //释放数据集对象
        if(rs != null) {
            try {
                rs.close();
                rs = null;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //释放语句对象
        if(stmt != null) {
            try {
                stmt.close();
                stmt = null;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}
//使用cookie保存浏览记录;
public ArrayList getViewList(String list){
    ArrayList goodsList = new ArrayList();
    int iCount=5;   //每次返回前5条;
    if(list != null && list.length()>0) {
        String[] arr = list.split("#");
        //如果商品记录大于等于"iCount"条,倒叙输出最后"iCount"条;
        if(arr.length>=iCount) {
            for(int i = arr.length-1;i>=arr.length-iCount;i--) {
                goodsList.add(getGoodsById(Integer.parseInt(arr[i])));
            }
        }
        else {//否则直接倒叙输出;
            for(int i = arr.length-1;i>=0;i--) {
                goodsList.add(getGoodsById(Integer.parseInt(arr[i])));
            }
        }

// for(String s:arr) {
// int id = Integer.parseInt(s);
// goodsList.add(getGoodsById(id));
// }
return goodsList;
}
else {
return null;
}

}

}

商品展示页面:index.jsp;
pageEncoding="UTF-8"%>









商品展示页面:goods_details.jsp
pageEncoding="UTF-8"%>







请使用浏览器的分享功能分享到微信等