文章目录
- 一、项目介绍
- 二、项目展示
- 三、源码展示
- 四、源码获取
一、项目介绍
- 管理员用户:需要能够添加商品类型以及商品,能够对商品进行管理,能够查询用户信息,能够查询出售记录;
- 普通用户:需要能够搜索商品并执行购买商品操作。能够查询购买记录,能够对余额进行充值。
- 注册:能够进行新用户的注册。
功能
1.注册、登录功能。
2.管理员有商品类别管理、商品管理、用户管理、出售记录查询等功能。
3.普通用户有查看购物车、购物卡充值、修改密码、购买商品等功能。
二、项目展示
登录
主页
全部商品类
商品添加类
查询页面
用户列表
三、源码展示
连接数据库
public class JDBCUtils {private static String driver;private static String url;private static String username;private static String password;private static ResourceBundle bundle;static{bundle = ResourceBundle.getBundle("db");driver = bundle.getString("jdbc.driverClass");url = bundle.getString("jdbc.jdbcUrl");username = bundle.getString("jdbc.username");password = bundle.getString("jdbc.password");}/*** ** @return*/public static Connection getConnection() {Connection conn = null;try {Class.forName(driver);conn = DriverManager.getConnection(url, username, password);} catch (Exception e) {e.printStackTrace();}return conn;}public static void release(Connection conn) {if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}public static void release(Connection conn, PreparedStatement pstmt) {if (pstmt != null) {try {pstmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (pstmt != null) {try {pstmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}
登录类
public class Login extends JFrame {private JPanel contentPane;private JTextField txtT;private JPasswordField passwordField;private UserDao userDao = new UserDao();/*** Create the frame.*/public Login() {setResizable(false);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 542, 482);contentPane = new JPanel();contentPane.setBackground(SystemColor.menu);contentPane.setForeground(Color.LIGHT_GRAY);contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);JLabel lblNewLabel_1 = new JLabel("密码");lblNewLabel_1.setFont(new Font("微软雅黑", Font.BOLD, 15));JButton btnNewButton = new JButton("登录");btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 15));txtT = new JTextField();txtT.setFont(new Font("微软雅黑", Font.BOLD, 15));txtT.setColumns(10);JButton btnNewButton_1 = new JButton("注册");btnNewButton_1.setFont(new Font("微软雅黑", Font.BOLD, 15));JButton btnNewButton_2 = new JButton("重置");btnNewButton_2.setFont(new Font("微软雅黑", Font.BOLD, 15));passwordField = new JPasswordField();JLabel lblNewLabel = new JLabel("用户名:");lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 15));GroupLayout gl_contentPane = new GroupLayout(contentPane);gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane.createSequentialGroup().addGap(108).addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false).addGroup(gl_contentPane.createSequentialGroup().addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane.createSequentialGroup().addGap(8).addComponent(lblNewLabel_1)).addGroup(gl_contentPane.createSequentialGroup().addPreferredGap(ComponentPlacement.RELATED).addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE))).addGap(18).addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE).addComponent(txtT, GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE))).addGroup(gl_contentPane.createSequentialGroup().addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE).addGap(33).addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE).addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(btnNewButton_2, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE))).addContainerGap(141, Short.MAX_VALUE)));gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane.createSequentialGroup().addContainerGap(150, Short.MAX_VALUE).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE).addComponent(txtT, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)).addGap(33).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE).addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)).addGap(41).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(btnNewButton).addComponent(btnNewButton_2).addComponent(btnNewButton_1)).addContainerGap(108, Short.MAX_VALUE)));contentPane.setLayout(gl_contentPane);ImageIcon bg=new ImageIcon(Login.class.getResource("/image/login.jpg"));this.setSize(bg.getIconWidth(),bg.getIconHeight());JLabel label=new JLabel(bg); label.setSize(bg.getIconWidth(),bg.getIconHeight());JPanel pan=(JPanel)this.getContentPane();pan.setOpaque(false); this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setLocationRelativeTo(null);/*** 点击方法*/btnNewButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {check();}});btnNewButton_1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {dispose();new FirstLogin().setVisible(true);}});btnNewButton_2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {txtT.setText("");passwordField.setText("");}});passwordField.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if(e.getKeyCode()==10) {check();}}});}/*** 登录检查*/private void check() {String userName = txtT.getText();String password = passwordField.getText();Connection conn = JDBCUtils.getConnection();if(StringUtils.isEmpty(userName, password)) {JOptionPane.showMessageDialog(null, "用户名或密码不能为空");return;}User user = new User(userName,password);UserId userid = null;try {userid = userDao.login(conn,user);//返回权限if(userid!=null) {if(userid.getUserid()==1) {LoginConfig.writeUser(userName,userid.getId().toString(),password,userid.getMoney().toString());JOptionPane.showMessageDialog(null, "欢迎你管理员");dispose();AdminFrm adminfrm = new AdminFrm();adminfrm.setVisible(true);return;}else if(userid.getUserid()==0){LoginConfig.writeUser(userName,userid.getId().toString(),password,userid.getMoney().toString());JOptionPane.showMessageDialog(null, "登录成功");dispose();UserFrm userfrm = new UserFrm();userfrm.setVisible(true);return;}}else {JOptionPane.showMessageDialog(null, "登录失败");return;}} catch (Exception e) {e.printStackTrace();}}
}
登录主页
public class AdminFrm extends JFrame {private JMenu mnNewMenu;private JPanel contentPane;public JDesktopPane desk = new JDesktopPane();//为了实现一次点击只能打开一个窗口,打开变为false,关闭变为truepublic static boolean flagGoodsTypeAdd = true;public static boolean flagIntroduce = true;public static boolean flagGoodsAll = true;public static boolean flagGoodsTypeAll = true;public static boolean flagUserList = true;public static boolean flagUserShopHistory = true;public static boolean flagUpdatePassword = true;public static boolean flagChongMoney = true;/*** Create the frame.*/public AdminFrm() {setTitle("管理员界面");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(893, 813);JMenuBar menuBar = new JMenuBar();setJMenuBar(menuBar);mnNewMenu = new JMenu("");menuBar.add(mnNewMenu);mnNewMenu = new JMenu();mnNewMenu.setIcon(new ImageIcon(UserFrm.class.getResource("/image/user2.jpg")));mnNewMenu.setFont(new Font("Microsoft YaHei UI", Font.BOLD | Font.ITALIC, 15));//mnNewMenu.setBackground(new Color(0, 204, 255));menuBar.add(mnNewMenu);JMenuItem paswordUpd = new JMenuItem("修改密码");paswordUpd.setIcon(new ImageIcon(UserFrm.class.getResource("/image/password.jpg")));paswordUpd.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 15));mnNewMenu.add(paswordUpd);JMenuItem logout = new JMenuItem("退出登录");logout.setIcon(new ImageIcon(UserFrm.class.getResource("/image/logout.jpg")));logout.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 15));mnNewMenu.add(logout);JMenu mnNewMenu_1 = new JMenu("商品维护");//mnNewMenu_1.setBackground(Color.ORANGE);mnNewMenu_1.setFont(new Font("Microsoft YaHei UI", Font.BOLD | Font.ITALIC, 15));menuBar.add(mnNewMenu_1);JMenu menu = new JMenu("商品类别管理");menu.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 15));mnNewMenu_1.add(menu);JMenuItem typeAll = new JMenuItem("商品类别维护");typeAll.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 15));menu.add(typeAll);JMenuItem typeAdd = new JMenuItem("商品类别添加");typeAdd.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 15));menu.add(typeAdd);JMenu menu_1 = new JMenu("商品管理");menu_1.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 15));mnNewMenu_1.add(menu_1);JMenuItem shopAll = new JMenuItem("商品维护");shopAll.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 15));menu_1.add(shopAll);JMenuItem shopAdd = new JMenuItem("商品添加");shopAdd.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 15));menu_1.add(shopAdd);JMenu mnNewMenu_3 = new JMenu("出售情况");mnNewMenu_3.setFont(new Font("Microsoft YaHei UI", Font.BOLD | Font.ITALIC, 15));menuBar.add(mnNewMenu_3);JMenuItem mntmNewMenuItem_1 = new JMenuItem("用户列表");mntmNewMenuItem_1.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 15));mnNewMenu_3.add(mntmNewMenuItem_1);JMenuItem mntmNewMenuItem_2 = new JMenuItem("出售记录");mntmNewMenuItem_2.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 15));mnNewMenu_3.add(mntmNewMenuItem_2);JMenu mnNewMenu_2 = new JMenu("关于我们");mnNewMenu_2.setFont(new Font("Microsoft YaHei UI", Font.BOLD | Font.ITALIC, 15));menuBar.add(mnNewMenu_2);JMenuItem mntmNewMenuItem = new JMenuItem("介绍");mntmNewMenuItem.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 15));mnNewMenu_2.add(mntmNewMenuItem);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(new BorderLayout(0, 0));desk.setBackground(new Color(72, 209, 204));contentPane.add(desk, BorderLayout.CENTER);this.setExtendedState(JFrame.MAXIMIZED_BOTH);/*** 点击生成界面*///介绍界面mntmNewMenuItem.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {Introduce introduce = Introduce.getIntroduce();if (flagIntroduce) {introduce.setVisible(true);desk.add(introduce);flagIntroduce = false;}}});//商品类别管理界面typeAll.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {GoodsTypeAll goodsTypeAll = GoodsTypeAll.getGoodsTypeAll();if (flagGoodsTypeAll) {goodsTypeAll.setVisible(true);goodsTypeAll.fillJComboBox2();desk.add(goodsTypeAll);flagGoodsTypeAll = false;}}});//商品添加界面typeAdd.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {GoodsTypeAdd goodsTypeAdd = GoodsTypeAdd.getGoodsTypeAdd();if (flagGoodsTypeAdd) {goodsTypeAdd.setVisible(true);desk.add(goodsTypeAdd);flagGoodsTypeAdd = false;}}});//商品管理界面shopAll.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {GoodsAll goodsAll = GoodsAll.getGoodsAll();if (flagGoodsAll) {goodsAll.setVisible(true);goodsAll.fillJComboBox2();desk.add(goodsAll);flagGoodsAll = false;}}});//商品添加界面shopAdd.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {GoodsAdd goodsAdd = GoodsAdd.getGoodsAdd();goodsAdd.setVisible(true);goodsAdd.fillJComboBox();desk.add(goodsAdd);}});//用户列表界面mntmNewMenuItem_1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {UserList userlist = UserList.getUserList();if (flagUserList) {userlist.setVisible(true);userlist.fillTable(null);desk.add(userlist);flagUserList = false;}}});//销售记录界面mntmNewMenuItem_2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {UserShopHistory usershophistory = UserShopHistory.getShopHistory();if (flagUserShopHistory) {usershophistory.setVisible(true);usershophistory.fillTable();desk.add(usershophistory);flagUserShopHistory = false;}}});this.fillName();//修改密码界面paswordUpd.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {UpdatePassword updatePassword = UpdatePassword.getUpdatePassword();updatePassword.setVisible(true);desk.add(updatePassword);}});//充值界面mntmNewMenuItem.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {ChongMoney chongMoney = ChongMoney.getChongMoney();if (flagChongMoney) {chongMoney.setMoney();chongMoney.setVisible(true);desk.add(chongMoney);flagChongMoney = false;}}});//退出登录logout.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {JOptionPane.showMessageDialog(null, "退出成功");dispose();new Login().setVisible(true);}});
// logout.addMouseListener(new MouseAdapter() {
// @Override
// public void mouseClicked(MouseEvent mouseEvent) {
// JOptionPane.showMessageDialog(null, "退出成功");
// dispose();
// new Login().setVisible(true);
// }
// });}private void fillName() {ArrayList useList = LoginConfig.getUserList();String userName = useList.get(0).toString();mnNewMenu.setText(userName);}
}
四、源码获取
因为页面与源码太多了,所以页面与源码只展示了一部分,完整源码已经打包了,点击下面蓝色链接获取!
点我获取源码