类名是ItemDAO

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList; import util.DBHelper; import entity.Items; //商品的业务逻辑类
public class ItemsDAO { // 获得所有的商品信息
public ArrayList<Items> getAllItems() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList<Items> list = new ArrayList<Items>(); // 商品集合
try {
conn = DBHelper.getConnection();
String sql = "select * from items;"; // SQL语句
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
Items item = new Items();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setCity(rs.getString("city"));
item.setNumber(rs.getInt("number"));
item.setPrice(rs.getInt("price"));
item.setPicture(rs.getString("picture"));
list.add(item);// 把一个商品加入集合
}
return list; // 返回集合。
} catch (Exception ex) {
ex.printStackTrace();
return null;
} finally {
// 释放数据集对象
if (rs != null) {
try {
rs.close();
rs = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 释放语句对象
if (stmt != null) {
try {
stmt.close();
stmt = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
} } // 根据商品编号获得商品资料
public Items getItemsById(int id) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DBHelper.getConnection();
String sql = "select * from items where id=?;"; // SQL语句
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
if (rs.next()) {
Items item = new Items();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setCity(rs.getString("city"));
item.setNumber(rs.getInt("number"));
item.setPrice(rs.getInt("price"));
item.setPicture(rs.getString("picture"));
return item;
} else {
return null;
} } catch (Exception ex) {
ex.printStackTrace();
return null;
} finally {
// 释放数据集对象
if (rs != null) {
try {
rs.close();
rs = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 释放语句对象
if (stmt != null) {
try {
stmt.close();
stmt = null;
} catch (Exception ex) {
ex.printStackTrace();
}
} }
}
//获取最近浏览的前五条商品信息
public ArrayList<Items> getViewList(String list)
{
System.out.println("list:"+list);
ArrayList<Items> itemlist = new ArrayList<Items>();
int iCount=5; //每次返回前五条记录
if(list!=null&&list.length()>0)
{
String[] arr = list.split(",");
System.out.println("arr.length="+arr.length);
//如果商品记录大于等于5条
if(arr.length>=5)
{
for(int i=arr.length-1;i>=arr.length-iCount;i--)
{
itemlist.add(getItemsById(Integer.parseInt(arr[i])));
}
}
else
{
for(int i=arr.length-1;i>=0;i--)
{
itemlist.add(getItemsById(Integer.parseInt(arr[i])));
}
}
return itemlist;
}
else
{
return null;
} } }

最新文章

  1. IOS开发-UIScrollView陷阱之----删除所有子view, 滚动条(indicator) 消失
  2. Vim光标定位
  3. iOS - HTTPS接口加密和身份认证
  4. wampserver安装之后连接phpMyAdmin 不成功的解决方法
  5. iOS的一些坑
  6. BI与大数据
  7. sql常识-IN 操作符
  8. mongodb在window下和linux下的部署 和 安装可视化工具
  9. 总结:spring 的ioc 知识点
  10. cf C. Find Maximum
  11. SpringMVC表单中post请求转换为put或delete请求
  12. win10 UWP读写文件
  13. 【jq】prop和attr的区别
  14. graph easy绘制ascii简易流程图
  15. 集腋成裘-02-css基础-01
  16. 【BZOJ】3214: [Zjoi2013]丽洁体
  17. 粗略的整改一下blog
  18. Serial Wire Debug (SWD) Interface -- PSoc5
  19. C++空类
  20. C陷阱与缺陷读书笔记

热门文章

  1. padding 填充
  2. Qt installer framework学习
  3. win 10 把秘钥清掉之后查不到秘钥怎么办
  4. 【Java多线程系列八】volatile和ThreadLocal
  5. JasperReports教程
  6. apache虚拟主机配置及解析
  7. linux中errno及perror的应用
  8. MVC5+EF6 完整教程
  9. Java面试宝典(5)算法
  10. less&amp;sass