首页>代码>Java Servlet开发的可用于java毕业设计的甜品糕点整站源码>/zuidaima_cakeshop/src/com/lovo/cq/cakeshop/daoimpl/AdminCakeDaoImpl.java
package com.lovo.cq.cakeshop.daoimpl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.lovo.cq.cakeshop.common.DbUtil; import com.lovo.cq.cakeshop.dao.AdminCakeDao; import com.lovo.cq.cakeshop.po.Cake; import com.lovo.cq.cakeshop.po.CakePager; import com.lovo.cq.cakeshop.po.Inform; import com.lovo.cq.cakeshop.po.InformPager; public class AdminCakeDaoImpl implements AdminCakeDao{ public List getAllCakes() { List cakes = new ArrayList(); DbUtil daoUtil = null; PreparedStatement ps = null; ResultSet rs = null; String sql = null; try { daoUtil = new DbUtil(); sql = "select * from tb_cake"; ps = daoUtil.getCon().prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { Cake cake = new Cake(); cake.setCakeId(rs.getInt("cakeId")); cake.setSuperTypeId(rs.getInt("superTypeId")); cake.setSubTypeId(rs.getInt("subTypeId")); cake.setCakeName(rs.getString("cakeName")); cake.setIntroduce(rs.getString("introduce")); cake.setPrice(rs.getFloat("price")); cake.setNowPrice(rs.getFloat("nowPrice")); cake.setPicture(rs.getString("picture")); cake.setInTime(rs.getString("inTime")); cake.setNewCakes(rs.getInt("newCakes")); cake.setSaleCakes(rs.getInt("saleCakes")); cake.setHostCakes(rs.getInt("hostCakes")); cake.setCakeNum(rs.getInt("cakeNum")); cakes.add(cake); } } catch(Exception e) { e.printStackTrace(); } finally { try { rs.close(); ps.close(); daoUtil.close(); } catch(SQLException s) { s.printStackTrace(); } } return cakes; } public boolean addCake(Cake cake) { DbUtil dao = null; PreparedStatement ps = null; String sql = ""; try { dao = new DbUtil(); sql = "insert into tb_cake values(null,?,?,?,?,?,?,?,?,?,?,?,null,?,?,?,?,?)"; ps = dao.getCon().prepareStatement(sql); ps.setInt(1, cake.getSuperTypeId()); ps.setInt(2, cake.getSubTypeId()); ps.setString(3, cake.getCakeName()); ps.setString(4, cake.getSNumber()); ps.setString(5, cake.getIntroduce()); ps.setFloat(6, cake.getPrice()); ps.setFloat(7, cake.getNowPrice()); ps.setString(8, cake.getPicture()); ps.setInt(9, cake.getPages()); ps.setString(10, cake.getAdress()); ps.setString(11, cake.getCooker()); ps.setInt(12, cake.getNewCakes()); ps.setInt(13, cake.getSaleCakes()); ps.setInt(14, cake.getHostCakes()); ps.setInt(15, cake.getSpecialCakes()); ps.setInt(16, cake.getCakeNum()); int i = ps.executeUpdate(); if(i != 0) { return true; } } catch(Exception e) { e.printStackTrace(); } finally { try { ps.close(); dao.close(); } catch(SQLException s) { s.printStackTrace(); } } return false; } public boolean checkCakeNameIsExist(String cakeName) { DbUtil daoUtil = null; PreparedStatement ps = null; ResultSet rs = null; String sql = null; try { daoUtil = new DbUtil(); sql = "select * from tb_cake where cakeName = ?"; ps = daoUtil.getCon().prepareStatement(sql); ps.setString(1, cakeName); rs = ps.executeQuery(); if (rs.next()) { return true; } } catch(Exception e) { e.printStackTrace(); } finally { try { rs.close(); ps.close(); daoUtil.close(); } catch(SQLException s) { s.printStackTrace(); } } return false; } public boolean checkSNumberIsExist(String sNumber) { DbUtil daoUtil = null; PreparedStatement ps = null; ResultSet rs = null; String sql = null; try { daoUtil = new DbUtil(); sql = "select * from tb_cake where sNumber = ?"; ps = daoUtil.getCon().prepareStatement(sql); ps.setString(1, sNumber); rs = ps.executeQuery(); if (rs.next()) { return true; } } catch(Exception e) { e.printStackTrace(); } finally { try { rs.close(); ps.close(); daoUtil.close(); } catch(SQLException s) { s.printStackTrace(); } } return false; } public CakePager searchCake(String cakeName) { Map cakeMap = new HashMap(); DbUtil daoUtil = null; PreparedStatement ps = null; ResultSet rs = null; String sql = null; try { daoUtil = new DbUtil(); sql = "select * from tb_cake where cakeName like ?"; ps = daoUtil.getCon().prepareStatement(sql); ps.setString(1, cakeName+"%"); rs = ps.executeQuery(); while (rs.next()) { Cake cake = new Cake(); cake.setCakeId(rs.getInt("cakeId")); cake.setSuperTypeId(rs.getInt("superTypeId")); cake.setSubTypeId(rs.getInt("subTypeId")); cake.setCakeName(rs.getString("cakeName")); cake.setIntroduce(rs.getString("introduce")); cake.setPrice(rs.getFloat("price")); cake.setNowPrice(rs.getFloat("nowPrice")); cake.setPicture(rs.getString("picture")); cake.setInTime(rs.getString("inTime")); cake.setNewCakes(rs.getInt("newCakes")); cake.setSaleCakes(rs.getInt("saleCakes")); cake.setHostCakes(rs.getInt("hostCakes")); cake.setCakeNum(rs.getInt("cakeNum")); cakeMap.put(cake.getCakeId(),cake); } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); ps.close(); daoUtil.close(); } catch (Exception e) { e.printStackTrace(); } } CakePager bp = new CakePager(); bp.setCakeMap(cakeMap); bp.setTotalNum(cakeMap.size()); return bp; } public List getAllCakeName() { List cakeNameList = new ArrayList(); DbUtil daoUtil = null; PreparedStatement ps = null; ResultSet rs = null; String sql = null; try { daoUtil = new DbUtil(); sql = "select cakeName from tb_cake"; ps = daoUtil.getCon().prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { cakeNameList.add(rs.getString("cakeName")); } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); ps.close(); daoUtil.close(); } catch (Exception e) { e.printStackTrace(); } } return cakeNameList; } public CakePager getCakePager(int index,int pageSize) { Map cakeMap = new HashMap(); DbUtil db = null; PreparedStatement ps = null; ResultSet rs = null; try { db = new DbUtil(); String sql = "select * from tb_cake limit ?,?"; ps = db.getCon().prepareStatement(sql); ps.setInt(1, index); ps.setInt(2, pageSize); rs = ps.executeQuery(); while(rs.next()) { Cake cake = new Cake(); cake.setCakeId(rs.getInt("cakeId")); cake.setSuperTypeId(rs.getInt("superTypeId")); cake.setSubTypeId(rs.getInt("subTypeId")); cake.setCakeName(rs.getString("cakeName")); cake.setIntroduce(rs.getString("introduce")); cake.setPrice(rs.getFloat("price")); cake.setNowPrice(rs.getFloat("nowPrice")); cake.setPicture(rs.getString("picture")); cake.setInTime(rs.getString("inTime")); cake.setNewCakes(rs.getInt("newCakes")); cake.setSaleCakes(rs.getInt("saleCakes")); cake.setHostCakes(rs.getInt("hostCakes")); cake.setCakeNum(rs.getInt("cakeNum")); cakeMap.put(cake.getCakeId(), cake); } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); ps.close(); db.close(); } catch (Exception e) { e.printStackTrace(); } } CakePager bp = new CakePager(); bp.setCakeMap(cakeMap); bp.setPageSize(pageSize); bp.setTotalNum(getAllCakes().size()); return bp; } public boolean deleteCake(int[] cakeIds) { DbUtil daoUtil = null; PreparedStatement ps = null; Connection conn = null; String sql = "delete from tb_cake where cakeId=?"; try { daoUtil = new DbUtil(); conn = daoUtil.getCon(); conn.setAutoCommit(false); ps = conn.prepareStatement(sql); for(int j=0;j<cakeIds.length;j++) { ps.setInt(1, cakeIds[j]); ps.addBatch(); } int[] k = ps.executeBatch(); conn.commit(); if(k.length == cakeIds.length) { return true; } } catch (Exception e) { try { conn.rollback(); } catch (Exception ex) { ex.printStackTrace(); } } finally { try { ps.close(); daoUtil.close(); } catch (SQLException e) { e.printStackTrace(); } } return false; } }
最近下载更多
hmdzmsl12 LV2
2023年10月28日
微信网友_6184005683466240 LV11
2023年6月12日
微信网友_6508798513811456 LV5
2023年6月8日
北方菜 LV11
2022年12月30日
1532593037 LV8
2022年11月23日
zhaoyangwfd LV17
2022年6月22日
kirito1994 LV5
2022年4月19日
330786215 LV14
2022年4月18日
xiaodao12 LV2
2022年2月28日
MarkLee 琥珀川 LV13
2022年1月25日
最近浏览更多
哭了跨服了莫 LV1
10月16日
柳咪华沙 LV7
6月29日
rain112 LV31
6月18日
郭宇航裹裹 LV5
6月7日
ming_123_9715 LV23
5月16日
bbczlitao LV9
3月5日
liiiyou LV1
1月26日
a2796831202
2023年12月20日
暂无贡献等级
zzzhhhhhh7758521 LV1
2023年11月16日
hmdzmsl12 LV2
2023年10月28日