模拟亚马逊、淘宝等浏览记录功能(访问数据库)
来源:互联网 发布:钢筋笼计算软件 编辑:程序博客网 时间:2024/05/18 09:07
Q题目
现在要求,模拟一个类似的功能,实体为一个网上书店,要求显示最近的三条浏览记录,使用数据库存储数据和查询数据,如下图
例如:
1)现有如下网上书店:
本站书籍目录: JAVA C++ C语言 Linux Android最近三次浏览记录:
3)浏览第一个商品后,若为Java,显示界面
本站书籍目录: JAVA C++ C语言 Linux Android最近三次浏览记录: JAVA
4)浏览第二个商品,若为C++
5)浏览第三个商品,若为Java,此时涉及一个排序问题了,最前的必须是最近浏览的。
6)浏览第四个商品,为C语言
具体分析见:http://blog.csdn.net/baidu_37107022/article/details/72783206
实现代码
目录
注意:数据库驱动jar包,以及配置文件info.properties
数据库数据如下:
book类
package com.tcb.domain;public class Book { private int id; private String name; private int price; private String author; 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 int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Book(String name, int price, String author) { super(); this.name = name; this.price = price; this.author = author; } @Override public String toString() { return "Book [id=" + id + ", name=" + name + ", price=" + price + ", author=" + author + "]"; } public Book() { super(); // TODO Auto-generated constructor stub }}
bookDao类:查询数据库内容
package com.tcb.dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import com.tcb.domain.Book;import com.tcb.utils.DBUtils;public class BookDao { //查询指定书籍的详情 public Book queryBook(int id){ Connection connection=DBUtils.getConnection(); try { String sql="select * from book where id=?"; PreparedStatement ps=connection.prepareStatement(sql); ps.setInt(1, id); ResultSet resultSet=ps.executeQuery(); while(resultSet.next()){ int id2= resultSet.getInt("id"); String name=resultSet.getString("name"); int price=resultSet.getInt("price"); String author=resultSet.getString("author"); Book book=new Book(name, price, author); book.setId(id2); return book; } } catch (SQLException e) { e.printStackTrace(); } return null; } //查询出所有书籍信息用于展示 public ArrayList<Book> queryAllBook(){ Connection connection=DBUtils.getConnection(); ArrayList<Book> list=new ArrayList<>(); try { String sql="select * from book"; PreparedStatement ps=connection.prepareStatement(sql); ResultSet resultSet=ps.executeQuery(); while(resultSet.next()){ int id=resultSet.getInt("id"); String name=resultSet.getString("name"); int price=resultSet.getInt("price"); String author=resultSet.getString("author"); Book book=new Book(name, price, author); book.setId(id); list.add(book); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; }}
工具类:获得数据库连接Connection
package com.tcb.utils;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ResourceBundle;public class DBUtils { public static String driverName=""; public static String url=""; public static String username=""; public static String password=""; static{ ResourceBundle resourceBundle=ResourceBundle.getBundle("info"); driverName=resourceBundle.getString("driverName"); url=resourceBundle.getString("url"); username=resourceBundle.getString("username"); password=resourceBundle.getString("password"); } public static Connection getConnection(){ try { //1.注册驱动 Class.forName(driverName); //2.获取连接 Connection connection = DriverManager.getConnection(url,username,password); return connection; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void closeAll(ResultSet resultSet,Statement statement ,Connection connection){ if (resultSet!=null) { try { resultSet.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (statement!=null) { try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (connection!=null) { try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
配置文件info.properties
url的最后一个参数:book为数据库名称(自己定)
数据库的用户名username和密码password(根据自己设置的填写)
driverName=com.mysql.jdbc.Driverurl=jdbc\:mysql\://localhost\:3306/bookusername=rootpassword=123
展示所有书籍BookContent
package com.tcb.web.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.tcb.dao.BookDao;import com.tcb.domain.Book;import com.tcb.utils.DBUtils;public class ShowBookContent extends HttpServlet { private static final long serialVersionUID = 1L; public ShowBookContent() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); // 展示所有书籍 writer.write("本站书籍目录:<br>"); BookDao bookDao=new BookDao(); ArrayList<Book> list=bookDao.queryAllBook(); for (Book book : list) { writer.write("<a href='" + request.getContextPath() + "/ShowBookDetails?id=" + book.getId()+ "'>" + book.getName() + "</a><br>"); } // 显示最近三次的访问记录 writer.write("最近三次浏览记录:<br>"); Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { String name = cookie.getName(); if ("ids".equals(name)) { String value = cookie.getValue(); String[] values = value.split("-"); for (int i = 0; i < values.length; i++) { String id = values[i]; Book book1 = bookDao.queryBook(Integer.parseInt(id)); writer.write("<a href='ShowBookDetails?id="+book1.getId() + "'>" + book1.getName() + "</a><br>"); } } } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}
展示书籍具体信息
package com.tcb.web.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.Arrays;import java.util.LinkedList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.tcb.dao.BookDao;import com.tcb.domain.Book;import com.tcb.utils.DBUtils;public class ShowBookDetails extends HttpServlet { private static final long serialVersionUID = 1L; public ShowBookDetails() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); // 显示被查看书籍的具体信息 BookDao bookDao=new BookDao(); String id = request.getParameter("id"); Book book = bookDao.queryBook(Integer.parseInt(id)); writer.write("您查看了" + book.getName() + "这本书。<br><br>"); writer.write("书籍具体信息如下:<br>"); writer.write(book.getName() + "<br>"); writer.write(book.getAuthor() + "<br>"); writer.write(book.getPrice() + "<br>"); // 存储该次访问记录,方便在主页面中显示最近的访问记录 // 存取数据格式为:3-2-1 先要进行数据拆分,根据情况做修改数据操作,再将数据合并放回到cookie中 Cookie[] cookies = request.getCookies(); if (cookies != null) { //创建一个Stringbuffer来存取所有Cookie的名字,用于判断Cookies数组中是否有ids这个Cookie,没有就需要创建 StringBuffer cookieNames=new StringBuffer(); //遍历Cookies数组 for (Cookie cookie : cookies) { //得到Cookie名字 String name = cookie.getName(); //将Cookie名字存起来 cookieNames.append(name+"-"); //加“-”目的是:防止分隔开每个Cookie的名字,防止相连的名字组合匹配到ids //若有ids这个Cookie,就需要做处理 if ("ids".equals(name)) { // 取出Cookie中存的值 String value = cookie.getValue(); // ids这个Cookie取出的值为空那就没必要做拆分了 if (value != null && !"".equals(value)) { String[] values = value.split("-"); // 数组转化成Collection集合 List<String> list = Arrays.asList(values); // 把list集合直接转化成LinkedList,为了方便后续处理 LinkedList<String> linked = new LinkedList<String>(list); // 判断Cookie值中包含id否 if (linked.contains(id)) { linked.remove(id); } else { if (linked.size() > 2) { linked.removeLast(); } } linked.addFirst(id); // 将存进linkedList中的数据取出来组合成1-2或1-2-3等形式,放到Cookie中 String result = ""; for (int i = 0; i < linked.size(); i++) { result += linked.get(i); if (i < linked.size()-1) { result += "-"; } } Cookie cookie2 = new Cookie("ids", result); cookie2.setMaxAge(10 * 60); response.addCookie(cookie2); } } } if(!cookieNames.toString().contains("ids")){ Cookie cookie = new Cookie("ids", id); cookie.setMaxAge(10 * 60); response.addCookie(cookie); } } else { Cookie cookie = new Cookie("ids", id); cookie.setMaxAge(10 * 60); response.addCookie(cookie); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}
不使用,而使用集合实现数据存储实现该功能:http://blog.csdn.net/baidu_37107022/article/details/72783206
阅读全文
1 0
- 模拟亚马逊、淘宝等浏览记录功能(访问数据库)
- 模拟亚马逊、淘宝等浏览记录(历史记录)功能(访问集合存储的数据)
- 淘宝浏览足迹功能实现(含代码)--cookie案例
- php 浏览记录功能
- 数据库学习实践(二)----记录浏览
- servlet实现浏览记录功能
- 模拟浏览器访问增加页面浏览数量
- 访问统计(浏览过的页面)[数据库设计]
- android 数据重构(仿淘宝浏览记录,足迹)
- 淘宝亚马逊开撕
- 用户的浏览商品记录功能代码
- 【iOS_GitHub】评分条/星级条(类似于淘宝/京东/1号店/亚马逊等商城类的评分条,支持0.5分哦)
- Visual C#中轻松浏览数据库记录
- 用VisualC#中轻松浏览数据库记录
- 一个数据库简易浏览功能的实现
- JAVA、jsp、数据库实现分页浏览功能
- JDBC访问Oracle数据库例子源代码,包括创建table,删除table,插入记录,删除记录,查询记录等
- 如何实现访问apache时的目录浏览功能(apache file list view)
- SSL certificate problem: unable to get local issuer certificate 解决方法
- hibernate导致出现500错误
- tensorflow:1)简单的神经网络
- POJ
- hdu1241 Oil Deposits (bfs水题)
- 模拟亚马逊、淘宝等浏览记录功能(访问数据库)
- struts2第一次配置并且运行
- Github入门资料汇总
- 【bzoj1709】[Usaco2007 Oct]Super Paintball超级弹珠
- 曾经的程序猿,现在的七夜在舞步学院,丢掉不会恋爱的帽子
- zoj1081(点与多边形的位置关系)
- linux设备驱动归纳总结(六):1.中断的实现
- socket通信---并发服务器图解和阻塞通信图解
- codeforces——339A——Helpful Maths