赞
踩
##需要的工具
计算机软件:JDK1.6以上Java开发包
IntelliJIDEA开发工具Mysql数据库mysql服务管理器
Navicat数据库管理和开发工具
#模块设计
1、登录:登录分为员工登录和用户登录,通过正确的编号和密码登录系统后,可进入系统进行相关操作。
2、员工管理模块:登录员工可对商品进行管理,可展示所有员工的信息,可查询员工信息,增加新员工,删除员工信息。
3、商品信息管理模块:主要实现对商品进存销信息的管理。有商品价格,商品编号,库存总数,该模块实现了商品的查询、添加、修改、删除、导出数据和用模块导入数据功能。
4、商品库存信息管理模块:主要管理商品的进存销数量。该模块实现了查询、添加、修改、删除功能。
数据库表如下(员工表,商品表,用户表)
数据库E-R图如下
运行结果如下:
进行项目时,先部署
导入架包
EmployeeDaoImpl代码如下:
package com.csi.dao.impl; import com.csi.dao.EmployeeDao; import com.csi.domain.Employee; import com.csi.utils.JDBCUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class EmployeeDaoImpl extends BaseDao implements EmployeeDao { /** * 登录方法 * @param * @param password * @return * @throws SQLException */ @Override public Employee login(String empNo, String password) throws SQLException { //获取到链接 JDBCUtils jdbcUtils = new JDBCUtils() ; Connection connection = jdbcUtils.getConnection(); //通过connection创建PreparedStatement对象,实现SQL语句的执行 PreparedStatement ps = connection.prepareStatement("SELECT * FROM emp WHERE empNo=? AND password=?") ; ps.setString(1,empNo); ps.setString(2,password); //得到ResultSet结果集 ResultSet rs = ps.executeQuery(); Employee employee = null ; if(rs.next()) { employee = new Employee() ; employee.setEmpNO(empNo); employee.setSal(rs.getDouble("sal")); employee.setHiredate(rs.getDate("hiredate")); employee.setEmpName(rs.getString("empName")); employee.setEmpGender(rs.getString("empGender")); employee.setTel(rs.getString("tel")); } //释放数据库链接资源 jdbcUtils.release(rs,ps,connection); return employee; } @Override public int save(Employee employee) throws SQLException { String sql = "INSERT INTO emp(empNo,empName,empGender,tel,hiredate,sal,password)value(?,?,?,?,?,?,?)" ; Object[] params = {employee.getEmpNO(),employee.getEmpName(),employee.getEmpGender(),employee.getTel(),employee.getHiredate(),employee.getSal(),employee.getPassword()} ; return this.update(sql,params) ; } @Override public int deleteById(String empNo) throws SQLException { return this.update("DELETE FROM emp WHERE empNo = ?",new Object[] {empNo}) ; } @Override public int modify(Employee employee) throws SQLException { String sql = "UPDATE emp SET empName=?,empGender=?,tel=?,hiredate=?,sal=?,password=? WHERE empNo=?" ; Object[] params = {employee.getEmpName(),employee.getEmpGender(),employee.getTel(),employee.getHiredate(),employee.getSal(),employee.getPassword(),employee.getEmpNO()} ; return this.update(sql,params) ; } @Override public List<Employee> list() throws SQLException { //获取到链接 JDBCUtils jdbcUtils = new JDBCUtils() ; Connection connection = jdbcUtils.getConnection(); //通过connection创建PreparedStatement对象,实现SQL语句的执行 PreparedStatement ps = connection.prepareStatement("SELECT * FROM emp") ; //得到ResultSet结果集 ResultSet rs = ps.executeQuery(); List<Employee> employees = new ArrayList<>() ; while(rs.next()) { Employee employee = new Employee() ; employee.setEmpNO(rs.getString("empNo")); employee.setSal(rs.getDouble("sal")); employee.setHiredate(rs.getDate("hiredate")); employee.setEmpName(rs.getString("empName")); employee.setEmpGender(rs.getString("empGender")); employee.setTel(rs.getString("tel")); employees.add(employee) ; } //释放数据库链接资源 jdbcUtils.release(rs,ps,connection); return employees; } @Override public Employee findById(String empNo) throws SQLException { //获取到链接 JDBCUtils jdbcUtils = new JDBCUtils() ; Connection connection = jdbcUtils.getConnection(); //通过connection创建PreparedStatement对象,实现SQL语句的执行 PreparedStatement ps = connection.prepareStatement("SELECT * FROM emp WHERE empno=?") ; ps.setString(1,empNo); //得到ResultSet结果集 ResultSet rs = ps.executeQuery(); Employee employee = null ; if(rs.next()) { employee =new Employee() ; employee.setEmpNO(empNo); employee.setSal(rs.getDouble("sal")); employee.setHiredate(rs.getDate("hiredate")); employee.setEmpName(rs.getString("empName")); employee.setEmpGender(rs.getString("empGender")); employee.setTel(rs.getString("tel")); employee.setPassword(rs.getString("password")); } //释放数据库链接资源 jdbcUtils.release(rs,ps,connection); return employee; } }
main函数代码:
package com.csi; import com.csi.domain.Employee; import com.csi.domain.Goods; import com.csi.domain.User; import com.csi.service.impl.EmployeeServiceImpl; import com.csi.service.impl.GoodsServiceImpl; import com.csi.service.impl.UserServiceImpl; import com.csi.utils.StringDateFormat; import java.util.Collection; import java.util.Scanner; public class Main1 { public static void main(String[] args) { System.out.println("======================"); System.out.println("1. 员工登录"); System.out.println("2. 用户登录"); System.out.println("3. 退出"); System.out.println("======================"); Scanner input = new Scanner(System.in); System.out.print("请选择:"); int choice = input.nextInt(); switch (choice) { case 1: login(); break; case 2: login1(); break; case 3: System.out.println("bye~"); System.exit(-1); break; } } private static void login1() { Scanner input = new Scanner(System.in); //步骤1:让用户输入员工编号及密码信息 System.out.print("请输入用户编号:"); String userNO = input.next(); System.out.print("请输入电话:"); String tel = input.next(); UserServiceImpl userService = new UserServiceImpl(); User user = userService.login(userNO, tel); if (user != null) { System.out.println("登录成功!"); } if (user != null) { System.out.println("您好," + user.getUserName()); System.out.println("================="); System.out.println("1.用户管理"); System.out.println("2.商品管理"); System.out.println("================="); int choice = input.nextInt(); switch (choice) { case 1: userC(); break; case 2: goodsC(); break; } if (choice == 1) { String con; do { System.out.println("您好,以下进行用户管理:"); System.out.println("3.用户新增"); System.out.println("4.查询全部用户信息"); System.out.println("5.根据编号查询用户信息"); System.out.println("6.删除用户信息"); System.out.println("7.更新用户信息"); System.out.println("================="); System.out.println("请选择:"); choice = input.nextInt(); switch (choice) { case 3: save_emp(); break; case 4: list_emp(); break; case 5: findById_emp(); break; case 6: remove_emp(); break; case 7: modify_emp(); break; default: System.out.println("您选择的有误"); } System.out.println("继续输入y,输入其它退出"); System.out.println("请输入:"); con = input.next(); } while (con.equals("y")); } else { String con; do { System.out.println("您好,以下是商品管理"); System.out.println("================="); System.out.println("3.商品新增"); System.out.println("4.查询全部商品信息"); System.out.println("5.根据编号查询商品信息"); System.out.println("6.删除商品信息"); System.out.println("7.更新商品信息"); System.out.println("================="); System.out.println("请选择:"); choice = input.nextInt(); switch (choice) { case 3: save1(); break; case 4: list1(); break; case 5: findById1(); break; case 6: remove1(); break; case 7: modify1(); break; default: System.out.println("您的选择有误!"); } System.out.println("继续输入y,输入其它退出"); System.out.println("请输入:"); con = input.next(); } while (con.equals("y")); } } else { System.out.println("登录失败!"); } } private static void userC() { } //private static void goodsC() { //} /** * 登录方法 */ private static void login() { Scanner input = new Scanner(System.in) ; //步骤1:让用户输入员工编号及密码信息 System.out.print("请输入员工编号:"); String empNO = input.next() ; System.out.print("请输入密码:"); String password = input.next() ; //步骤2:调用Service的登录方法进行判断 EmployeeServiceImpl employeeService = new EmployeeServiceImpl() ; Employee employee = employeeService.login(empNO, password); //步骤3:处理返回结果 if (employee !=null) { System.out.println("登录成功!"); } if (employee != null) { System.out.println("您好," + employee.getEmpName()); System.out.println("================="); System.out.println("1.员工管理"); System.out.println("2.商品管理"); System.out.println("================="); int choice = input.nextInt(); switch (choice) { case 1: employeeC(); break; case 2: goodsC(); break; } if (choice == 1) { String con; do{ System.out.println("您好,以下进行员工管理:"); System.out.println("3.员工新增"); System.out.println("4.查询全部员工信息"); System.out.println("5.根据编号查询员工信息"); System.out.println("6.删除员工信息"); System.out.println("7.更新员工信息"); System.out.println("================="); System.out.println("请选择:"); choice = input.nextInt(); switch (choice) { case 3: save_emp(); break; case 4: list_emp(); break; case 5: findById_emp(); break; case 6: remove_emp(); break; case 7: modify_emp(); break; default: System.out.println("您选择的有误"); } System.out.println("继续输入y,输入其它退出"); System.out.println("请输入:"); con=input.next(); } while (con.equals("y"));} else { String con; do { System.out.println("您好,以下是商品管理"); System.out.println("================="); System.out.println("3.商品新增"); System.out.println("4.查询全部商品信息"); System.out.println("5.根据编号查询商品信息"); System.out.println("6.删除商品信息"); System.out.println("7.更新商品信息"); System.out.println("================="); System.out.println("请选择:"); choice = input.nextInt(); switch (choice) { case 3: save(); break; case 4: list(); break; case 5: findById(); break; case 6: remove(); break; case 7: modify(); break; default: System.out.println("您的选择有误!"); } System.out.println("继续输入y,输入其它退出"); System.out.println("请输入:"); con=input.next(); }while (con.equals("y"));} } else { System.out.println("登录失败!"); } } private static void employeeC() { } private static void goodsC() { } //商品管理 private static void save() { Scanner input = new Scanner(System.in); //接收信息 System.out.print("请输入商品编号:"); String goodsNo = input.next(); System.out.print("请输入商品名称:"); String goodaName = input.next(); //System.out.print("请输入商品单价:"); // String price = input.next(); System.out.print("请输入商品总数:"); int sum = Integer.parseInt(input.next()); //System.out.print("请输入员工入职日期:"); //String s = input.next(); System.out.print("请输入商品单价:"); double price = input.nextDouble(); //对象的封装 Goods goods = new Goods(); goods.setGoodsNo(goodsNo); goods.setGoodsName(goodaName); goods.setPrice(price); goods.setSum(sum); //employee.setPassword("111111"); //employee.setHiredate(StringDateFormat.str2Date(s)); //employee.setSal(sal); //将数据存储在map集合中 GoodsServiceImpl goodsService = new GoodsServiceImpl(); goodsService.save(goods); System.out.println("新增成功!"); } private static void list() { //步骤1.调用service的list方法 GoodsServiceImpl goodsService = new GoodsServiceImpl(); Collection<Goods> list = goodsService.list(); //步骤2.遍历集合 System.out.println("商品编号\t商品名称\t商品价格(单位:kg)\t商品数量"); for (Goods goods : list) { System.out.println(goods.getGoodsNo() + "\t" + goods.getGoodsName() + "\t\t" + goods.getPrice() + "\t\t" + goods.getSum()); } } private static void findById() { Scanner input = new Scanner(System.in); System.out.println("请输入要查询的商品编号:"); String goodsNo = input.next(); //调用service的findById方法 GoodsServiceImpl goodsService = new GoodsServiceImpl(); Goods goods = goodsService.findById(goodsNo); if (goods != null) { System.out.println("商品编号\t商品名称\t商品价格(单位:kg)\t商品数量"); System.out.println(goods.getGoodsNo()+"\t" + goods.getGoodsName() +"\t\t" +goods.getPrice() +"\t\t" + goods.getSum()); } else { System.out.println("您所查询的商品编号不存在!"); } } private static void remove() { System.out.println("请输入要删除的商品编号"); Scanner input = new Scanner(System.in); String goodsNo = input.next(); System.out.print("您确认要删除" + goodsNo + "编号的商品信息吗?(y/n)"); String confirm = input.next(); if ("y".equals(confirm)) { // if (empNo.equals("D100001")){ // System.out.println("D100001是管理员,不能够被删除!"); } GoodsServiceImpl goodsService = new GoodsServiceImpl(); goodsService.deleteById(goodsNo); System.out.println("商品信息删除成功!"); } private static void modify() { System.out.print("请输入要更新的商品编号:"); Scanner input = new Scanner(System.in); String goodsNo = input.next(); //步骤1.调用findById的方法获取到要修改的对象 GoodsServiceImpl goodsService = new GoodsServiceImpl(); Goods goods = goodsService.findById(goodsNo); //步骤2.设置商品新的更新信息 System.out.print("请输入商品名称:"); String goodsName = input.next(); System.out.print("请输入商品价格:"); double goodsPrice = input.nextDouble(); System.out.print("请输入商品数量:"); int goodsSum = input.nextInt(); goods.setGoodsName(goodsName); goods.setPrice(goodsPrice); goods.setSum(goodsSum); //步骤3.将信息还原到数据库 goodsService.modify(goods); System.out.println("数据更新成功!"); list(); } //员工管理 private static void modify_emp() { System.out.print("请输入要更新的员工编号:"); Scanner input = new Scanner(System.in) ; String empNO = input.next() ; //步骤1:调用findById的方法获取到要修改的对象 EmployeeServiceImpl employeeService = new EmployeeServiceImpl() ; Employee employee = employeeService.findById(empNO); //步骤1-1:将要修改的用户信息展现出来 System.out.println(employee); //步骤2:设置员工新的更新信息 System.out.print("请输入员工的姓名:"); String empName = input.next() ; System.out.print("请输入员工的性别:"); String empGender = input.next() ; System.out.print("请输入员工的电话号码:"); String tel = input.next() ; System.out.print("请输入员工的入职日期:"); String s = input.next() ; System.out.print("请输入员工的薪水:"); double sal = input.nextDouble() ; employee.setSal(sal); employee.setHiredate(StringDateFormat.str2Date(s)); employee.setTel(tel); employee.setEmpGender(empGender); employee.setEmpName(empName); //步骤3:将信息还原到数据库 employeeService.modify(employee); System.out.println("数据更新成功!"); list(); } private static void remove_emp() { System.out.print("请输入要删除的员工编号:"); Scanner input = new Scanner(System.in) ; String empNO = input.next(); System.out.print("您确认要删除" + empNO + "编号的员工信息么?(y/n)"); String confirm = input.next() ; if("y".equals(confirm)) { if(empNO.equals("E10001")) { System.out.println("E10001是管理员,不能够被删除!"); return ; } EmployeeServiceImpl employeeService = new EmployeeServiceImpl() ; employeeService.deleteById(empNO); System.out.println("员工信息删除成功!"); } } private static void findById_emp() { Scanner input = new Scanner(System.in) ; System.out.print("请输入要查询的员工编号:"); String empNO = input.next() ; //调用service的findById方法 EmployeeServiceImpl employeeService = new EmployeeServiceImpl() ; Employee employee = employeeService.findById(empNO); if(employee != null) { System.out.println("员工编号\t\t员工姓名\t\t员工入职日期\t\t员工性别\t\t员工电话"); System.out.println(employee.getEmpNO() + "\t\t" + employee.getEmpName() + "\t\t" + StringDateFormat.date2Str(employee.getHiredate()) + "\t\t" + employee.getEmpGender() + "\t\t" + employee.getTel()); }else{ System.out.println("您所查询的员工编号不存在!"); } } private static void list_emp() { //步骤1:调用service的list方法 EmployeeServiceImpl employeeService = new EmployeeServiceImpl() ; Collection<Employee> list = employeeService.list(); //步骤2:遍历集合 System.out.println("员工编号\t\t员工姓名\t\t员工入职日期\t\t员工性别\t\t员工电话"); for (Employee employee : list) { System.out.println(employee.getEmpNO() + "\t\t" + employee.getEmpName() + "\t\t" + StringDateFormat.date2Str(employee.getHiredate()) + "\t\t" + "\t\t" + employee.getEmpGender() + "\t\t" + employee.getTel()); } } private static void save_emp() { Scanner input = new Scanner(System.in) ; //接收信息 System.out.print("请输入员工的编号:"); String empNO = input.next() ; System.out.print("请输入员工的姓名:"); String empName = input.next() ; System.out.print("请输入员工的性别:"); String empGender = input.next() ; System.out.print("请输入员工的电话号码:"); String tel = input.next() ; System.out.print("请输入员工的入职日期:"); String s = input.next() ; System.out.print("请输入员工的薪水:"); double sal = input.nextDouble() ; System.out.println("请输入员工的密码:"); String password = input.next() ; //对象的封装 Employee employee = new Employee() ; employee.setSal(sal); employee.setHiredate(StringDateFormat.str2Date(s)); employee.setPassword("000000"); employee.setTel(tel); employee.setEmpGender(empGender); employee.setEmpName(empName); employee.setEmpNO(empNO); employee.setPassword(password); //将数据存储在map集合中 EmployeeServiceImpl employeeService = new EmployeeServiceImpl() ; employeeService.save(employee); System.out.println("新增成功!"); } //用户管理 private static void save1() { Scanner input = new Scanner(System.in); //接收信息 System.out.print("请输入用户编号:"); String userNo = input.next(); System.out.print("请输入用户名字:"); String userName = input.next(); System.out.print("请输入积分总数:"); int points = Integer.parseInt(input.next()); //对象的封装 User user = new User(); user.setUserNo(userNo); user.setUserName(userName); //user.setTel(tel); user.setPoints(points); //将数据存储在map集合中 UserServiceImpl userService = new UserServiceImpl(); userService.save(user); System.out.println("新增成功!"); } private static void list1() { //步骤1.调用service的list方法 UserServiceImpl userService = new UserServiceImpl(); Collection<User> list = userService.list(); //步骤2.遍历集合 System.out.println("用户编号\t用户名称\t商品价格(单位:kg)\t商品数量"); for (User user : list) { System.out.println(user.getUserNo() + "\t" + user.getUserName() + "\t\t" + user.getPoints() ); } } private static void findById1() { Scanner input = new Scanner(System.in); System.out.println("请输入要查询的商品编号:"); String userNo = input.next(); //调用service的findById方法 UserServiceImpl userService = new UserServiceImpl(); User user = userService.findById(userNo); if (user != null) { System.out.println("商品编号\t商品名称\t商品价格(单位:kg)\t商品数量"); System.out.println(user.getUserNo()+"\t" + user.getUserName() +"\t\t" +user.getPoints() ); } else { System.out.println("您所查询的商品编号不存在!"); } } private static void remove1() { System.out.println("请输入要删除的商品编号"); Scanner input = new Scanner(System.in); String userNo = input.next(); System.out.print("您确认要删除" + userNo + "编号的商品信息吗?(y/n)"); String confirm = input.next(); if ("y".equals(confirm)) { // if (empNo.equals("D100001")){ // System.out.println("D100001是管理员,不能够被删除!"); } UserServiceImpl userService = new UserServiceImpl(); userService.deleteById(userNo); System.out.println("商品信息删除成功!"); } private static void modify1() { System.out.print("请输入要更新的用户编号:"); Scanner input = new Scanner(System.in); String userNo = input.next(); //步骤1.调用findById的方法获取到要修改的对象 UserServiceImpl userService = new UserServiceImpl(); User user = userService.findById(userNo); //步骤2.设置商品新的更新信息 System.out.print("请输入用户姓名:"); String userName = input.next(); // System.out.print("请输入用户积分:"); //String Point = input.next(); System.out.print("请输入用户积分:"); int Points = input.nextInt(); user.setUserName(userName); user.setUserNo(userNo); //User.setPoints(points); //步骤3.将信息还原到数据库 userService.modify(user); System.out.println("数据更新成功!"); list(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。