package com.tyj.dao.mysql; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.tyj.dao.ProductDao; import com.tyj.domain.Product; public class productDaolmptyj implements ProductDao { @Override public List findAll() { String sql= "select productid,category,cname,ename,images,listprice,unitcost,descn from product"; List list = new ArrayList(); try (Connection conn = DBHelper.getConnection(); //创建语句对象 PreparedStatement pstmt = conn.prepareStatement(sql); //绑定参数 //执行查询 ResultSet rs = pstmt.executeQuery()){ //遍历结果集 while (rs.next()) { Product P = new Product(); P.setProductid(rs.getString("productid")); P.setCategory(rs.getString("category")); P.setCname(rs.getString("cname")); P.setEname(rs.getString("ename")); P.setImage(rs.getString("images")); P.setListprice(rs.getString("listprice")); P.setUnitcost(rs.getString("unitcost")); P.setDescn(rs.getString("descn")); list.add(P); } }catch (SQLException e) { e.printStackTrace(); } // TODO Auto-generated method stub return list; } @Override public Product findById(String id) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; String sql= "select productid,category,cname,ename,images,listprice,unitcost,descn from product where productid = ?"; try { // 2.创建数据库链接 conn = DBHelper.getConnection();//加载驱动程序 // 创建语句对象 pstmt = conn.prepareStatement(sql); // 绑定参数 pstmt.setString(1, id); // 执行查询 rs = pstmt.executeQuery(); // 遍历结果 while (rs.next()) { Product p = new Product(); p.setProductid(rs.getString("productid")); p.setCategory(rs.getString("category")); p.setCname(rs.getString("cname")); p.setEname(rs.getString("ename")); p.setImage(rs.getString("images")); p.setListprice(rs.getString("listprice")); p.setUnitcost(rs.getString("unitcost")); p.setDescn(rs.getString("descn")); return p; } } catch (SQLException e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } return null; } public List findByCategory(String category) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; List list = new ArrayList(); String sql= "select productid,category,cname,ename,images,listprice,unitcost,descn from product where category = ?"; try { // 2.创建数据库链接 conn = DBHelper.getConnection();//加载驱动程序 // 创建语句对象 pstmt = conn.prepareStatement(sql); // 绑定参数 pstmt.setString(1, category); // 执行查询 rs = pstmt.executeQuery(); // 遍历结果 while (rs.next()) { Product P = new Product(); P.setProductid(rs.getString("productid")); P.setCategory(rs.getString("category")); P.setCname(rs.getString("cname")); P.setEname(rs.getString("ename")); P.setImage(rs.getString("images")); P.setListprice(rs.getString("listprice")); P.setUnitcost(rs.getString("unitcost")); P.setDescn(rs.getString("descn")); } } catch (SQLException e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } return list; } @Override public int create(Product product) { // TODO Auto-generated method stub return 0; } @Override public int modify(Product product) { // TODO Auto-generated method stub return 0; } @Override public int remove(Product product) { // TODO Auto-generated method stub return 0; } @Override public List findCategory(String category) { // TODO 自动生成的方法存根 return null; } }