package org.dao.impl; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.LinkedList; import java.util.List; import org.dao.DaoException; import org.dao.ProductDao; import org.pojo.Product; import org.utils.DBUtil; /** * 产品Dao实现 */ public class ProductDaoImpl implements ProductDao { /** * 获取所有产品 */ @Override public List<Product> getProducts() throws DaoException { ResultSet rs = null; try { List<Product> products = new LinkedList<Product>(); rs = DBUtil.getResultSet("SELECT id, name, price, count FROM product"); while(rs.next()) { products.add(Product.fromResultSet(rs)); } return products; } catch (Exception e) { return null; } finally { DBUtil.clearResultSet(rs); DBUtil.closeConnection(); } } /** * 保存产品 * @param p 产品对象 * @return true 保存成功 false 保存失败 */ @Override public boolean save(Product p) throws DaoException { PreparedStatement st = null; try { st = DBUtil.getPrepareStatement( "INSERT INTO product(" + "id, " + "name, " + "price, " + "count) " + "VALUES(" + "null, " + "?, " + "?, " + "?)"); st.setString(1, p.getName()); st.setFloat(2, p.getPrice()); st.setInt(3, p.getCount()); st.executeUpdate(); return true; } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.clearPreparedStatement(st); DBUtil.closeConnection(); } return false; } /** * 更新产品 * @param p 产品对象 * @return true 更新成功 false 更新失败 */ @Override public boolean update(Product p) throws DaoException { PreparedStatement st = null; try { st = DBUtil.getPrepareStatement( "UPDATE product SET " + "name = ?, " + "price = ?, " + "count = ? " + "WHERE id = ? "); st.setString(1, p.getName()); st.setFloat(2, p.getPrice()); st.setInt(3, p.getCount()); st.setInt(4, p.getId()); st.executeUpdate(); return true; } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.clearPreparedStatement(st); DBUtil.closeConnection(); } return false; } /** * 删除产品 * @param id 产品编号 * @return true 删除成功 false 删除失败 */ @Override public boolean delete(int id) throws DaoException { PreparedStatement st = null; try { st = DBUtil.getPrepareStatement("DELETE FROM product WHERE id = ?"); st.setInt(1, id); st.executeUpdate(); return true; } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.clearPreparedStatement(st); DBUtil.closeConnection(); } return false; } /** * 获取产品对象 * @param id 产品编号 */ @Override public Product getProdcutById(int id) throws DaoException { Product product = null; PreparedStatement st = null; ResultSet rs = null; try { st = DBUtil.getPrepareStatement( "SELECT id, name, price, count FROM product WHERE id = ?"); st.setInt(1, id); rs = st.executeQuery(); if(rs.next()){ product = Product.fromResultSet(rs); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.clearPreparedStatement(st); DBUtil.clearResultSet(rs); DBUtil.closeConnection(); } return product; } }

gshnbb LV9
2023年4月9日
ssd1224 LV1
2021年5月19日
1669613755 LV2
2020年9月12日
chengxvyang LV7
2020年9月4日
qq2528732622 LV8
2020年6月15日
410286328 LV2
2020年6月11日
y6622576 LV9
2020年6月7日
bcr1234 LV9
2020年5月29日
dadakjdsd LV1
2020年5月10日
LIcquent LV1
2020年4月25日