案例:商品瀏覽記錄的實現

nt1979發表於2021-09-09

說明:
主要技術:使用 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"%>







來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4822/viewspace-2799215/,如需轉載,請註明出處,否則將追究法律責任。

相關文章