一、项目介绍
超市账单管理系统主要用于对超市的交易账单进行管理,如账单录入、账单修改、账单
删除,以及和超市商品相关的供应商、用户的管理等。所谓账单,就是超市与供应商进
行交易的凭据。超市采购部的职员、超市的部门经理是该系统的目标用户。
要求:
- 使用 jsp、jstl 以及其他技术整合实现该系统。
- 使用MySQL数据库。
- 必须按照 B/S 结构完成系统。
二、项目功能
三、核心代码
用户servlet
java">@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public UserServlet() {super();// TODO Auto-generated constructor stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoPost(request, response);}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubtry {request.setCharacterEncoding("utf-8");User user = new User();user.setId(Integer.parseInt(request.getParameter("userId")));user.setName(request.getParameter("username"));user.setPassword(request.getParameter("password"));user.setSex(Integer.parseInt(request.getParameter("sex")));user.setAge(Integer.parseInt(request.getParameter("age")));user.setTel(request.getParameter("mobile"));user.setLoc(request.getParameter("address"));user.setRole(Integer.parseInt(request.getParameter("auth")));UserDao userdao = new UserDao();if (userdao.insertUser(user)) {response.sendRedirect("GetUserServlet");}else {response.sendRedirect("userAdd.jsp");}} catch (Exception e) {response.sendRedirect("userAdd.jsp");// TODO: handle exception}}}
数据库工具类
java">public class DBUtil {private static Connection con = null;private static Statement sts = null;public static Connection getDB(String name, String user, String password){try {Class.forName("com.mysql.cj.jdbc.Driver");String url = "jdbc:mysql://localhost:3306/" + name + "?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8";con = DriverManager.getConnection(url, user, password);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (SQLException e) {// TODO: handle exceptione.printStackTrace();}return con;}
}
用户数据库层代码
java">public class UserDao {Connection con;PreparedStatement ps;ResultSet rs;//判断登陆public boolean checkUser(String username,String password) {String sql = "select user_password from user where user_name = '"+username +"'";con = DBUtil.getDB("supermarket", "root", "02200059");try {PreparedStatement ps = con.prepareStatement(sql);rs = ps.executeQuery();if (rs.next()) {if (rs.getString(1).equals(password)) {return true;}}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return false;}//遍历数据库中数据public ArrayList<User> getAllUser(){ArrayList<User> list = new ArrayList<User>();con = DBUtil.getDB("supermarket", "root", "02200059");try {ps = con.prepareStatement("select * from user");rs = ps.executeQuery();while(rs.next()) {User user = new User();user.setId(rs.getInt("user_id"));user.setName(rs.getString("user_name"));user.setPassword(rs.getString("user_password"));user.setSex(rs.getInt("user_sex"));user.setAge(rs.getInt("user_age"));user.setTel(rs.getString("user_tel"));user.setLoc(rs.getString("user_loc"));user.setRole(rs.getInt("user_role"));list.add(user);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return list;}//插入数据public boolean insertUser(User user) {con = DBUtil.getDB("supermarket", "root", "02200059");String sql = "insert into user values(?,?,?,?,?,?,?,?)";try {ps = con.prepareStatement(sql);ps.setInt(1, user.getId());ps.setString(2, user.getName());ps.setString(3, user.getPassword());ps.setInt(4,user.getSex());ps.setInt(5,user.getAge());ps.setString(6, user.getTel());ps.setString(7, user.getLoc());ps.setInt(8, user.getRole());int i = ps.executeUpdate();if(i != 0) {return true;}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return false;}//查询用户名public ArrayList<User> getUser(String username){ArrayList<User> list = new ArrayList<User>();con = DBUtil.getDB("supermarket", "root", "02200059");String sql = "select * from user where user_name = ?";try {ps = con.prepareStatement(sql);ps.setString(1, username);rs = ps.executeQuery();while(rs.next()) {User user = new User();user.setId(rs.getInt("user_id"));user.setName(rs.getString("user_name"));user.setPassword(rs.getString("user_password"));user.setSex(rs.getInt("user_sex"));user.setAge(rs.getInt("user_age"));user.setTel(rs.getString("user_tel"));user.setLoc(rs.getString("user_loc"));user.setRole(rs.getInt("user_role"));list.add(user);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return list;}//查询idpublic ArrayList<User> getUserid(int userid){ArrayList<User> list = new ArrayList<User>();con = DBUtil.getDB("supermarket", "root", "02200059");String sql = "select * from user where user_id = " + userid;try {ps = con.prepareStatement(sql);rs = ps.executeQuery();if(rs.next()) {User user = new User();user.setId(rs.getInt("user_id"));user.setName(rs.getString("user_name"));user.setPassword(rs.getString("user_password"));user.setSex(rs.getInt("user_sex"));user.setAge(rs.getInt("user_age"));user.setTel(rs.getString("user_tel"));user.setLoc(rs.getString("user_loc"));user.setRole(rs.getInt("user_role"));list.add(user);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return list;}//删除public boolean delUser(int userid) {con = DBUtil.getDB("supermarket", "root", "02200059");String sql = "delete from user where user_id = " + userid;try {ps = con.prepareStatement(sql);int i = ps.executeUpdate();if(i != 0) {return true;}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return false;}//修改public boolean alterUser(User user) {con = DBUtil.getDB("supermarket", "root", "02200059");String sql = "update user set user_name = ?, user_password = ?, "+ "user_sex = ?,user_age = ?, user_tel = ?, "+ "user_loc = ? where user_id = ?";try {ps = con.prepareStatement(sql);ps.setString(1, user.getName());ps.setString(2, user.getPassword());ps.setInt(3,user.getSex());ps.setInt(4,user.getAge());ps.setString(5, user.getTel());ps.setString(6, user.getLoc());ps.setInt(7, user.getId());int i = ps.executeUpdate();if(i != 0) {return true;}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return false;}}
四、项目展示
登录页面
主页面
账单列表
用户管理
供货商管理
账号管理
五、源码
因为页面与源码太多了,所以页面与源码只展示了一部分,完整源码已经打包了,点击下面蓝色链接获取!
点我获取源码