|
|
@@ -0,0 +1,195 @@
|
|
|
+package com.sf.dao.impl;
|
|
|
+
|
|
|
+import com.sf.dao.ProductDAO;
|
|
|
+import com.sf.domain.Product;
|
|
|
+import com.sf.util.JDBCUtil;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.sql.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class ProductDAOImpl implements ProductDAO {
|
|
|
+ @Override
|
|
|
+ public void insert(Product product) {
|
|
|
+ //1 加载驱动
|
|
|
+ Connection connection = null;
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ try {
|
|
|
+ connection = JDBCUtil.getConn();
|
|
|
+ //3 创建预编译语句对象
|
|
|
+ preparedStatement = connection.prepareStatement("insert into product " +
|
|
|
+ "(productName,dir_id,salePrice,supplier,brand,cutoff,costPrice) values(?,?,?,?,?,?,?)");
|
|
|
+ preparedStatement.setString(1,product.getProductName());
|
|
|
+ preparedStatement.setLong(2,product.getDir_id());
|
|
|
+ preparedStatement.setBigDecimal(3,product.getSalePrice());
|
|
|
+ preparedStatement.setString(4,product.getSupplier());
|
|
|
+ preparedStatement.setString(5,product.getBrand());
|
|
|
+ preparedStatement.setDouble(6,product.getCutoff());
|
|
|
+ preparedStatement.setBigDecimal(7,product.getCostPrice());
|
|
|
+ //4 执行sql
|
|
|
+ preparedStatement.executeUpdate();
|
|
|
+ //5 释放资源
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }finally {
|
|
|
+ JDBCUtil.close(connection,preparedStatement,null);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void update(Product product) {
|
|
|
+ //1 加载驱动
|
|
|
+ Connection connection = null;
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ try {
|
|
|
+ connection = JDBCUtil.getConn();
|
|
|
+ //3 创建预编译语句对象
|
|
|
+ preparedStatement = connection.prepareStatement("update product " +
|
|
|
+ "set productName=?,dir_id=?,salePrice=?,supplier=?,brand=?,cutoff=?,costPrice=? where id=?");
|
|
|
+ preparedStatement.setString(1,product.getProductName());
|
|
|
+ preparedStatement.setLong(2,product.getDir_id());
|
|
|
+ preparedStatement.setBigDecimal(3,product.getSalePrice());
|
|
|
+ preparedStatement.setString(4,product.getSupplier());
|
|
|
+ preparedStatement.setString(5,product.getBrand());
|
|
|
+ preparedStatement.setDouble(6,product.getCutoff());
|
|
|
+ preparedStatement.setBigDecimal(7,product.getCostPrice());
|
|
|
+ preparedStatement.setLong(8,product.getId());
|
|
|
+ //4 执行sql
|
|
|
+ preparedStatement.executeUpdate();
|
|
|
+ //5 释放资源
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }finally {
|
|
|
+ JDBCUtil.close(connection,preparedStatement,null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void delete(Long id) {
|
|
|
+ //1 加载驱动
|
|
|
+ Connection connection = null;
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ try {
|
|
|
+ connection = JDBCUtil.getConn();
|
|
|
+ //3 创建预编译语句对象
|
|
|
+ preparedStatement = connection.prepareStatement("delete from product where id = ?");
|
|
|
+ preparedStatement.setLong(1,id);
|
|
|
+ //4 执行sql
|
|
|
+ preparedStatement.executeUpdate();
|
|
|
+ //5 释放资源
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }finally {
|
|
|
+ JDBCUtil.close(connection,preparedStatement,null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Product get(Long id) {
|
|
|
+ //1 加载驱动
|
|
|
+ Connection connection = null;
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ ResultSet resultSet = null;
|
|
|
+ try {
|
|
|
+ connection = JDBCUtil.getConn();
|
|
|
+ //3 创建预编译语句对象
|
|
|
+ preparedStatement = connection.prepareStatement("select * from product where id = ?");
|
|
|
+ preparedStatement.setLong(1,id);
|
|
|
+ //4 执行sql
|
|
|
+ resultSet = preparedStatement.executeQuery();
|
|
|
+ // 判断查询是否有结果 next()
|
|
|
+ if(resultSet.next()){
|
|
|
+ Product product = new Product();
|
|
|
+ product.setId(resultSet.getLong("id"));
|
|
|
+ product.setProductName(resultSet.getString("productName"));
|
|
|
+ product.setDir_id(resultSet.getLong("dir_id"));
|
|
|
+ product.setSalePrice(resultSet.getBigDecimal("salePrice"));
|
|
|
+ product.setSupplier(resultSet.getString("supplier"));
|
|
|
+ product.setBrand(resultSet.getString("brand"));
|
|
|
+ product.setCutoff(resultSet.getDouble("cutoff"));
|
|
|
+ product.setCostPrice(resultSet.getBigDecimal("costPrice"));
|
|
|
+ return product;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ //5 释放资源
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }finally {
|
|
|
+ JDBCUtil.close(connection,preparedStatement,resultSet);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Product> list() {
|
|
|
+ //1 加载驱动
|
|
|
+ Connection connection = null;
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ ResultSet resultSet = null;
|
|
|
+ try {
|
|
|
+ connection = JDBCUtil.getConn();
|
|
|
+ //3 创建预编译语句对象
|
|
|
+ preparedStatement = connection.prepareStatement("select * from product ");
|
|
|
+ //4 执行sql
|
|
|
+ resultSet = preparedStatement.executeQuery();
|
|
|
+ List<Product> list = new ArrayList<>();
|
|
|
+ // 判断查询是否有结果 next()
|
|
|
+ while (resultSet.next()){
|
|
|
+ Product product = new Product();
|
|
|
+ product.setId(resultSet.getLong("id"));
|
|
|
+ product.setProductName(resultSet.getString("productName"));
|
|
|
+ product.setDir_id(resultSet.getLong("dir_id"));
|
|
|
+ product.setSalePrice(resultSet.getBigDecimal("salePrice"));
|
|
|
+ product.setSupplier(resultSet.getString("supplier"));
|
|
|
+ product.setBrand(resultSet.getString("brand"));
|
|
|
+ product.setCutoff(resultSet.getDouble("cutoff"));
|
|
|
+ product.setCostPrice(resultSet.getBigDecimal("costPrice"));
|
|
|
+ list.add(product);
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ //5 释放资源
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }finally {
|
|
|
+ JDBCUtil.close(connection,preparedStatement,resultSet);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Product> queryProductsByPrice(BigDecimal price) {
|
|
|
+ //1 加载驱动
|
|
|
+ Connection connection = null;
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ ResultSet resultSet = null;
|
|
|
+ try {
|
|
|
+ connection = JDBCUtil.getConn();
|
|
|
+ //3 创建预编译语句对象
|
|
|
+ preparedStatement = connection.
|
|
|
+ prepareStatement("select * from product where salePrice>? order by salePrice desc");
|
|
|
+ preparedStatement.setBigDecimal(1,price);
|
|
|
+ //4 执行sql
|
|
|
+ resultSet = preparedStatement.executeQuery();
|
|
|
+ List<Product> list = new ArrayList<>();
|
|
|
+ // 判断查询是否有结果 next()
|
|
|
+ while (resultSet.next()){
|
|
|
+ Product product = new Product();
|
|
|
+ product.setId(resultSet.getLong("id"));
|
|
|
+ product.setProductName(resultSet.getString("productName"));
|
|
|
+ product.setDir_id(resultSet.getLong("dir_id"));
|
|
|
+ product.setSalePrice(resultSet.getBigDecimal("salePrice"));
|
|
|
+ product.setSupplier(resultSet.getString("supplier"));
|
|
|
+ product.setBrand(resultSet.getString("brand"));
|
|
|
+ product.setCutoff(resultSet.getDouble("cutoff"));
|
|
|
+ product.setCostPrice(resultSet.getBigDecimal("costPrice"));
|
|
|
+ list.add(product);
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ //5 释放资源
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }finally {
|
|
|
+ JDBCUtil.close(connection,preparedStatement,resultSet);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|