首页>代码>java web开发在线电商购物车系统(jsp+jdbc+spring mvc+bootstrap)>/Ebdb-client/src/com/dspt/dao/impl/GoodsDaoImpl.java
package com.dspt.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dspt.dao.intf.I_GoodsDao;
import com.dspt.entity.GoodsBean;
import com.dspt.util.DBUtil;

/*商品增删改查实现类*/
public class GoodsDaoImpl implements I_GoodsDao {
	public Connection conn;
	public PreparedStatement pst;
	public ResultSet rs;
	/*查询每个大类中小类的具体商品*/
	
	/*Timestamp转String*/
	public static String gettime(Timestamp ts){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Long longtime=ts.getTime();
		Date date=new Date(longtime);
		return sdf.format(date);
	}
	//服务端连接数据库查找对应的商品信息,根据当前页每页显示8条数据
	@Override
	public List<GoodsBean> findgoodsall(int currentpage,String goodstype) throws Exception {
		List<GoodsBean> list=new ArrayList<GoodsBean>();
		GoodsBean goodsbean=null;
		try {
			String sql="select * from goodsinfo where goodstype=? limit ?,10";
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1,goodstype);
			pst.setInt(2,(currentpage-1)*8);
			rs=pst.executeQuery();
			while(rs.next()){
				goodsbean=new GoodsBean();
				goodsbean.setGid(rs.getInt("gid"));
				goodsbean.setGkey(rs.getString("gkey"));
				goodsbean.setGname(rs.getString("gname"));
				goodsbean.setGcolor(rs.getString("gcolor"));
				goodsbean.setGspec(rs.getString("gspec"));
				goodsbean.setGweight(rs.getFloat("gweight"));
				goodsbean.setGpic(rs.getBytes("gpic"));
				goodsbean.setGarea(rs.getString("garea"));
				goodsbean.setGmaker(rs.getString("gmaker"));
				goodsbean.setGdatetime(gettime(rs.getTimestamp("gdate")));//使用实体类的String的时间获取rs得到的数据库的Timestamp型的时间
				goodsbean.setGprice(rs.getFloat("gprice"));
				goodsbean.setGhotprice(rs.getFloat("ghotprice"));
				goodsbean.setIs_push(rs.getBoolean("is_push"));
				goodsbean.setGmout(rs.getInt("gmount"));
				goodsbean.setGindatetime(gettime(rs.getTimestamp("gindate")));
				goodsbean.setAccount(rs.getFloat("account"));
				goodsbean.setGoodstype(rs.getString("goodstype"));
				list.add(goodsbean);//返回list
			}
			return list;
		}finally{
			DBUtil.close(rs);
			DBUtil.close(pst);
			DBUtil.close(conn);
		}
	}
	
	/*根据点击的商品类型进行查找数据库相对应的商品的行数并返回*/
	@Override
	public int findcout(String goodstype) throws Exception {
		String sql="select count(*) from goodsinfo where goodstype=? ";
		conn=DBUtil.getConnection();
		pst=conn.prepareStatement(sql);
		pst.setString(1,goodstype);
		rs=pst.executeQuery();
		int num=0;
		if(rs.next()){
			//取查询到的数据
			num=rs.getInt(1);
		}	
		System.out.println(num);
		return num;
	}
}
最近下载更多
2036495585  LV9 2023年10月6日
lzlzyw  LV14 2022年3月24日
jdndbh  LV6 2021年12月19日
憨代写  LV2 2021年12月5日
〖怪咖〗  LV7 2021年4月11日
1771658781  LV5 2020年12月27日
Demo1111  LV30 2020年12月17日
tnrthr  LV6 2020年12月16日
uuu33442  LV4 2020年11月3日
超级无敌小二狗  LV8 2020年10月8日
最近浏览更多
zolscy  LV12 3月25日
a2418735612  LV1 1月4日
lwqzyx  LV1 2023年12月22日
文法的发射  LV2 2023年11月24日
微信网友_6735233053462528 2023年11月14日
暂无贡献等级
hhhhui  LV1 2023年11月12日
jiemomo  LV12 2023年10月19日
Chuiwaitim  LV2 2023年10月18日
2036495585  LV9 2023年10月6日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友