早几天,老师让我们做一个超市账单管理系统,做了一半之后,不想做了,发奋一样上网找源代码做参考,结果全部都要钱,或积分,逗~吐血~~~
硬着头皮坐下来了,不容易啊,对于初学者来说.
先写属性

1 package com.zhiyou.sm.entity; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.util.Date; 6 public class Bill { 7 private int id; 8 private String product_name; 9 private String product_desc; 10 private Date billTime; 11 private int isPay; 12 private int department; 13 private int provider_id;//外键,未设置 14 private String billMoney; 15 private int counts; 16 private String unit; 17 private String provider_name;//外键,未设置 18 public int getId() { 19 return id; 20 } 21 public void setId(int id) { 22 this.id = id; 23 } 24 public String getProduct_name() { 25 return product_name; 26 } 27 public void setProduct_name(String product_name) { 28 this.product_name = product_name; 29 } 30 public String getProduct_desc() { 31 return product_desc; 32 } 33 public void setProduct_desc(String product_desc) { 34 this.product_desc = product_desc; 35 } 36 public Date getBillTime() { 37 return billTime; 38 } 39 public void setBillTime(Date billTime) { 40 this.billTime = billTime; 41 } 42 public int getIsPay() { 43 return isPay; 44 } 45 public void setIsPay(int isPay) { 46 this.isPay = isPay; 47 } 48 public int getDepartment() { 49 return department; 50 } 51 public void setDepartment(int department) { 52 this.department = department; 53 } 54 public int getProvider_id() { 55 return provider_id; 56 } 57 public void setProvider_id(int provider_id) { 58 this.provider_id = provider_id; 59 } 60 public String getBillMoney() { 61 return billMoney; 62 } 63 public void setBillMoney(String billMoney) { 64 this.billMoney = billMoney; 65 } 66 public int getCounts() { 67 return counts; 68 } 69 public void setCounts(int counts) { 70 this.counts = counts; 71 } 72 public String getUnit() { 73 return unit; 74 } 75 public void setUnit(String unit) { 76 this.unit = unit; 77 } 78 public String getProvider_name() { 79 return provider_name; 80 } 81 public void setProvider_name(String provider_name) { 82 this.provider_name = provider_name; 83 } 84 public Bill(int id, String product_name, String product_desc, 85 Date billTime, int isPay, int department, int provider_id, 86 String billMoney, int counts, String unit, String provider_name) { 87 super(); 88 this.id = id; 89 this.product_name = product_name; 90 this.product_desc = product_desc; 91 this.billTime = billTime; 92 this.isPay = isPay; 93 this.department = department; 94 this.provider_id = provider_id; 95 this.billMoney = billMoney; 96 this.counts = counts; 97 this.unit = unit; 98 this.provider_name = provider_name; 99 } 100 public Bill() { 101 super(); 102 // TODO Auto-generated constructor stub 103 } 104 @Override 105 public String toString() { 106 return "Bill [id=" + id + ", product_name=" + product_name 107 + ", product_desc=" + product_desc + ", billTime=" + billTime 108 + ", isPay=" + isPay + ", department=" + department 109 + ", provider_id=" + provider_id + ", billMoney=" + billMoney 110 + ", counts=" + counts + ", unit=" + unit + ", provider_name=" 111 + provider_name + "]"; 112 } 113 114 115 }

1 package com.zhiyou.sm.entity; 2 /** 3 * @author 司徒魏源 4 */ 5 public class Provider { 6 private int proId;//编号 7 private String proName;//姓名 8 private String proContact;//联系人 9 private String proDesc;//描述 10 private String proPhone;//电话 11 private String proAdress;//地址 12 private String proFax;//传真 13 public int getProId() { 14 return proId; 15 } 16 public void setProId(int proId) { 17 this.proId = proId; 18 } 19 public String getProName() { 20 return proName; 21 } 22 public void setProName(String proName) { 23 this.proName = proName; 24 } 25 public String getProContact() { 26 return proContact; 27 } 28 public void setProContact(String proContact) { 29 this.proContact = proContact; 30 } 31 public String getProDesc() { 32 return proDesc; 33 } 34 public void setProDesc(String proDesc) { 35 this.proDesc = proDesc; 36 } 37 public String getProPhone() { 38 return proPhone; 39 } 40 public void setProPhone(String proPhone) { 41 this.proPhone = proPhone; 42 } 43 public String getProAdress() { 44 return proAdress; 45 } 46 public void setProAdress(String proAdress) { 47 this.proAdress = proAdress; 48 } 49 public String getProFax() { 50 return proFax; 51 } 52 public void setProFax(String proFax) { 53 this.proFax = proFax; 54 } 55 public Provider(int proId, String proName, String proContact, 56 String proDesc, String proPhone, String proAdress, String proFax) { 57 super(); 58 this.proId = proId; 59 this.proName = proName; 60 this.proContact = proContact; 61 this.proDesc = proDesc; 62 this.proPhone = proPhone; 63 this.proAdress = proAdress; 64 this.proFax = proFax; 65 } 66 public Provider() { 67 super(); 68 // TODO Auto-generated constructor stub 69 } 70 @Override 71 public String toString() { 72 return "Provider [proId=" + proId + ", proName=" + proName 73 + ", proContact=" + proContact + ", proDesc=" + proDesc 74 + ", proPhone=" + proPhone + ", proAdress=" + proAdress 75 + ", proFax=" + proFax + "]"; 76 } 77 78 79 }

1 package com.zhiyou.sm.entity; 2 /** 3 * @author 司徒魏源 4 */ 5 public class User { 6 private int id; 7 private String username; 8 private String password; 9 private int sex; 10 private int age; 11 private String mobile; 12 private String address; 13 private int common;//普通用户 14 private int superno;//管理员 15 public int getId() { 16 return id; 17 } 18 public void setId(int id) { 19 20 this.id = id; 21 } 22 public String getUsername() { 23 return username; 24 } 25 public void setUsername(String username) { 26 this.username = username; 27 } 28 public String getPassword() { 29 return password; 30 } 31 public void setPassword(String password) { 32 this.password = password; 33 } 34 public int getSex() { 35 return sex; 36 } 37 public void setSex(int sex) { 38 this.sex = sex; 39 } 40 public int getAge() { 41 return age; 42 } 43 public void setAge(int age) { 44 this.age = age; 45 } 46 public String getMobile() { 47 return mobile; 48 } 49 public void setMobile(String mobile) { 50 this.mobile = mobile; 51 } 52 public String getAddress() { 53 return address; 54 } 55 public void setAddress(String address) { 56 this.address = address; 57 } 58 public int getCommon() { 59 return common; 60 } 61 public void setCommon(int common) { 62 this.common = common; 63 } 64 public int getSuperno() { 65 return superno; 66 } 67 public void setSuperno(int superno) { 68 this.superno = superno; 69 } 70 public User(int id, String username, String password, int sex, int age, 71 String mobile, String address, int common, int superno) { 72 super(); 73 this.id = id; 74 this.username = username; 75 this.password = password; 76 this.sex = sex; 77 this.age = age; 78 this.mobile = mobile; 79 this.address = address; 80 this.common = common; 81 this.superno = superno; 82 } 83 public User() { 84 super(); 85 // TODO Auto-generated constructor stub 86 } 87 @Override 88 public String toString() { 89 return "User [id=" + id + ", username=" + username + ", password=" 90 + password + ", sex=" + sex + ", age=" + age + ", mobile=" 91 + mobile + ", address=" + address + ", common=" + common 92 + ", superno=" + superno + "]"; 93 } 94 95 96 }
dao层

1 package com.zhiyou.sm.dao; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.util.List; 6 import com.zhiyou.sm.entity.Bill; 7 public interface BillDao { 8 /** 9 * 获得所有 10 * @param object 11 * @return 12 */ 13 public List<Bill> getAll(String name,int ispay); 14 /** 15 * 通过编号获得一条记录 16 * @param id 17 * @return 18 */ 19 public Bill getBillById(int id); 20 /** 21 * 添加 22 * @param bill 23 * @return 24 */ 25 public boolean add(Bill bill); 26 /** 27 * 更新 28 * @param bill 29 * @return 30 */ 31 public boolean update(Bill bill); 32 /** 33 * 删除 34 * @param id 35 * @return 36 */ 37 public boolean delete(int id); 38 }

1 package com.zhiyou.sm.dao; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.util.List; 6 import com.zhiyou.sm.entity.Provider; 7 public interface ProviderDao { 8 /** 9 * 获得总记录数 10 * @param proName 11 * @param proDesc 12 * @return 13 */ 14 public int getTotalCount(String proName,String proDesc); 15 /** 16 * 根据条件进行模糊查询 17 * @param pageIndex,代表当前页 18 * @param proName 19 * @param proDesc 20 * @return 21 */ 22 public List<Provider> getAllByParams(int pageIndex,String proName,String proDesc); 23 /** 24 * 根据供应商编号获得一个对象 25 * @param proId 26 * @return 27 */ 28 public Provider getProByProId(int proId); 29 /** 30 * 添加操作 31 * @param provide 32 * @return 33 */ 34 public boolean add(Provider provider); 35 /** 36 * 更新操作 37 * @param provide 38 * @return 39 */ 40 public boolean update(Provider provider); 41 /** 42 * 根据编号删除 43 * @param proId 44 * @return 45 */ 46 public boolean delete(int proId); 47 }

1 package com.zhiyou.sm.dao; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.util.List; 6 import com.zhiyou.sm.entity.User; 7 public interface UserDao { 8 //增删改查的方法 9 /** 10 * 根据查询条件查询所有用户信息 11 * @param name 12 * @return 13 */ 14 public List<User> getAll(String name); 15 /** 16 * 根据用户编号获得用户 17 * @param id 18 * @return 19 */ 20 public User findUserByUserid(int id); 21 /** 22 * 添加一个用户 23 * @param user 24 * @return 25 */ 26 public boolean add(User user); 27 /** 28 * 更新用户信息 29 * @param user 30 * @return 31 */ 32 public boolean update(User user); 33 /** 34 * 根据主键删除用户 35 * @param id 36 * @return 37 */ 38 public boolean delete(int id); 39 }

1 package com.zhiyou.sm.dao; 2 import java.sql.Connection; 3 import java.sql.DriverManager; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import javax.naming.Context; 8 import javax.naming.InitialContext; 9 import javax.naming.NamingException; 10 import javax.sql.DataSource; 11 import com.zhiyou.sm.util.ConfigManager; 12 //ctrl + shift +o 13 /** 14 * 数据库的基类 15 * @author 司徒魏源 16 */ 17 public class BaseDao { 18 //建立连接 19 protected Connection conn=null; 20 //操作SQL的对象 21 protected PreparedStatement ps=null; 22 //结果集 23 protected ResultSet rs=null; 24 protected int count=0; 25 //利用数据源获得连接对象 26 public Connection getConnection2(){ 27 try { 28 //初始化上下文 29 Context cxt=new InitialContext(); 30 //获取与逻辑名相关联的数据源对象 31 DataSource ds; 32 ds = (DataSource)cxt.lookup("java:comp/env/jdbc/oracle"); 33 conn=ds.getConnection(); 34 } catch (NamingException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } catch (SQLException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } 41 return conn; 42 } 43 /* 44 * 获取连接 45 */ 46 public Connection getConnection(){ 47 /*String url="jdbc:oracle:thin:@localhost:1521:orcl"; 48 String username="scott"; 49 String password="tiger"; */ 50 String driver=ConfigManager.getInstance().getString("driver_class"); 51 String url=ConfigManager.getInstance().getString("url"); 52 String name=ConfigManager.getInstance().getString("username"); 53 String pwd=ConfigManager.getInstance().getString(""); 54 try { 55 //加载驱动 56 Class.forName(driver); 57 //建立连接 58 conn=DriverManager.getConnection(url,name,pwd); 59 } catch (ClassNotFoundException e) { 60 e.printStackTrace(); 61 } catch (SQLException e) { 62 e.printStackTrace(); 63 } 64 //返回连接 65 return conn; 66 } 67 /* 68 * 查询 69 * sql--SQL语句 70 * params参数列表 71 */ 72 public ResultSet executeQuery(String sql,Object [] params){ 73 //获取连接 74 this.conn=getConnection(); 75 // this.conn=getConnection2(); 76 try { 77 //获取PreparedStatement对象,通过连接对象获取 78 ps=conn.prepareStatement(sql); 79 //判断参数列表是否为空 80 if(params!=null){ 81 for (int i = 0; i < params.length; i++) { 82 //设置占位符 83 ps.setObject(i+1, params[i]); 84 } 85 } 86 //执行查询并返回结果集 87 rs=ps.executeQuery(); 88 } catch (SQLException e) { 89 e.printStackTrace(); 90 } 91 //返回结果集 92 return rs; 93 } 94 /* 95 * 增加删除改 96 */ 97 public int executeUpdate(String sql,Object[] params){ 98 //影响行数 99 int updateNum=0; 100 //获取连接 101 this.conn=getConnection(); 102 // this.conn=getConnection2(); 103 try { 104 //获取PreparedStatement对象,通过conn 105 ps=conn.prepareStatement(sql); 106 //判断参数列表是否为空 107 if(params!=null){ 108 //设置占位符 109 for (int i = 0; i < params.length; i++) { 110 ps.setObject(i+1, params[i]); 111 } 112 } 113 //执行,增,删除,修改 114 count=ps.executeUpdate(); 115 } catch (SQLException e) { 116 e.printStackTrace(); 117 }finally{ 118 //关闭连接 119 closeConnection(); 120 } 121 //返回影响行数 122 return count; 123 } 124 /* 125 * 关闭连接 126 */ 127 public void closeConnection(){ 128 try { 129 if(rs!=null) 130 rs.close(); 131 } catch (SQLException e) { 132 e.printStackTrace(); 133 } 134 try { 135 if(ps!=null) 136 ps.close(); 137 } catch (SQLException e) { 138 e.printStackTrace(); 139 } 140 try { 141 if(conn!=null) 142 conn.close(); 143 } catch (SQLException e) { 144 e.printStackTrace(); 145 } 146 } 147 }
dao层impl

1 package com.zhiyou.sm.dao.impl; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.sql.SQLException; 6 import java.util.ArrayList; 7 import java.util.Date; 8 import java.util.List; 9 import com.zhiyou.sm.dao.BaseDao; 10 import com.zhiyou.sm.dao.BillDao; 11 import com.zhiyou.sm.entity.Bill; 12 public class BillDaoImpl extends BaseDao implements BillDao{ 13 @Override 14 public List<Bill> getAll(String name, int ispay) { 15 List<Bill> list = new ArrayList<Bill>(); 16 List<Object> paramsList = new ArrayList<Object>(); 17 StringBuffer buffer = new StringBuffer(" select * from bill b where 1=1 "); 18 String sql = buffer.toString(); 19 Object[] params = new Object[paramsList.size()]; 20 for (int i = 0; i < paramsList.size(); i++) { 21 params[i] = paramsList.get(i); 22 } 23 executeQuery(sql, params); 24 try { 25 while(rs.next()){ 26 int id = rs.getInt(1); 27 String product_name=rs.getString(2); 28 String product_desc=rs.getString(3); 29 Date billTime=rs.getDate(4); 30 int isPay=rs.getInt(5); 31 int department=rs.getInt(6); 32 int provider_id=rs.getInt(7); 33 String billMoney=rs.getString(8); 34 int counts=rs.getInt(9); 35 String unit=rs.getString(10); 36 String provider_name=rs.getString(11); 37 Bill bill = new Bill(id, product_name, product_desc, billTime, isPay, department, provider_id, billMoney, counts, unit, provider_name); 38 list.add(bill); 39 } 40 } catch (SQLException e) { 41 e.printStackTrace(); 42 } 43 return list; 44 } 45 public static void main(String[] args) { 46 BillDaoImpl b = new BillDaoImpl(); 47 List<Bill> list = b.getAll("", 0); 48 System.out.println(list); 49 } 50 @Override 51 public Bill getBillById(int id) { 52 Bill bill = null; 53 String sql="select * from bill where id = ?"; 54 Object[] params={id}; 55 executeQuery(sql, params); 56 try { 57 while(rs.next()){ 58 String product_name=rs.getString(2); 59 String product_desc=rs.getString(3); 60 Date billTime=rs.getDate(4); 61 int isPay=rs.getInt(5); 62 int department=rs.getInt(6); 63 int provider_id=rs.getInt(7); 64 String billMoney=rs.getString(8); 65 int counts=rs.getInt(9); 66 String unit=rs.getString(10); 67 String provider_name=rs.getString(11); 68 bill = new Bill(id, product_name, product_desc, billTime, isPay, department, provider_id, billMoney, counts, unit, provider_name); 69 } 70 } catch (SQLException e) { 71 e.printStackTrace(); 72 } 73 return bill; 74 } 75 @Override 76 public boolean add(Bill bill) { 77 78 String sql="insert into bill (id,product_name,product_desc,bill_time,ispay,department,provider_Id,bill_money,counts,unit,provider_name) values(?,?,?,?,?,?,?,?,?,?,?)"; 79 Object[] params={bill.getId(),bill.getProduct_name(),bill.getProduct_desc(),new java.sql.Date(bill.getBillTime().getTime()),bill.getIsPay(),bill.getDepartment(),bill.getProvider_id(),bill.getBillMoney(),bill.getCounts(),bill.getUnit(),bill.getProvider_name()}; 80 executeUpdate(sql, params); 81 if(count>0){ 82 return true; 83 } 84 return false; 85 } 86 @Override 87 public boolean update(Bill bill) { 88 String sql="update bill set product_name=?,product_desc=?,bill_time=?,ispay=?,department=?,provider_id=?,bill_money=?,counts=?,unit=?,provider_name=? where id=?"; 89 Object[] params={bill.getProduct_name(),bill.getProduct_desc(),bill.getBillTime(),bill.getIsPay(),bill.getDepartment(),bill.getProvider_id(),bill.getBillMoney(),bill.getCounts(),bill.getUnit(),bill.getProvider_name(),bill.getId()}; 90 executeUpdate(sql, params); 91 if(count>0){ 92 return true; 93 } 94 return false; 95 } 96 @Override 97 public boolean delete(int id) { 98 String sql="delete from bill where id=?"; 99 Object[] params={id}; 100 executeUpdate(sql, params); 101 if(count>0){ 102 return true; 103 } 104 return false; 105 } 106 }

1 package com.zhiyou.sm.dao.impl; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.sql.SQLException; 6 import java.util.ArrayList; 7 import java.util.List; 8 import com.zhiyou.sm.dao.BaseDao; 9 import com.zhiyou.sm.dao.ProviderDao; 10 import com.zhiyou.sm.entity.Provider; 11 public class ProviderDaoImpl extends BaseDao implements ProviderDao { 12 @Override 13 public List<Provider> getAllByParams(int pageIndex, String proName, String proDesc) { 14 //创建存放供应商的集合 15 List<Provider> list=new ArrayList<Provider>(); 16 //存放参数的集合 17 List<Object> paramsList = new ArrayList<Object>(); 18 //拼接字符串,StringBuffer,拼接sql语句 19 StringBuffer buffer=new StringBuffer("select * from provider where 1=1 "); 20 if(proName!=null&&!"".equals(proName)){ 21 //动态拼接sql语句 22 buffer.append(" and pro_name like ? "); 23 //添加参数 24 paramsList.add("%"+proName+"%"); 25 } 26 if(proDesc!=null&&!"".equals(proDesc)){ 27 buffer.append(" and pro_desc like ? "); 28 paramsList.add("%"+proDesc+"%"); 29 } 30 buffer.append(" limit ?,? "); 31 //每页显示的个数 32 int pageSize=3; 33 //开始记录数startRow 34 int startRow=(pageIndex-1)*pageSize; 35 //添加参数 36 paramsList.add(startRow); 37 paramsList.add(pageSize); 38 String sql=buffer.toString(); 39 Object[] params=new Object[paramsList.size()]; 40 //循环集合中的数据,赋值给数组元素 41 for (int i=0;i<paramsList.size();i++) { 42 params[i]=paramsList.get(i); 43 } 44 executeQuery(sql, params); 45 try { 46 while(rs.next()){ 47 int proId=rs.getInt(1); 48 String proname = rs.getString(2);// 姓名 49 String proContact = rs.getString(3);// 联系人 50 String prodesc = rs.getString(4);// 描述 51 String proPhone = rs.getString(5);// 电话 52 String proAdress = rs.getString(6);// 地址 53 String proFax = rs.getString(7);// 传真 54 //创建供应商对象 55 Provider pro=new Provider(proId, proname, proContact, prodesc, proPhone, proAdress, proFax); 56 //把供应商对象添加到集合 57 list.add(pro); 58 } 59 } catch (SQLException e) { 60 // TODO Auto-generated catch block 61 e.printStackTrace(); 62 }finally{ 63 //关闭资源 64 closeConnection(); 65 } 66 return list; 67 } 68 @Override 69 public Provider getProByProId(int proId) { 70 Provider pro = null; 71 String sql = "select * from provider where id=?"; 72 Object[] params = { proId }; 73 executeQuery(sql, params); 74 try { 75 while (rs.next()) { 76 String proName = rs.getString(2);// 姓名 77 String proContact = rs.getString(3);// 联系人 78 String proDesc = rs.getString(4);// 描述 79 String proPhone = rs.getString(5);// 电话 80 String proAdress = rs.getString(6);// 地址 81 String proFax = rs.getString(7);// 传真 82 pro = new Provider(proId, proName, proContact, proDesc, 83 proPhone, proAdress, proFax); 84 } 85 } catch (SQLException e) { 86 // TODO Auto-generated catch block 87 e.printStackTrace(); 88 } 89 return pro; 90 } 91 @Override 92 public boolean add(Provider provider) { 93 String sql = "insert into provider (pro_name,pro_contact,pro_desc,pro_phone,pro_address,pro_fax) values(?,?,?,?,?,?)"; 94 Object[] params = { provider.getProName(), provider.getProContact(), 95 provider.getProDesc(), provider.getProPhone(), 96 provider.getProAdress(), provider.getProFax() }; 97 executeUpdate(sql, params); 98 if (count > 0) 99 return true; 100 return false; 101 } 102 @Override 103 public boolean update(Provider provider) { 104 String sql = "update provider set pro_name=?,pro_contact=?,pro_desc=?,pro_phone=?,pro_address=?,pro_fax=? where id=?"; 105 Object[] params = { provider.getProName(), provider.getProContact(), 106 provider.getProDesc(), provider.getProPhone(), 107 provider.getProAdress(), provider.getProFax(), 108 provider.getProId() }; 109 executeUpdate(sql, params); 110 if (count > 0) 111 return true; 112 return false; 113 } 114 @Override 115 public boolean delete(int proId) { 116 String sql = "delete from provider where id=?"; 117 Object[] params = { proId }; 118 executeUpdate(sql, params); 119 if (count > 0) 120 return true; 121 return false; 122 } 123 @Override 124 public int getTotalCount(String proName, String proDesc) { 125 //存放总记录数的变量 126 int totalCount=0; 127 //存放参数的集合 128 List<Object> paramsList = new ArrayList<Object>(); 129 //拼接字符串,StringBuffer,拼接sql语句 130 StringBuffer buffer=new StringBuffer("select count(*) from provider where 1=1 "); 131 if(proName!=null&&!"".equals(proName)){ 132 //动态拼接sql语句 133 buffer.append(" and pro_name like ? "); 134 //添加参数 135 paramsList.add("%"+proName+"%"); 136 } 137 if(proDesc!=null&&!"".equals(proDesc)){ 138 buffer.append(" and pro_desc like ? "); 139 paramsList.add("%"+proDesc+"%"); 140 } 141 142 String sql=buffer.toString(); 143 Object[] params=new Object[paramsList.size()]; 144 //循环集合中的数据,赋值给数组元素 145 for (int i=0;i<paramsList.size();i++) { 146 params[i]=paramsList.get(i); 147 } 148 executeQuery(sql, params); 149 try { 150 while(rs.next()){ 151 totalCount=rs.getInt(1); 152 } 153 } catch (SQLException e) { 154 // TODO Auto-generated catch block 155 e.printStackTrace(); 156 }finally{ 157 //关闭资源 158 closeConnection(); 159 } 160 return totalCount; 161 } 162 }

1 package com.zhiyou.sm.dao.impl; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.sql.SQLException; 6 import java.util.ArrayList; 7 import java.util.List; 8 import com.zhiyou.sm.dao.BaseDao; 9 import com.zhiyou.sm.dao.UserDao; 10 import com.zhiyou.sm.entity.User; 11 public class UserDaoImpl extends BaseDao implements UserDao { 12 @Override 13 public List<User> getAll(String name) { 14 List<User> list = new ArrayList<User>(); 15 String sql=""; 16 Object[] params =null; 17 if(name==null||"".equals(name)){ 18 sql="select * from market_user"; 19 params=new Object[]{}; 20 }else{ 21 sql = "select * from market_user where username like ? "; 22 params =new Object[] {"%"+name+"%"}; 23 } 24 executeQuery(sql, params); 25 try { 26 while (rs.next()) { 27 int id = rs.getInt(1); 28 String username = rs.getString(2); 29 String password = rs.getString(3); 30 int sex = rs.getInt(4); 31 int age = rs.getInt(5); 32 String mobile = rs.getString(6); 33 String address = rs.getString(7); 34 int common = rs.getInt(8);// 普通用户 35 int superno = rs.getInt(9);// 管理员 36 User user = new User(id, username, password, sex, age, mobile, 37 address, common, superno); 38 list.add(user); 39 } 40 } catch (SQLException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } 44 return list; 45 } 46 @Override 47 public User findUserByUserid(int id) { 48 User user = null; 49 String sql = "select * from market_user where id=?"; 50 Object[] params = { id }; 51 executeQuery(sql, params); 52 try { 53 while (rs.next()) { 54 String username = rs.getString(2); 55 String password = rs.getString(3); 56 int sex = rs.getInt(4); 57 int age = rs.getInt(5); 58 String mobile = rs.getString(6); 59 String address = rs.getString(7); 60 int common = rs.getInt(8);// 普通用户 61 int superno = rs.getInt(9);// 管理员 62 user = new User(id, username, password, sex, age, mobile, 63 address, common, superno); 64 } 65 } catch (SQLException e) { 66 // TODO Auto-generated catch block 67 e.printStackTrace(); 68 } 69 return user; 70 } 71 @Override 72 public boolean add(User user) { 73 String sql="INSERT INTO market_user (USERNAME,PASSWORD,sex,age,MOBILE,ADDRESS,common,SUPER) VALUES (?,?,?,?,?,?,?,?)"; 74 Object[] params={user.getUsername(),user.getPassword(),user.getSex(),user.getAge(),user.getMobile(),user.getAddress(),user.getCommon(),user.getSuperno()}; 75 executeUpdate(sql, params); 76 if(count>0) 77 return true; 78 return false; 79 } 80 @Override 81 public boolean update(User user) { 82 String sql="UPDATE market_user SET USERNAME=?,sex=?,age=?,MOBILE=?,ADDRESS=?,common=?,SUPER=? WHERE id=?"; 83 Object[] params={user.getUsername(),user.getSex(),user.getAge(),user.getMobile(),user.getAddress(),user.getCommon(),user.getSuperno(),user.getId()}; 84 executeUpdate(sql, params); 85 if(count>0) 86 return true; 87 return false; 88 } 89 @Override 90 public boolean delete(int id) { 91 String sql="DELETE FROM market_user WHERE id=?"; 92 Object[] params={id}; 93 executeUpdate(sql, params); 94 if(count>0) 95 return true; 96 return false; 97 } 98 }
filtre层

1 package com.zhiyou.sm.filtre; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.io.IOException; 6 import javax.servlet.Filter; 7 import javax.servlet.FilterChain; 8 import javax.servlet.FilterConfig; 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletRequest; 11 import javax.servlet.ServletResponse; 12 import javax.servlet.annotation.WebFilter; 13 import javax.servlet.annotation.WebInitParam; 14 @WebFilter(urlPatterns = "/*",initParams={@WebInitParam(name="code",value="utf-8")}) 15 public class CharSet implements Filter { 16 String myCode=null; 17 @Override 18 public void destroy() { 19 // TODO Auto-generated method stub 20 } 21 @Override 22 public void doFilter(ServletRequest req, ServletResponse resp, 23 FilterChain chain) throws IOException, ServletException { 24 //设置post提交,解决字符编码 25 req.setCharacterEncoding(myCode); 26 //设置响应的字符编码集 27 resp.setCharacterEncoding(myCode); 28 //web资源继续想下执行 29 chain.doFilter(req, resp); 30 } 31 @Override 32 public void init(FilterConfig arg0) throws ServletException { 33 myCode=arg0.getInitParameter("code"); 34 } 35 }
service层

1 package com.zhiyou.sm.service; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.util.List; 6 import com.zhiyou.sm.entity.Bill; 7 public interface BillService { 8 /** 9 * 获得所有 10 * @param object 11 * @return 12 */ 13 public List<Bill> getAll(String name,int ispay); 14 /** 15 * 通过编号获得一条记录 16 * @param id 17 * @return 18 */ 19 public Bill getBillById(int id); 20 /** 21 * 添加 22 * @param bill 23 * @return 24 */ 25 public boolean add(Bill bill); 26 /** 27 * 更新 28 * @param bill 29 * @return 30 */ 31 public boolean update(Bill bill); 32 /** 33 * 删除 34 * @param id 35 * @return 36 */ 37 public boolean delete(int id); 38 }

1 package com.zhiyou.sm.service; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.util.List; 6 import com.zhiyou.sm.entity.Provider; 7 public interface ProviderService { 8 /** 9 * 获得总页数 10 * @param proName 11 * @param proDesc 12 * @return 13 */ 14 public int getTotalpage(String proName,String proDesc); 15 /** 16 * 根据条件进行模糊查询 17 * @param pageIndex,代表当前页 18 * @param proName 19 * @param proDesc 20 * @return 21 */ 22 public List<Provider> getAllByParams(int pageIndex,String proName,String proDesc); 23 /** 24 * 根据供应商编号获得一个对象 25 * @param proId 26 * @return 27 */ 28 public Provider getProByProId(int proId); 29 /** 30 * 添加操作 31 * @param provide 32 * @return 33 */ 34 public boolean add(Provider provider); 35 /** 36 * 更新操作 37 * @param provide 38 * @return 39 */ 40 public boolean update(Provider provider); 41 /** 42 * 根据编号删除 43 * @param proId 44 * @return 45 */ 46 public boolean delete(int proId); 47 }

1 package com.zhiyou.sm.service; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.util.List; 6 import java.util.Map; 7 import com.zhiyou.sm.entity.User; 8 public interface UserService { 9 /** 10 * 根据查询条件查询所有用户信息 11 * @param name 12 * @return 13 */ 14 public List<User> getAll(String name); 15 /** 16 * 根据用户编号获得用户 17 * @param id 18 * @return 19 */ 20 public User findUserByUserid(int id); 21 /** 22 * 添加一个用户 23 * @param user 24 * @return 25 */ 26 public boolean add(User user); 27 /** 28 * 更新用户信息 29 * @param user 30 * @return 31 */ 32 public boolean update(User user); 33 /** 34 * 根据主键删除用户 35 * @param id 36 * @return 37 */ 38 public boolean delete(int id); 39 /** 40 * 验证登录操作 41 * @param id 42 * @param pwd 43 * @return 44 */ 45 public Map<String,Object> login(int id, String pwd); 46 }
service层impl

1 package com.zhiyou.sm.service.impl; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.util.List; 6 import com.zhiyou.sm.dao.BillDao; 7 import com.zhiyou.sm.dao.impl.BillDaoImpl; 8 import com.zhiyou.sm.entity.Bill; 9 import com.zhiyou.sm.service.BillService; 10 public class BillServiceImpl implements BillService { 11 //创建dao层对象 12 BillDao bd= new BillDaoImpl(); 13 @Override 14 public List<Bill> getAll(String name, int ispay) { 15 // TODO Auto-generated method stub 16 return bd.getAll(name, ispay); 17 } 18 @Override 19 public Bill getBillById(int id) { 20 // TODO Auto-generated method stub 21 return bd.getBillById(id); 22 } 23 @Override 24 public boolean add(Bill bill) { 25 // TODO Auto-generated method stub 26 return bd.add(bill); 27 } 28 @Override 29 public boolean update(Bill bill) { 30 // TODO Auto-generated method stub 31 return bd.update(bill); 32 } 33 @Override 34 public boolean delete(int id) { 35 // TODO Auto-generated method stub 36 return bd.delete(id); 37 } 38 }

1 package com.zhiyou.sm.service.impl; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.util.List; 6 import com.zhiyou.sm.dao.ProviderDao; 7 import com.zhiyou.sm.dao.impl.ProviderDaoImpl; 8 import com.zhiyou.sm.entity.Provider; 9 import com.zhiyou.sm.service.ProviderService; 10 public class ProviderServiceImpl implements ProviderService { 11 //创建dao层对象 12 ProviderDao dao=new ProviderDaoImpl(); 13 @Override 14 public int getTotalpage(String proName, String proDesc) { 15 // 首先获得总记录数 16 int totalCount=dao.getTotalCount(proName, proDesc); 17 //总记录数%每页显示的个数==0?总记录数/每页显示的个数:总记录数/每页显示的个数+1 18 // 16 % 4 ==0? 4 : 5 19 return totalCount%3==0?totalCount/3:totalCount/3+1; 20 } 21 @Override 22 public List<Provider> getAllByParams(int pageIndex, String proName, 23 String proDesc) { 24 // TODO Auto-generated method stub 25 return dao.getAllByParams(pageIndex, proName, proDesc); 26 } 27 @Override 28 public Provider getProByProId(int proId) { 29 // TODO Auto-generated method stub 30 return dao.getProByProId(proId); 31 } 32 @Override 33 public boolean add(Provider provider) { 34 // TODO Auto-generated method stub 35 return dao.add(provider); 36 } 37 @Override 38 public boolean update(Provider provider) { 39 // TODO Auto-generated method stub 40 return dao.update(provider); 41 } 42 @Override 43 public boolean delete(int proId) { 44 // TODO Auto-generated method stub 45 return dao.delete(proId); 46 } 47 }

1 package com.zhiyou.sm.service.impl; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 import com.zhiyou.sm.dao.UserDao; 9 import com.zhiyou.sm.dao.impl.UserDaoImpl; 10 import com.zhiyou.sm.entity.User; 11 import com.zhiyou.sm.service.UserService; 12 import com.zhiyou.sm.util.Base64; 13 public class UserServiceImpl implements UserService { 14 UserDao dao=new UserDaoImpl(); 15 @Override 16 public List<User> getAll(String name) { 17 // TODO Auto-generated method stub 18 return dao.getAll(name); 19 } 20 @Override 21 public User findUserByUserid(int id) { 22 // TODO Auto-generated method stub 23 return dao.findUserByUserid(id); 24 } 25 @Override 26 public boolean add(User user) { 27 // TODO Auto-generated method stub 28 return dao.add(user); 29 } 30 @Override 31 public boolean update(User user) { 32 // TODO Auto-generated method stub 33 return dao.update(user); 34 } 35 @Override 36 public boolean delete(int id) { 37 // TODO Auto-generated method stub 38 return dao.delete(id); 39 } 40 @Override 41 public Map<String, Object> login(int id, String pwd) { 42 Map<String, Object> map = new HashMap<String, Object>(); 43 User user = dao.findUserByUserid(id); 44 if (user == null) { 45 // 用户名不存在 46 map.put("isLogin", false); 47 map.put("errMess", "员工编号错误"); 48 } else { 49 String encode=user.getPassword(); 50 //传入加密的密码,进行解密 51 String decode=Base64.decode(encode); 52 // 名称存在,要验证密码 53 if (decode.equals(pwd)) { 54 // 登录成功 55 map.put("isLogin", true); 56 map.put("user", user); 57 } else { 58 // 密码错误 59 map.put("isLogin", false); 60 map.put("errMess", "密码错误"); 61 } 62 } 63 return map; 64 } 65 }
工具包

1 package com.zhiyou.sm.util; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.io.IOException; 6 import java.io.UnsupportedEncodingException; 7 import sun.misc.BASE64Decoder; 8 import sun.misc.BASE64Encoder; 9 public class Base64 { 10 /** 11 * 加密的过程 12 * 13 * @param str 14 * @return 15 */ 16 public static String encode(String str) { 17 BASE64Encoder base = new BASE64Encoder(); 18 try { 19 str = base.encode(str.getBytes("utf-8")); 20 } catch (UnsupportedEncodingException e) { 21 // TODO Auto-generated catch block 22 e.printStackTrace(); 23 } 24 return str; 25 } 26 /** 27 * 加密的过程 28 * 29 * @param str 30 * @return 31 */ 32 public static String decode(String str) { 33 BASE64Decoder bd = new BASE64Decoder(); 34 byte[] b; 35 String result=null; 36 try { 37 b = bd.decodeBuffer(str); 38 result = new String(b, "utf-8"); 39 } catch (IOException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 return result; 44 } 45 }

1 package com.zhiyou.sm.util; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.util.Properties; 8 public class ConfigManager { 9 //读取配置文件properties.load(inputstream); 10 private static ConfigManager configManager; 11 private static Properties properties; 12 private ConfigManager(){ 13 String configFile="db.properties"; 14 properties = new Properties(); 15 InputStream is = ConfigManager.class.getClassLoader().getResourceAsStream(configFile); 16 try { 17 properties.load(is); 18 is.close(); 19 } catch (IOException e) { 20 // TODO Auto-generated catch block 21 e.printStackTrace(); 22 } 23 } 24 public static ConfigManager getInstance(){ 25 if(configManager==null){ 26 configManager = new ConfigManager(); 27 } 28 return configManager ; 29 } 30 //取得key对应的value�? 31 public String getString(String key){ 32 return properties.getProperty(key); 33 } 34 }
db.properties配置
driver_class= com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/market
username=root
password=
-----------------分割线--------------
action层

1 package com.zhiyou.sm.action; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.io.IOException; 6 import java.util.Date; 7 import java.util.List; 8 import javax.servlet.ServletException; 9 import javax.servlet.annotation.WebServlet; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 import com.zhiyou.sm.entity.Bill; 14 import com.zhiyou.sm.service.BillService; 15 import com.zhiyou.sm.service.impl.BillServiceImpl; 16 @WebServlet(urlPatterns = "/bill.do") 17 public class BillServlet extends HttpServlet { 18 BillService bs = new BillServiceImpl(); 19 protected void doGet(HttpServletRequest request, 20 HttpServletResponse response) throws ServletException, IOException { 21 doPost(request, response); 22 } 23 protected void doPost(HttpServletRequest request, 24 HttpServletResponse response) throws ServletException, IOException { 25 String flag = request.getParameter("flag1"); 26 if ("doAdd".equals(flag)) { 27 doAdd1(request, response); 28 } else if ("update".equals(flag)) { 29 doUpdate1(request, response); 30 } else if ("delete".equals(flag)) { 31 doDelete1(request, response); 32 } 33 String name = request.getParameter("productName"); 34 String status = request.getParameter("payStatus"); 35 List<Bill> bills = bs.getAll(name, 1); 36 request.setAttribute("blist", bills); 37 request.getRequestDispatcher("admin_bill_list.jsp").forward(request, 38 response); 39 } 40 protected void doAdd1(HttpServletRequest request, 41 HttpServletResponse response) throws ServletException, IOException { 42 String money = request.getParameter("money"); 43 String unit = request.getParameter("unit"); 44 String countsStr = request.getParameter("counts"); 45 int counts = Integer.valueOf(countsStr); 46 String product_name = request.getParameter("product_name"); 47 String desc = request.getParameter("desc"); 48 String viderId = request.getParameter("provider_id"); 49 int vid = Integer.valueOf(viderId); 50 String ispayStr = request.getParameter("ispay"); 51 int isPay = Integer.valueOf(ispayStr); 52 Bill bill = new Bill(0, product_name, desc, new Date(), isPay, 1, vid, 53 money, 1, unit, "haha"); 54 boolean isAdd = bs.add(bill); 55 if (!isAdd) { 56 request.setAttribute("errMess", "添加失败"); 57 } 58 } 59 protected void doUpdate1(HttpServletRequest request, 60 HttpServletResponse response) throws ServletException, IOException { 61 String idStr=request.getParameter("billNum"); 62 int id=Integer.valueOf(idStr); 63 String money = request.getParameter("money"); 64 String unit = request.getParameter("unit"); 65 String countsStr = request.getParameter("counts"); 66 int counts = Integer.valueOf(countsStr); 67 String product_name = request.getParameter("product_name"); 68 String desc = request.getParameter("desc"); 69 String viderId = request.getParameter("provider_id"); 70 int vid = Integer.valueOf(viderId); 71 String ispayStr = request.getParameter("ispay"); 72 int isPay = Integer.valueOf(ispayStr); 73 Bill bill = bs.getBillById(id); 74 bill.setBillMoney(money); 75 bill.setUnit(unit); 76 bill.setCounts(counts); 77 bill.setProduct_name(product_name); 78 bill.setProduct_desc(desc); 79 bill.setProvider_id(vid); 80 bill.setIsPay(isPay); 81 boolean isUpdate = bs.update(bill); 82 if (!isUpdate) { 83 request.setAttribute("errMess", "更新错误"); 84 } 85 } 86 protected void doDelete1(HttpServletRequest request, 87 HttpServletResponse response) throws ServletException, IOException { 88 String idStr = request.getParameter("pid"); 89 int id = Integer.parseInt(idStr); 90 boolean isDel = bs.delete(id); 91 if (!isDel) { 92 request.setAttribute("errMess", "删除失败"); 93 } 94 } 95 }

1 package com.zhiyou.sm.action; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.io.IOException; 6 import java.util.Map; 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import com.zhiyou.sm.service.UserService; 13 import com.zhiyou.sm.service.impl.UserServiceImpl; 14 @WebServlet(urlPatterns = "/login.do") 15 public class LoginServlet extends HttpServlet { 16 // 创建service层对象 17 private UserService us = new UserServiceImpl(); 18 @Override 19 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 20 throws ServletException, IOException { 21 doPost(req, resp); 22 } 23 @Override 24 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 25 throws ServletException, IOException { 26 // 中文乱码用过滤器 27 // 获得用户编号和密码 28 String idStr = req.getParameter("uid"); 29 String pwd = req.getParameter("password"); 30 // 获得验证码 31 String ver = req.getParameter("ver"); 32 // 获得生成存在session中的验证码 33 String code = (String) req.getSession().getAttribute("rCode"); 34 if (!code.equals(ver)) { 35 // 验证码错误 36 req.setAttribute("errMess", "验证码错误,请重新输入"); 37 //转发回登录页面 38 req.getRequestDispatcher("login.jsp").forward(req, resp); 39 } else { 40 // 验证码正确,接着验证用户名和密码 41 int id = Integer.parseInt(idStr); 42 Map<String, Object> map = us.login(id, pwd); 43 boolean isLogin = (Boolean) map.get("isLogin"); 44 if (isLogin) { 45 // 登录成功 46 // 把登录的用户对象放入到session 47 req.getSession().setAttribute("user1", map.get("user")); 48 resp.sendRedirect("admin_index.jsp"); 49 } else { 50 // 登录失败 51 String err = (String) map.get("errMess"); 52 // 重定向到登陆界面 53 req.setAttribute("errMess", err); 54 req.getRequestDispatcher("login.jsp").forward(req, resp); 55 } 56 } 57 } 58 }

1 package com.zhiyou.sm.action; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.io.IOException; 6 import java.util.List; 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import com.zhiyou.sm.entity.Provider; 13 import com.zhiyou.sm.service.ProviderService; 14 import com.zhiyou.sm.service.impl.ProviderServiceImpl; 15 @WebServlet(urlPatterns="/pro.do") 16 public class ProviderServlet extends HttpServlet { 17 //创建业务层对象 18 ProviderService ps=new ProviderServiceImpl(); 19 @Override 20 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 21 throws ServletException, IOException { 22 doPost(req, resp); 23 } 24 @Override 25 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 26 throws ServletException, IOException { 27 String flag=req.getParameter("flag1"); 28 if("doAdd".equals(flag)){ 29 doAdd(req,resp); 30 }else if("update".equals(flag)){ 31 doUpdate(req,resp); 32 }else if("delete".equals(flag)){ 33 dodelete(req,resp); 34 } 35 //根据分页取出所有供应商的信息 36 //得到当前页 37 int pageIndex=1; 38 String currentPage=req.getParameter("pageIndex"); 39 if(currentPage==null||"".equals(currentPage)){ 40 currentPage="1";//设置初始值是第一页 41 } 42 pageIndex=Integer.parseInt(currentPage); 43 String proName=req.getParameter("proName"); 44 String proDesc=req.getParameter("proDesc"); 45 //调用获得所有记录的集合 46 List<Provider> proList = ps.getAllByParams(pageIndex, proName, proDesc); 47 //把集合放入到作用域 48 req.setAttribute("proList", proList); 49 //把pageIndex放入到作用域 50 req.setAttribute("pageIndex", pageIndex); 51 //把取到的名称放入到作用域 52 req.setAttribute("proName", proName); 53 //把取到的描述放入到作用域 54 req.setAttribute("proDesc", proDesc); 55 //得到总页数放入到作用域 56 int totalpage=ps.getTotalpage(proName, proDesc); 57 req.setAttribute("totalpage", totalpage); 58 //转发到providerAdmin.jsp页面 59 req.getRequestDispatcher("providerAdmin.jsp").forward(req, resp); 60 } 61 private void dodelete(HttpServletRequest req, HttpServletResponse resp) { 62 String idStr = req.getParameter("id"); 63 int id = Integer.parseInt(idStr); 64 boolean isDel = ps.delete(id); 65 if (!isDel) { 66 req.setAttribute("errMess", "删除失败"); 67 } 68 } 69 private void doUpdate(HttpServletRequest req, HttpServletResponse resp) { 70 String proIds = req.getParameter("proId");// 编号 71 String proName = req.getParameter("proName");// 姓名 72 String proContact = req.getParameter("contact");// 联系人 73 String proDesc = req.getParameter("proDesc");// 描述 74 String proPhone = req.getParameter("phone");// 电话 75 String proAdress = req.getParameter("address");// 地址 76 String proFax = req.getParameter("fax");// 传真 77 int proId = Integer.parseInt(proIds); 78 // 通过id获得数据库中的记录 79 Provider provider = ps.getProByProId(proId); 80 provider.setProName(proName); 81 provider.setProContact(proContact); 82 provider.setProDesc(proDesc); 83 provider.setProPhone(proPhone); 84 provider.setProAdress(proAdress); 85 provider.setProFax(proFax); 86 boolean isUpdate = ps.update(provider); 87 if (!isUpdate) { 88 req.setAttribute("errMess", "更新失败"); 89 } 90 } 91 private void doAdd(HttpServletRequest req, HttpServletResponse resp) { 92 //添加操作 93 String proName=req.getParameter("proName"); 94 String proDesc=req.getParameter("proDesc"); 95 String proPhone=req.getParameter("proPhone"); 96 String proContact=req.getParameter("proContact"); 97 String proFax=req.getParameter("proFax"); 98 String proAddress=req.getParameter("proAddress"); 99 Provider pro=new Provider(0, proName, proContact, proDesc, proPhone, proAddress, proFax); 100 boolean isAdd=ps.add(pro); 101 if (!isAdd) { 102 req.setAttribute("errMess", "添加失败"); 103 } 104 } 105 }

1 package com.zhiyou.sm.action; 2 /** 3 * @author 司徒魏源 4 */ 5 import java.io.IOException; 6 import java.util.List; 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import com.zhiyou.sm.entity.User; 13 import com.zhiyou.sm.service.UserService; 14 import com.zhiyou.sm.service.impl.UserServiceImpl; 15 import com.zhiyou.sm.util.Base64; 16 @WebServlet(urlPatterns = "/user.do") 17 public class UserServlet extends HttpServlet { 18 // Service层 19 private UserService us = new UserServiceImpl(); 20 @Override 21 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 22 throws ServletException, IOException { 23 //req.setCharacterEncoding("utf-8"); 24 doPost(req, resp); 25 } 26 @Override 27 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 28 throws ServletException, IOException { 29 //req.setCharacterEncoding("utf-8"); 30 // 获得 flag标记 31 String flag = req.getParameter("flag1"); 32 //System.out.println(flag+" 哦哦哦"); 33 if ("delete".equals(flag)) { 34 // 删除 操作 35 doDelete(req, resp); 36 } else if ("update".equals(flag)) { 37 // 修改操作 38 doUpdate(req, resp); 39 } else if ("doAdd".equals(flag)) { 40 // 增加操作 41 doAdd(req, resp); 42 }else if("doout".equals(flag)){ 43 //退出 44 doout(req,resp); 45 return ; 46 } 47 // 获得所有用户的信息列表 48 String userName = req.getParameter("userName"); 49 List<User> list = us.getAll(userName); 50 // 把取出来的数据放入作用域 51 req.setAttribute("list", list); 52 // 共享数据用转发到userAdmin.jsp 53 req.getRequestDispatcher("userAdmin.jsp").forward(req, resp); 54 } 55 private void doout(HttpServletRequest req, HttpServletResponse resp) { 56 //System.out.println("************************ssss"); 57 //移除指定的session 58 //1.req.getSession().removeAttribute("user1"); 59 //2.让所有的session失效 60 req.getSession().invalidate(); 61 //重定向到登录页面 62 try { 63 resp.sendRedirect("login.jsp"); 64 } catch (IOException e) { 65 // TODO Auto-generated catch block 66 e.printStackTrace(); 67 } 68 } 69 private void doAdd(HttpServletRequest req, HttpServletResponse resp) { 70 String username = req.getParameter("username"); 71 String password = req.getParameter("password"); 72 // 密码加密 73 String newPwd = Base64.encode(password); 74 String sexStr = req.getParameter("sex"); 75 String ageStr = req.getParameter("age"); 76 String mobile = req.getParameter("mobile"); 77 String address = req.getParameter("address"); 78 String authStr = req.getParameter("auth"); 79 int sex = Integer.parseInt(sexStr); 80 int age = Integer.parseInt(ageStr); 81 int auth = Integer.parseInt(authStr); 82 User user = new User(0, username, newPwd, sex, age, mobile, address, 0, 83 1); 84 if (auth == 0) { 85 user.setCommon(2); 86 } else if (auth == 1) { 87 user.setSuperno(1); 88 } 89 boolean isAdd = us.add(user); 90 if (!isAdd) { 91 req.setAttribute("errMess", "添加失败"); 92 } 93 } 94 private void doUpdate(HttpServletRequest req, HttpServletResponse resp) { 95 String idStr = req.getParameter("userId"); 96 String username = req.getParameter("username"); 97 String sexStr = req.getParameter("sex"); 98 String ageStr = req.getParameter("age"); 99 String mobile = req.getParameter("mobile"); 100 String address = req.getParameter("address"); 101 String authStr = req.getParameter("auth"); 102 int id = Integer.parseInt(idStr); 103 // 通过id获得数据库中的记录 104 User user = us.findUserByUserid(id); 105 int sex = Integer.parseInt(sexStr); 106 int age = Integer.parseInt(ageStr); 107 int auth = Integer.parseInt(authStr); 108 // 给属性赋值 109 user.setUsername(username); 110 user.setSex(sex); 111 user.setAge(age); 112 if (auth == 0) { 113 user.setCommon(2); 114 user.setSuperno(0); 115 } else if (auth == 1) { 116 user.setCommon(0); 117 user.setSuperno(1); 118 } 119 user.setMobile(mobile); 120 user.setAddress(address); 121 boolean isUpdate = us.update(user); 122 if (!isUpdate) { 123 req.setAttribute("errMess", "更新失败"); 124 } 125 } 126 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) 127 throws ServletException, IOException { 128 String idStr = req.getParameter("id"); 129 int id = Integer.parseInt(idStr); 130 boolean isDel = us.delete(id); 131 if (!isDel) { 132 req.setAttribute("errMess", "删除失败"); 133 } 134 } 135 }
后台代码就这么多拉,测试过没问题前端代码我就不给了,还是要自己做做的,