package com.example.dao; import com.example.entity.Product; import com.example.utils.JdbcUtil; import java.sql.Connection; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; /** * 商品与数据库交互类 */ public class ProductDao { /** * 根据id查询商品信息 * @param id * @return */ public List<Product> selectProduct(String id) { Connection connection = null; ResultSet resultSet = null; try { connection = JdbcUtil.getConnection(); // 查询商品 String sql = ""; if(id == null || id.trim().equals("")) { sql = "select * from product"; resultSet = JdbcUtil.executeQuery(connection, sql); } else { sql = "select * from product where id = ?"; resultSet = JdbcUtil.executeQuery(connection, sql, id); } List<Product> productList = new ArrayList<>(); while (resultSet.next()) { // 查询结果不为空就返回商品信息 int proId = resultSet.getInt("id"); String name = resultSet.getString("name"); int num = resultSet.getInt("num"); String style = resultSet.getString("style"); String provider = resultSet.getString("provider"); Product product = new Product(proId, name, num, style, provider); productList.add(product); } return productList; }catch (Exception e){ e.printStackTrace(); return new ArrayList<>(); }finally { try { JdbcUtil.close(resultSet, connection); }catch (Exception e){ e.printStackTrace(); } } } /** * 添加商品信息 * @param product * @return */ public int insertProduct(Product product) throws Exception { Connection connection = null; int result = 0; try { connection = JdbcUtil.getConnection(); // 新增商品信息 String sql = "insert into product(name, num, style, provider) values(?, ?, ?, ?)"; result = JdbcUtil.update(connection, sql, product.getName(), product.getNum(), product.getStyle(), product.getProvider()); return result; }catch (Exception e){ e.printStackTrace(); throw new Exception("添加失败!"); }finally { try { JdbcUtil.close(null, connection); }catch (Exception e){ e.printStackTrace(); } } } /** * 删除商品信息 * @param id * @return */ public int deleteProduct(String id) throws Exception { Connection connection = null; int result = 0; try { connection = JdbcUtil.getConnection(); // 删除商品信息 String sql = "delete from product where id = ?"; result = JdbcUtil.update(connection, sql, id); return result; }catch (Exception e){ e.printStackTrace(); throw new Exception("删除失败!"); }finally { try { JdbcUtil.close(null, connection); }catch (Exception e){ e.printStackTrace(); } } } /** * 修改商品信息 * @param product * @return */ public int updateProduct(Product product) throws Exception { Connection connection = null; int result = 0; try { connection = JdbcUtil.getConnection(); // 修改商品信息 String sql = "update product set name = ?, num = ?, style = ?, provider = ? where id = ?"; result = JdbcUtil.update(connection, sql, product.getName(), product.getNum(), product.getStyle(), product.getProvider(), product.getId()); return result; }catch (Exception e){ e.printStackTrace(); throw new Exception("修改失败!"); }finally { try { JdbcUtil.close(null, connection); }catch (Exception e){ e.printStackTrace(); } } } }这一段代码的功能与意义分析
理解问题package com.example.dao; import com.example.entity.Product; import com.example.utils.JdbcUtil; import java.sql.Connection; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; /** * 商品与数据库交互类 */ public class ProductDao { /** * 根据id查询商品信息 * @param id * @return */ public List<Product> selectProduct(String id) { Connection connection = null; ResultSet resultSet = null; try { connection = JdbcUtil.getConnection(); // 查询商品 String sql = ""; if(id == null || id.trim().equals("")) { sql = "select * from product"; resultSet = JdbcUtil.executeQuery(connection, sql); } else { sql = "select * from product where id = ?"; resultSet = JdbcUtil.executeQuery(connection, sql, id); } List<Product> productList = new ArrayList<>(); while (resultSet.next()) { // 查询结果不为空就返回商品信息 int proId = resultSet.getInt("id"); String name = resultSet.getString("name"); int num = resultSet.getInt("num"); String style = resultSet.getString("style"); String provider = resultSet.getString("provider"); Product product = new Product(proId, name, num, style, provider); productList.add(product); } return productList; }catch (Exception e){ e.printStackTrace(); return new ArrayList<>(); }finally { try { JdbcUtil.close(resultSet, connection); }catch (Exception e){ e.printStackTrace(); } } } /** * 添加商品信息 * @param product * @return */ public int insertProduct(Product product) throws Exception { Connection connection = null; int result = 0; try { connection = JdbcUtil.getConnection(); // 新增商品信息 String sql = "insert into product(name, num, style, provider) values(?, ?, ?, ?)"; result = JdbcUtil.update(connection, sql, product.getName(), product.getNum(), product.getStyle(), product.getProvider()); return result; }catch (Exception e){ e.printStackTrace(); throw new Exception("添加失败!"); }finally { try { JdbcUtil.close(null, connection); }catch (Exception e){ e.printStackTrace(); } } } /** * 删除商品信息 * @param id * @return */ public int deleteProduct(String id) throws Exception { Connection connection = null; int result = 0; try { connection = JdbcUtil.getConnection(); // 删除商品信息 String sql = "delete from product where id = ?"; result = JdbcUtil.update(connection, sql, id); return result; }catch (Exception e){ e.printStackTrace(); throw new Exception("删除失败!"); }finally { try { JdbcUtil.close(null, connection); }catch (Exception e){ e.printStackTrace(); } } } /** * 修改商品信息 * @param product * @return */ public int updateProduct(Product product) throws Exception { Connection connection = null; int result = 0; try { connection = JdbcUtil.getConnection(); // 修改商品信息 String sql = "update product set name = ?, num = ?, style = ?, provider = ? where id = ?"; result = JdbcUtil.update(connection, sql, product.getName(), product.getNum(), product.getStyle(), product.getProvider(), product.getId()); return result; }catch (Exception e){ e.printStackTrace(); throw new Exception("修改失败!"); }finally { try { JdbcUtil.close(null, connection); }catch (Exception e){ e.printStackTrace(); } } } }这一段代码的功能与意义分析
已完成理解「package com.example.dao; import com.example.entity.Product; import com.example.utils.JdbcUtil; import java.sql.Connection; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; /** * 商品与数据库交互类 */ public class ProductDao { /** * 根据id查询商品信息 * @param id * @return */ public List<Product> selectProduct(String id) { Connection connection = null; ResultSet resultSet = null; try { connection = JdbcUtil.getConnection(); // 查询商品 String sql = ""; if(id == null || id.trim().equals("")) { sql = "select * from product"; resultSet = JdbcUtil.executeQuery(connection, sql); } else { sql = "select * from product where id = ?"; resultSet = JdbcUtil.executeQuery(connection, sql, id); } List<Product> productList = new ArrayList<>(); while (resultSet.next()) { // 查询结果不为空就返回商品信息 int proId = resultSet.getInt("id"); String name = resultSet.getString("name"); int num = resultSet.getInt("num"); String style = resultSet.getString("style"); String provider = resultSet.getString("provider"); Product product = new Product(proId, name, num, style, provider); productList.add(product); } return productList; }catch (Exception e){ e.printStackTrace(); return new ArrayList<>(); }finally { try { JdbcUtil.close(resultSet, connection); }catch (Exception e){ e.printStackTrace(); } } } /** * 添加商品信息 * @param product * @return */ public int insertProduct(Product product) throws Exception { Connection connection = null; int result = 0; try { connection = JdbcUtil.getConnection(); // 新增商品信息 String sql = "insert into product(name, num, style, provider) values(?, ?, ?, ?)"; result = JdbcUtil.update(connection, sql, product.getName(), product.getNum(), product.getStyle(), product.getProvider()); return result; }catch (Exception e){ e.printStackTrace(); throw new Exception("添加失败!"); }finally { try { JdbcUtil.close(null, connection); }catch (Exception e){ e.printStackTrace(); } } } /** * 删除商品信息 * @param id * @return */ public int deleteProduct(String id) throws Exception { Connection connection = null; int result = 0; try { connection = JdbcUtil.getConnection(); // 删除商品信息 String sql = "delete from product where id = ?"; result = JdbcUtil.update(connection, sql, id); return result; }catch (Exception e){ e.printStackTrace(); throw new Exception("删除失败!"); }finally { try { JdbcUtil.close(null, connection); }catch (Exception e){ e.printStackTrace(); } } } /** * 修改商品信息 * @param product * @return */ public int updateProduct(Product product) throws Exception { Connection connection = null; int result = 0; try { connection = JdbcUtil.getConnection(); // 修改商品信息 String sql = "update product set name = ?, num = ?, style = ?, provider = ? where id = ?"; result = JdbcUtil.update(connection, sql, product.getName(), product.getNum(), product.getStyle(), product.getProvider(), product.getId()); return result; }catch (Exception e){ e.printStackTrace(); throw new Exception("修改失败!"); }finally { try { JdbcUtil.close(null, connection); }catch (Exception e){ e.printStackTrace(); } } } }这一段代码的功能与意义分析」
展开阅读网页