购物车项目(四)——编写业务逻辑类
2016-06-02 23:29
337 查看
购物车项目(四)——编写业务逻辑类
编写思路:
1.获得所有的商品信息 2.根据商品编号获得商品资料 3.获取最近浏览的前五条商品信息
代码如下:
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; } } }
相关文章推荐
- Spring之PropertyEditorSupport_浅谈
- NDK编程:Android.mk文件详解
- Android 记住密码和自动登录界面的实现(SharedPreferences 的用法)
- 重定向详解
- 红薯 7 种将字符串反转的 Java 方法
- 由已打开的文件读取数据---read
- 我的第一篇html代码
- Oracle issue resolve guide
- Spring 的 IOC 容器和 SpringMVC 的 IOC 容器 关系
- Ubuntu安装MySQL及设置字符集为UTF8
- HDU 3068 最长回文
- CI学习--基本的显示结构探究
- Could not find method multiDexEnabled() for arguments [true] on ProductFlavorD sl_Decorated{name=mai
- Android仿外卖购物车的实现
- 柴俊理金:6、2午夜黄金,宁贵沥青操作策略
- 中软国际机试题
- 事务控制语言(DTL)
- 超平面
- sparkR介绍及安装
- git常用命令的使用(1)