10.24
跟着模板敲代码(1)
项目的架构
Dao为数据持久层,用于实现数据库的增删改查
entity为javabean用于封装数据库中的对象
servlet为前端数据的处理层
jsp为前端页面
现在来一个个实现
BaseDao用于链接mysql数据库
public class BaseDao { static { try{ Class.forName("com.mysql.jdbc.Driver"); }catch (ClassNotFoundException e){ e.printStackTrace(); } } public static Connection getConnection(){ Connection conn=null; try{ String url="jdbc:mysql://localhost:3306/fruit"; String username="root"; String password="1234"; conn= DriverManager.getConnection(url,username,password); }catch (SQLException e) { e.printStackTrace(); } return conn; } public static void closeConnection(Connection conn, PreparedStatement pstmt, ResultSet rs){ try { if(rs!=null){ rs.close(); } pstmt.close(); conn.close(); }catch (SQLException e){ e.printStackTrace(); } } }
Fruit为实体类
public class Fruit { public Integer id; public String name; public Integer price; public Integer weight; public String location; public Fruit() { } public Fruit(Integer id, String name, Integer price, Integer weight, String location) { this.id = id; this.name = name; this.price = price; this.weight = weight; this.location = location; } public Fruit(String name, Integer price, Integer weight, String location) { this.name = name; this.price = price; this.weight = weight; this.location = location; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } public Integer getWeight() { return weight; } public void setWeight(Integer weight) { this.weight = weight; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } }
接着实现增删改查
public class FruitDao { /** * 查看全部 * @return */ public List<Fruit> selectAll(){ List<Fruit> list=new ArrayList<>(); Connection conn = BaseDao.getConnection(); PreparedStatement pstmt=null; ResultSet rs=null; try { pstmt=conn.prepareStatement("select * from fruit"); rs=pstmt.executeQuery(); while (rs.next()){ Fruit f=new Fruit(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getInt(4),rs.getString(5)); list.add(f); } }catch (Exception e){ e.printStackTrace(); }finally { BaseDao.closeConnection(conn,pstmt,rs); } return list; } /** * 增加 * @param f * @return */ public int insert(Fruit f ){ int count=0; Connection conn = BaseDao.getConnection(); PreparedStatement pstmt=null; ResultSet rs=null; try { pstmt=conn.prepareStatement("insert into fruit(name,price,weight,location) values (?,?,?,?)"); // rs=pstmt.executeQuery(); pstmt.setString(1,f.getName()); pstmt.setInt(2,f.getPrice()); pstmt.setInt(3,f.getWeight()); pstmt.setString(4,f.getLocation()); count=pstmt.executeUpdate(); }catch (Exception e){ e.printStackTrace(); }finally { BaseDao.closeConnection(conn,pstmt,rs); } return count; } /** * 根据id进行查询 * @param id * @return */ public Fruit selectById(int id){ Fruit f=new Fruit(); Connection conn = BaseDao.getConnection(); PreparedStatement pstmt=null; ResultSet rs=null; try { pstmt=conn.prepareStatement("select * from fruit where id=?"); pstmt.setInt(1,id); rs=pstmt.executeQuery(); while (rs.next()){ f=new Fruit(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getInt(4),rs.getString(5)); // pstmt.setInt(1,id); } }catch (Exception e){ e.printStackTrace(); }finally { BaseDao.closeConnection(conn,pstmt,rs); } return f; } public int update(Fruit f ){ int count=0; Connection conn = BaseDao.getConnection(); PreparedStatement pstmt=null; ResultSet rs=null; try { pstmt=conn.prepareStatement("update fruit set name=? , price=? , weight=? , location=? where id=?"); pstmt.setString(1,f.getName()); pstmt.setInt(2,f.getPrice()); pstmt.setInt(3,f.getWeight()); pstmt.setInt(5,f.getId()); pstmt.setString(4,f.getLocation()); count=pstmt.executeUpdate(); }catch (Exception e){ e.printStackTrace(); }finally { BaseDao.closeConnection(conn,pstmt,rs); } return count; } /** * 根据id进行删除 * @param id * @return */ public int delete(int id){ int count=0; Connection conn = BaseDao.getConnection(); PreparedStatement pstmt=null; ResultSet rs=null; try { pstmt=conn.prepareStatement("delete from fruit where id=?"); pstmt.setInt(1,id); count=pstmt.executeUpdate(); }catch (Exception e){ e.printStackTrace(); }finally { BaseDao.closeConnection(conn,pstmt,rs); } return count; } }
剩下的明天继续