首先是创建数据库的连接池,代码如下
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://127.0.0.1:3306/java6?useUnicode=true&characterEncoding=utf-8&useSSL=false";
private static final String user = "user01";
private static final String password = "User01User01!";
private static Connection conn = null;
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConn() {
try {
if (conn == null) {
conn = DriverManager.getConnection(url, user, password);
return conn;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
private int id; //商品编号
private String name; //名称
private String city; //产地
private int price; //价格
private int number; //数量
private String picture; //图片
//获得所有商品
public ArrayList getAllItems() {
ArrayList itemsList = new ArrayList();
conn = JDBCPool.getConn();
try {
pstmt = conn.prepareStatement("select * from items");
rs = pstmt.executeQuery();
while (rs.next()) {
Items items = new Items();
items.setId(rs.getInt("id"));
items.setName(rs.getString("name"));
items.setCity(rs.getString("city"));
items.setNumber(rs.getInt("number"));
items.setPrice(rs.getInt("price"));
items.setPicture(rs.getString("picture"));
itemsList.add(items);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if(pstmt != null) {
pstmt.close();
pstmt = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return itemsList;
}
//通过商品id获得商品
public Items getItemsById(int id) {
conn = JDBCPool.getConn();
try {
pstmt = conn.prepareStatement("select * from items where id=?");
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if(rs.next()) {
Items item = new Items();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setCity(rs.getString("city"));
item.setPrice(rs.getInt("price"));
item.setNumber(rs.getInt("number"));
item.setPicture(rs.getString("picture"));
return item;
} else {
return null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(pstmt != null) {
pstmt.close();
pstmt = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//通过客户端保存的cookik获得前五条记录
public ArrayList getViewList(String list) {
int iCount = 5;
ArrayList itemsList = new ArrayList();
String[] arr = list.split(",");
if(arr != null && arr.length > 0) {
if(arr.length >= 5) {
for(int i = arr.length - 1; i >= arr.length - iCount; i--) {
itemsList.add(getItemsById(Integer.parseInt(arr[i])));
}
} else {
for(int i = arr.length - 1; i >= 0; i--) {
itemsList.add(getItemsById(Integer.parseInt(arr[i])));
}
}
return itemsList;
} else {
return null;
}
}
商品展示
<%
ItemsDAO itemsDAO = new ItemsDAO();
ArrayList itemsList = itemsDAO.getAllItems();
for(Items items : itemsList) {
%>
<%
}
%>
后面便是商品详情页
商品详情
<%
ItemsDAO itemsDAO = new ItemsDAO();
Items items = itemsDAO.getItemsById(Integer.parseInt(request.getParameter("id")));
if(items != null) {
%>
<%=items.getName() %>
产地:<%=items.getCity() %>
价格:<%=items.getPrice() %>¥
<%
}
%>
<%
String list = "";
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length > 0) {
for(Cookie cookie : cookies) {
if(("ListViewCookie").equals(cookie.getName())) {
list = cookie.getValue();
}
}
}
list += request.getParameter("id") + ",";
String[] arr = list.split(",");
if(arr != null && arr.length > 0) {
if(arr.length >= 1000) {
list = "";
}
}
Cookie cookie = new Cookie("ListViewCookie",list);
response.addCookie(cookie);
%>
您浏览过的商品
<%
ArrayList itemsList = itemsDAO.getViewList(list);
if(itemsList != null && itemsList.size() > 0) {
for(Items i : itemsList) {
%>
<%
}
}
%>
<%
String list = "";
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length > 0) {
for(Cookie cookie : cookies) {
if(("ListViewCookie").equals(cookie.getName())) {
list = cookie.getValue();
}
}
}
list += request.getParameter("id") + ",";
String[] arr = list.split(",");
if(arr != null && arr.length > 0) {
if(arr.length >= 1000) {
list = "";
}
}
Cookie cookie = new Cookie("ListViewCookie",list);
response.addCookie(cookie);
%>
最后,上图
最后的最后,再为慕课网打下广告吧,我的这些知识都是在慕课网学的,个人感觉网站还不错,不仅有教程,还有免费素材和源代码可以下载,有时间的小伙伴可以去看看上面有什么资源适合你学的.