寒假第九天
package com.zzw.dao; import java.util.List; import com.zzw.entity.Bill; import com.zzw.entity.User; public interface IUserDao { //注册 public boolean Register(User user) ; //查询账户是否存在 public boolean isExist(String uname) ; //登录 public boolean Login(String uname,String upwd) ; //根据帐号查询用户全部信息 public User Query(String uname) ; //记账 public boolean AddBill(Bill bill); //根据账目编号删除账目信息 public boolean DeleteBill(int bid); //根据账目编号修改账目信息 public boolean UpdateBill(int bid,Bill bill) ; //查询账目是否存在 public boolean isExist(int bid) ; //根据账目编号查询账目信息 public Bill Query(int bid); //根据消费类型查询账单信息 public List<Bill> QueryType(String btype); //根据日期查询账单信息 public List<Bill> QueryDate(String bdate) ; //根据消费类型和日期查询账单信息 public List<Bill> QueryPart(String btype,String bdate) ; //查询全部账单信息 public List<Bill> QueryAll() ; } package com.zzw.dao.Impl; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.ArrayList; import com.zzw.dao.IUserDao; import com.zzw.entity.Bill; import com.zzw.entity.User; import com.zzw.utils.DBUtil; public class UserDaoImpl implements IUserDao{ //注册 public boolean Register(User user) { String sql="insert into user(uname,upwd,usex) values(?,?,?)" ; Object [] params= {user.getUname(),user.getUpwd(),user.getUsex()}; return DBUtil.executeUpdate(sql, params); } //查询账户是否存在 public boolean isExist(String uname) { return Query(uname)==null? false:true; } //登录 public boolean Login(String uname,String upwd) { return Query(uname,upwd)==null? false:true; } //根据账号查询用户全部信息 public User Query(String uname) { User user= null; ResultSet rs = null; try { String sql="select * from user where uname =?" ; Object [] params= {uname}; rs=DBUtil.executeQuery(sql, params); if(rs.next()) { String name=rs.getString("uname"); String pwd=rs.getString("upwd"); String sex=rs.getString("usex"); user= new User(name,pwd,sex); } }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(rs!=null)rs.close(); if(DBUtil.pstmt!=null)DBUtil.pstmt.close(); if(DBUtil.connection !=null)DBUtil.connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return user; } //根据账户密码确定是否存在 public User Query(String uname,String upwd) { User user= null; ResultSet rs = null; try { String sql="select * from user where uname =? and upwd=?" ; Object [] params= {uname,upwd}; rs=DBUtil.executeQuery(sql, params); if(rs.next()) { String name=rs.getString("uname"); String pwd=rs.getString("upwd"); String sex=rs.getString("usex"); user= new User(name,pwd,sex); } }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(rs!=null)rs.close(); if(DBUtil.pstmt!=null)DBUtil.pstmt.close(); if(DBUtil.connection !=null)DBUtil.connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return user; } //记账 public boolean AddBill(Bill bill) { String sql="insert into bill(btype,bmoney,bdate,bremark) values(?,?,?,?)" ; Object [] params= {bill.getBtype(),bill.getBmoney(),bill.getBdate(),bill.getBremark()}; return DBUtil.executeUpdate(sql, params); } //根据账目编号删除账目信息 public boolean DeleteBill(int bid) { String sql="delete from bill where bid=?" ; Object [] params= {bid}; return DBUtil.executeUpdate(sql, params); } //根据账目编号修改账目信息 public boolean UpdateBill(int bid,Bill bill) { String sql="update bill set btype =?,bmoney=?,bdate=?,bremark=? where bid =?" ; Object [] params= {bill.getBtype(),bill.getBmoney(),bill.getBdate(),bill.getBremark(),bid}; return DBUtil.executeUpdate(sql, params); } //查询账目是否存在 public boolean isExist(int bid) { return Query(bid)==null? false:true; } //根据账目编号查询账目信息 public Bill Query(int bid) { Bill bill= null; ResultSet rs = null; try { String sql="select * from bill where bid =? " ; Object [] params= {bid}; rs=DBUtil.executeQuery(sql, params); if(rs.next()) { int id=rs.getInt("bid"); String type=rs.getString("btype"); int money=rs.getInt("bmoney"); String date=rs.getString("bdate"); String remark=rs.getString("bremark"); bill=new Bill(id,type,money,date,remark); } }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(rs!=null)rs.close(); if(DBUtil.pstmt!=null)DBUtil.pstmt.close(); if(DBUtil.connection !=null)DBUtil.connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return bill; } //根据消费类型查询账单信息 public List<Bill> QueryType(String btype) { List<Bill> bills = new ArrayList<>(); Bill bill= null; ResultSet rs=null; try { String sql="select * from bill where btype=?" ; Object [] params= {btype}; rs=DBUtil.executeQuery(sql, params); while(rs.next()) { int id=rs.getInt("bid"); String type= rs.getString("btype"); int money=rs.getInt("bmoney"); String date= rs.getString("bdate"); String remark= rs.getString("bremark"); bill= new Bill(id,type,money,date,remark); bills.add(bill); } }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(rs!=null)rs.close(); if(DBUtil.pstmt!=null)DBUtil.pstmt.close(); if(DBUtil.connection !=null)DBUtil.connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return bills; } //根据日期查询账单信息 public List<Bill> QueryDate(String bdate) { List<Bill> bills = new ArrayList<>(); Bill bill= null; ResultSet rs=null; try { String sql="select * from bill where bdate=?" ; Object [] params= {bdate}; rs=DBUtil.executeQuery(sql, params); while(rs.next()) { int id=rs.getInt("bid"); String type= rs.getString("btype"); int money=rs.getInt("bmoney"); String date= rs.getString("bdate"); String remark= rs.getString("bremark"); bill= new Bill(id,type,money,date,remark); bills.add(bill); } }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(rs!=null)rs.close(); if(DBUtil.pstmt!=null)DBUtil.pstmt.close(); if(DBUtil.connection !=null)DBUtil.connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return bills; } //根据消费类型和日期查询账单信息 public List<Bill> QueryPart(String btype,String bdate) { List<Bill> bills = new ArrayList<>(); Bill bill= null; ResultSet rs=null; try { String sql="select * from bill where btype=?and bdate=?" ; Object [] params= {btype,bdate}; rs=DBUtil.executeQuery(sql, params); while(rs.next()) { int id=rs.getInt("bid"); String type= rs.getString("btype"); int money=rs.getInt("bmoney"); String date= rs.getString("bdate"); String remark= rs.getString("bremark"); bill= new Bill(id,type,money,date,remark); bills.add(bill); } }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(rs!=null)rs.close(); if(DBUtil.pstmt!=null)DBUtil.pstmt.close(); if(DBUtil.connection !=null)DBUtil.connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return bills; } //查询全部账单信息 public List<Bill> QueryAll() { List<Bill> bills = new ArrayList<>(); Bill bill= null; ResultSet rs=null; try { String sql="select * from bill " ; rs=DBUtil.executeQuery(sql, null); while(rs.next()) { int id=rs.getInt("bid"); String type= rs.getString("btype"); int money=rs.getInt("bmoney"); String date= rs.getString("bdate"); String remark= rs.getString("bremark"); bill= new Bill(id,type,money,date,remark); bills.add(bill); } }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(rs!=null)rs.close(); if(DBUtil.pstmt!=null)DBUtil.pstmt.close(); if(DBUtil.connection !=null)DBUtil.connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return bills; } } package com.zzw.entity; public class Bill { private int bid; private String btype; private int bmoney; private String bdate; private String bremark; public Bill() { } public Bill(int bmoney, String bdate, String bremark) { this.bmoney = bmoney; this.bdate = bdate; this.bremark = bremark; } public Bill(String btype, int bmoney, String bdate, String bremark) { this.btype = btype; this.bmoney = bmoney; this.bdate = bdate; this.bremark = bremark; } public Bill(int bid, String btype, int bmoney, String bdate, String bremark) { this.bid = bid; this.btype = btype; this.bmoney = bmoney; this.bdate = bdate; this.bremark = bremark; } @Override public String toString() { return "Bill [bid=" + bid + ", btype=" + btype + ", bmoney=" + bmoney + ", bdate=" + bdate + ", bremark=" + bremark + "]"; } public int getBid() { return bid; } public void setBid(int bid) { this.bid = bid; } public String getBtype() { return btype; } public void setBtype(String btype) { this.btype = btype; } public int getBmoney() { return bmoney; } public void setBmoney(int bmoney) { this.bmoney = bmoney; } public String getBdate() { return bdate; } public void setBdate(String bdate) { this.bdate = bdate; } public String getBremark() { return bremark; } public void setBremark(String bremark) { this.bremark = bremark; } } package com.zzw.entity; public class User { private int uid; private String uname; private String upwd; private String usex; @Override public String toString() { return "User [uid=" + uid + ", uname=" + uname + ", upwd=" + upwd + ", usex=" + usex + "]"; } public User() { } public User( String uname, String upwd) { this.uname = uname; this.upwd = upwd; } public User( String uname, String upwd, String usex) { this.uname = uname; this.upwd = upwd; this.usex = usex; } public User(int uid, String uname, String upwd, String usex) { this.uid = uid; this.uname = uname; this.upwd = upwd; this.usex = usex; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } public String getUsex() { return usex; } public void setUsex(String usex) { this.usex = usex; } }
package com.zzw.service; import java.util.List; import com.zzw.entity.Bill; import com.zzw.entity.User; public interface IUserService { //登录 public boolean Login(User user); //注册 public boolean Register(User user) ; //根据账号查询用户 public User Query(String uname) ; //记账 public boolean AddBill(Bill bill) ; //根据账目编号进行删除 public boolean DeleteBill(int bid); //根据账目编号进行修改 public boolean UpdateBill(int bid,Bill bill) ; //根据账目编号查询账目 public Bill Query(int bid) ; //根据消费类型查询账单信息 public List<Bill> QueryType(String btype); //根据日期查询账单信息 public List<Bill> QueryDate(String bdate) ; //根据消费类型和日期查询账单信息 public List<Bill> QueryPart(String btype,String bdate) ; //查询全部账单信息 public List<Bill> QueryAll() ; } package com.zzw.service.Impl; import java.util.List; import com.zzw.dao.IUserDao; import com.zzw.dao.Impl.UserDaoImpl; import com.zzw.entity.Bill; import com.zzw.entity.User; import com.zzw.service.IUserService; public class UserServiceImpl implements IUserService{ IUserDao userdao= new UserDaoImpl(); //登录 public boolean Login(User user) { boolean flag=false; if(userdao.Login(user.getUname(),user.getUpwd())) { flag=true; } return flag; } //注册 public boolean Register(User user) { boolean flag=false; if(!userdao.isExist(user.getUname())) { userdao.Register(user); flag=true; }else { System.out.println("此人已存在"); } return flag; } //根据账号查询用户 public User Query(String uname) { return userdao.Query(uname); } //记账 public boolean AddBill(Bill bill) { boolean flag=false; if(userdao.AddBill(bill)) { flag=true; } return flag; } //根据账目编号进行删除 public boolean DeleteBill(int bid) { boolean flag=false; if(userdao.isExist(bid)) { userdao.DeleteBill(bid); flag=true; }else { System.out.println("此账目不存在"); } return flag; }
package com.zzw.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class DBUtil { //数据库URL和账号密码 private static final String URL="jdbc:mysql://localhost:3306/bookkeeping?serverTimezone=UTC&characterEncoding=utf-8"; private static final String UNAME="root"; private static final String UPWD="vayne"; public static PreparedStatement pstmt=null; public static ResultSet rs = null; public static Connection connection=null; //增删改 public static boolean executeUpdate(String sql,Object [] params) { boolean flag = false; try { //a.导入驱动,加载具体的驱动类 Class.forName("com.mysql.cj.jdbc.Driver"); //b.与数据库建立连接 connection = DriverManager.getConnection(URL,UNAME,UPWD); pstmt = connection.prepareStatement(sql); for(int i=0;i<params.length;i++) { pstmt.setObject(i+1, params[i]); } int count=pstmt.executeUpdate();//返回值表示,增删改几条数据 //处理结果 if(count>0) { System.out.println("操作成功!!!"); flag=true; } }catch(ClassNotFoundException e) { e.printStackTrace(); }catch(SQLException e) { e.printStackTrace(); }catch(Exception e){ e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(pstmt!=null)pstmt.close(); if(connection !=null)connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return flag; } //查 public static ResultSet executeQuery(String sql,Object [] params) { try { //a.导入驱动,加载具体的驱动类 Class.forName("com.mysql.cj.jdbc.Driver"); //b.与数据库建立连接 connection = DriverManager.getConnection(URL,UNAME,UPWD); pstmt = connection.prepareStatement(sql); if(params!=null) { for(int i=0;i<params.length;i++) { pstmt.setObject(i+1, params[i]); } } rs = pstmt.executeQuery(); return rs; }catch(ClassNotFoundException e) { e.printStackTrace(); return null; }catch(SQLException e) { e.printStackTrace(); return null; }catch(Exception e){ e.printStackTrace(); return null; } } }
以上是家庭记账本的后台的登陆部分等基本servlet