java 逻辑_java-逻辑处理

packagedao;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.util.ArrayList;importutil.DBHelper;importentity.Items;//商品的业务逻辑类

public classItemsDAO {//获得所有的商品信息

public ArrayListgetAllItems() {

Connection conn= null;

PreparedStatement stmt= null;

ResultSet rs= null;

ArrayList list = new ArrayList(); //商品集合

try{

conn=DBHelper.getConnection();

String sql= "select * from items;"; //SQL语句

stmt =conn.prepareStatement(sql);

rs=stmt.executeQuery();while(rs.next()) {

Items item= newItems();

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(intid) {

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= newItems();

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"));returnitem;

}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 ArrayListgetViewList(String list)

{

System.out.println("list:"+list);

ArrayList itemlist = new ArrayList();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])));

}

}returnitemlist;

}else{return null;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值