📄 productdao.java
字号:
package cart.dao;import cart.model.Product;import java.util.Collection;import java.sql.*;import cart.util.ConnUtil;import java.util.HashSet;import java.sql.PreparedStatement;public class ProductDao implements DAO{ public Collection selectAll(Connection con) throws SQLException{ Collection collection=new HashSet(); String sql="select * from products4"; Statement stm=null; ResultSet rs=null; int i=0; String id=null; try{ stm=con.createStatement(); rs=stm.executeQuery(sql); while(rs.next()) { Product p=new Product(); p.setId(rs.getInt("id")); p.setName(rs.getString("name")); p.setDescription(rs.getString("description")); p.setPrice(rs.getDouble("price")); collection.add(p); } }catch(SQLException e){ throw e; }finally{ ConnUtil.close(rs,stm); } return collection; } public Object selectById(Connection con,int id) throws SQLException{ Product p=null; String sql="select * from products4 where id=?"; PreparedStatement pstm=null; ResultSet rs=null; int i=1; try{ pstm=con.prepareStatement(sql); pstm.setInt(i++,id); rs=pstm.executeQuery(); if(rs.next()) { p=new Product(); p.setId(rs.getInt("id")); p.setName(rs.getString("name")); p.setDescription(rs.getString("description")); p.setPrice(rs.getDouble("price")); } }catch(SQLException e){ throw e; }finally{ ConnUtil.close(rs,pstm); } return p; } public void update(Connection con,Object o)throws SQLException{ Product product=(Product)o; } public void insert(Connection con,Object o)throws SQLException{ Product product=(Product)o; } public void delete(Connection con,int id)throws SQLException{ } public void delete(Connection con,int[] id)throws SQLException{ } public static void main(String[] args){ ProductDao p=new ProductDao(); Connection c=ConnUtil.getConnection(); Product pd=null; try{ pd=(Product)p.selectById(c,1); System.out.println(pd.getName()); }catch(Exception e){ } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -