复制
收藏
提问
全网

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(); } } } }这一段代码的功能与意义分析

b09e7a106
4个月前
全网
文档
学术
百科
知识库
时间不限
自动

理解问题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(); } } } }这一段代码的功能与意义分析
不在提醒

更专业内容正在努力生成中
知识树
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(); } } } }这一段代码的功能与意义分析
Java JDBC连接数据库的方法
如何使用JDBC进行数据库查询
Product类在Java中的作用

以上内容由AI搜集生成,仅供参考

在线客服