1.1总体目标
用Java技术实现一个单机版、图形界面、单用户的电商平台管理,包括2个身份:普通用户和管理员。
要求:对账号和密码进行验证,如果账号或密码不合法(包括空判断),则在当前页面给出错误提示。
1.2具体功能要求
(1)普通用户
普通用户登录成功后,跳转普通用户界面,界面包含个人信息及我的订单选项卡,分别显示对应用户资料和订单信息。个人资料页面显示用户个人信息,默认不可编辑,当点击修改信息按钮时,所有信息进入可编辑状态。
(2)管理员
管理员登录成功后,跳转商品管理界面,界面包含商品分类(树状菜单)、商品信息(文本区)及四个按钮,商品信息文本区默认是不可编辑状态。
“添加”按钮可以添加上商品,点新建按钮弹窗填入商品信息,且包含保存按钮。
“编辑”按钮:可以修改当前选中的商品信息。点编辑按钮,弹窗,弹窗上默认显示需修改信息的商品信息,且包含保存按钮。
“保存”按钮:用户单击此按钮,商品名称和价格不能为空,价格符合相应格式,价格必须为数值格式,要求进行验证,验证通过后才能保存。
“删除”按钮:可以删除当前商品信息,弹出确认提示框,用户再次确认后删除当前商品信息
“退出”按钮:单击”退出”按钮弹出确认提示框,用户再次确认退出本系统。

代码如下:

登陆界面:

package test2;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import test2.Mysql.DBHelper;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;


public class Login extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login frame = new Login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 546, 342);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("    \u8D26\u53F7\uFF1A");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
		lblNewLabel.setBounds(72, 101, 84, 46);
		contentPane.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setFont(new Font("宋体", Font.PLAIN, 16));
		textField.setBounds(182, 108, 213, 33);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("    \u5BC6\u7801\uFF1A");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
		lblNewLabel_1.setBounds(72, 171, 84, 46);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("\u8D26\u6237\u7C7B\u578B\uFF1A");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 16));
		lblNewLabel_2.setBounds(72, 28, 84, 46);
		contentPane.add(lblNewLabel_2);
		
		JButton btnNewButton = new JButton("\u767B\u5F55");
	
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton.setBounds(72, 243, 97, 52);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u53D6\u6D88");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField.setText(null);
				passwordField.setText(null);
			}
		});
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton_1.setBounds(363, 243, 97, 52);
		contentPane.add(btnNewButton_1);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setFont(new Font("宋体", Font.PLAIN, 16));
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"\u666E\u901A\u7528\u6237", "\u7BA1\u7406\u5458"}));
		comboBox.setBounds(182, 35, 213, 33);
		contentPane.add(comboBox);
		
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					String sql2 = "select * from atm1 where binary name ="+"'"+textField.getText()+"'"+"and binary password="+"'"+passwordField.getText()+"'";
					Mysql mysql=new Mysql();
					DBHelper dbHelper = mysql.new DBHelper();
					ResultSet rsResultSet = dbHelper.DB(sql2);//使用数据库判断该账号账号及密码是否对应
					if(rsResultSet.next()==true){
						JOptionPane.showMessageDialog(passwordField, "登录成功!", "提示:",JOptionPane.WARNING_MESSAGE);
						dbHelper.downConn();
						if(comboBox.getSelectedItem().equals("普通用户")){
							User frame = new User(textField.getText());
							frame.setVisible(true);//普通用户界面
							dispose();
		//					System.out.print(1);
						}
						if(comboBox.getSelectedItem().equals("管理员")){
		//					System.out.print(2);
							Controller frame = new Controller();
							frame.setVisible(true);//管理员界面
							dispose();
						}
						

						
						
					}
					else if(textField.getText().length()==0 && passwordField.getText().length()!=0) {
						JOptionPane.showMessageDialog(contentPane, "登录失败!\n用户名不可为空!", "错误提示:",JOptionPane.WARNING_MESSAGE);
						}
						else if(textField.getText().length()!=0 && passwordField.getText().length()==0) {
							JOptionPane.showMessageDialog(contentPane, "登录失败!\n密码不可为空!", "错误提示:",JOptionPane.WARNING_MESSAGE);
							}
						
						else if(textField.getText().length()==0 && passwordField.getText().length()==0){
							JOptionPane.showMessageDialog(contentPane, "登录失败!\n用户名和密码不可为空", "错误提示:",JOptionPane.WARNING_MESSAGE);
						}
						
						else {
							textField.setText(null);
							passwordField.setText(null);
							JOptionPane.showMessageDialog(contentPane, "登录失败!\n用户名不存在或者密码不正确!", "错误提示:",JOptionPane.WARNING_MESSAGE);
						}
			}
			
				catch (Exception e1) {
					
				}
			}
		});
		
		passwordField = new JPasswordField();
		passwordField.setFont(new Font("宋体", Font.PLAIN, 16));
		passwordField.setEchoChar('*');
		passwordField.setBounds(182, 179, 213, 33);
		contentPane.add(passwordField);
	}
}

在这里插入图片描述
个人用户界面:

package test2;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import test2.Mysql.DBHelper;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import java.awt.Choice;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JSpinner;
import javax.swing.JScrollPane;
import java.awt.Font;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;


public class User extends JFrame {//用户界面

	private int b;
	private int a;
	private String phonenum;
	private String adress;
	private String name;
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTable table;
	private ButtonGroup group;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					User frame = new User("");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public User(String name) {
		setResizable(false);
		setTitle("\u4E2A\u4EBA\u4E2D\u5FC3");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 851, 511);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setFont(new Font("宋体", Font.PLAIN, 16));
		tabbedPane.setToolTipText("");
		tabbedPane.setBounds(10, 26, 827, 447);
		contentPane.add(tabbedPane);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("个人资料", null, panel, null);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("        \u8D26\u53F7");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		lblNewLabel.setBounds(75, 45, 163, 44);
		panel.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("     \u8054\u7CFB\u65B9\u5F0F");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 25));
		lblNewLabel_1.setBounds(75, 120, 163, 44);
		panel.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("        \u6027\u522B");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 25));
		lblNewLabel_2.setBounds(75, 195, 163, 44);
		panel.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("     \u9ED8\u8BA4\u5730\u5740");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 25));
		lblNewLabel_3.setBounds(75, 270, 163, 44);
		panel.add(lblNewLabel_3);
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setFont(new Font("宋体", Font.PLAIN, 25));
		textField.setBounds(255, 50, 238, 44);
		panel.add(textField);
		textField.setColumns(10);
		textField.setText(name);
		
		textField_1 = new JTextField();
		textField_1.setFont(new Font("宋体", Font.PLAIN, 25));
		textField_1.setEditable(false);
		textField_1.setColumns(10);
		textField_1.setBounds(255, 125, 238, 44);
		panel.add(textField_1);
		
		
		
		textField_2 = new JTextField();
		textField_2.setFont(new Font("宋体", Font.PLAIN, 25));
		textField_2.setEditable(false);
		textField_2.setColumns(10);
		textField_2.setBounds(248, 270, 466, 44);
		panel.add(textField_2);

		
		JRadioButton rdbtnNewRadioButton = new JRadioButton("\u7537");
		rdbtnNewRadioButton.setEnabled(false);
		rdbtnNewRadioButton.setSelected(true);
		rdbtnNewRadioButton.setFont(new Font("宋体", Font.PLAIN, 25));
		rdbtnNewRadioButton.setBounds(255, 195, 107, 44);
		panel.add(rdbtnNewRadioButton);
		
		JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("\u5973");
		rdbtnNewRadioButton_1.setEnabled(false);
		rdbtnNewRadioButton_1.setFont(new Font("宋体", Font.PLAIN, 25));
		rdbtnNewRadioButton_1.setBounds(386, 195, 107, 44);
		panel.add(rdbtnNewRadioButton_1);
		
		group=new ButtonGroup();
		group.add(rdbtnNewRadioButton);
		group.add(rdbtnNewRadioButton_1);
		
		JButton btnNewButton = new JButton("\u4FEE\u6539\u4FE1\u606F");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				textField_1.setEditable(true);
				textField_2.setEditable(true);
				rdbtnNewRadioButton.setEnabled(true);
				rdbtnNewRadioButton_1.setEnabled(true);
				b=1;//1为可进行信息编辑 0为不可编辑
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton.setBounds(129, 337, 127, 44);
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u4FDD\u5B58");
		
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton_1.setBounds(304, 337, 127, 44);
		panel.add(btnNewButton_1);
		
		JScrollPane scrollPane = new JScrollPane();
		tabbedPane.addTab("我的订单", null, scrollPane, null);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane.setViewportView(scrollPane_1);
		
		try {
			Mysql mysql=new Mysql();
			DBHelper dbHelper =mysql.new DBHelper();
			String sql2 = "select * from atm1 where binary name ="+"'"+name+"'";
			ResultSet rsResultSet = dbHelper.DB(sql2);
			while (rsResultSet.next()) {
				
				adress=rsResultSet.getString("adress");
				phonenum=rsResultSet.getString("phonenum");
				a=rsResultSet.getInt("sex"); //获取性别 0为男,1为女;
				
				textField_1.setText(phonenum);
				textField_2.setText(adress);
				
				if(a==0) {
				rdbtnNewRadioButton.setSelected(true);
				}
				if(a==1) {
					rdbtnNewRadioButton_1.setSelected(true);
					}

			}
			}
			catch(Exception e){
				e.printStackTrace();
			}
		
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					if(b==1) {//判断是否为可修改状态
						if(rdbtnNewRadioButton.isSelected()) {
							a=0;//性别男
						}
						else {
							a=1;//性别女
						}
						
						Mysql mysql=new Mysql();
						DBHelper dbHelper =mysql.new DBHelper();
						String sql = "update atm1 set phonenum=?,adress=?,sex=? where name=?"; //更新个人信息
						PreparedStatement ps = dbHelper.connection.prepareStatement(sql);
						ps.setString(1, textField_1.getText());
				        ps.setString(2, textField_2.getText());
				        ps.setInt(3, a);
				        ps.setString(4, name);//修改字段password数据
				        ps.executeUpdate();
				        JOptionPane.showMessageDialog(contentPane, "保存成功!", "提示:",JOptionPane.WARNING_MESSAGE);

				        dispose();
				        User frame = new User(name);
						frame.setVisible(true);

				        textField_1.setEditable(false);
						textField_2.setEditable(false);
						rdbtnNewRadioButton.setEnabled(false);
						rdbtnNewRadioButton_1.setEnabled(false);
						
						b=0;
						
				        dbHelper.downConn();
					}
					else {
						 JOptionPane.showMessageDialog(contentPane, "此状态不可保存!", "提示:",JOptionPane.WARNING_MESSAGE);
					}
					
				}
				
				catch(Exception e1) {
					
				}
				
				
			}
		});

		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{1, "100000", "2021.6.17", "Java程序设计",name+" "+adress+" "+phonenum},
				{2, "100001", "2021.6.17", "电冰箱",name+" "+adress+" "+phonenum},
			},
			new String[] {
				"\u5E8F\u53F7", "\u7F16\u53F7", "\u4E0B\u5355\u65F6\u95F4", "\u5546\u54C1\u540D\u79F0", "\u6536\u8D27\u4FE1\u606F"
			}
		));
		table.getColumnModel().getColumn(0).setPreferredWidth(61);
		table.getColumnModel().getColumn(3).setPreferredWidth(84);
		table.getColumnModel().getColumn(4).setPreferredWidth(292);
		scrollPane_1.setViewportView(table);
		
	}
}

在这里插入图片描述管理员界面:

package test2;

import java.awt.BorderLayout;
import java.awt.EventQueue;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JMenuBar;
import javax.swing.JComboBox;
import javax.swing.JProgressBar;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JMenu;
import java.awt.TextArea;
import java.awt.Scrollbar;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import test2.Mysql.DBHelper;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Button;
import javax.swing.JTextArea;

public class Controller extends JFrame{//管理员界面

	
	private String xq;
	private String price;
	private JPanel contentPane;
	private JTree tree;
	private String name;
	;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Controller frame = new Controller();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Controller() {
		setResizable(false);
		setTitle("\u7BA1\u7406\u5546\u54C1");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 851, 515);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnNewButton = new JButton("\u6DFB\u52A0");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {//添加
				
				ControllerAdd frame = new ControllerAdd();
				frame.setVisible(true);
				dispose();
				
				
				
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton.setBounds(57, 435, 97, 42);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u7F16\u8F91");
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton_1.setBounds(257, 435, 97, 42);
		contentPane.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("\u5220\u9664");
		btnNewButton_2.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton_2.setBounds(457, 435, 97, 42);
		contentPane.add(btnNewButton_2);
		
		TextArea textArea = new TextArea();
		textArea.setFont(new Font("Dialog", Font.PLAIN, 15));
		textArea.setBounds(426, 10, 411, 218);
		contentPane.add(textArea);
		
	
		
		JButton btnNewButton_3 = new JButton("\u9000\u51FA");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int a=JOptionPane.showConfirmDialog(null, "确定退出吗?", "提示:", JOptionPane.YES_NO_OPTION);
				if(a==0) {
					dispose();
				}
			}
		});
		btnNewButton_3.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton_3.setBounds(657, 435, 97, 42);
		contentPane.add(btnNewButton_3);
		
		
		DefaultMutableTreeNode Root = new DefaultMutableTreeNode("商品分类");//根
		DefaultMutableTreeNode Jiadian = new DefaultMutableTreeNode("家电类");
		DefaultMutableTreeNode Fuzhuang = new DefaultMutableTreeNode("服装类");
		JTree tree_1 = new JTree(Root);
		Root.add(Jiadian);
		Root.add(Fuzhuang);
		
		try {
			Mysql mysql=new Mysql();
			DBHelper dbHelper=mysql.new DBHelper();
			String sql2 = "select * from jd ";
			ResultSet rsResultSet = dbHelper.DB(sql2);
			while (rsResultSet.next()) {
				Jiadian.add(new DefaultMutableTreeNode(rsResultSet.getString("name")));
				}
			String sql_2 = "select * from fz ";
			ResultSet rsResultSet1 = dbHelper.DB(sql_2);
			while (rsResultSet1.next()) {
				Fuzhuang.add(new DefaultMutableTreeNode(rsResultSet1.getString("name")));
				}
		}
		catch(Exception e1) {
			
		}
		
		btnNewButton_2.addActionListener(new ActionListener() {//删除
			public void actionPerformed(ActionEvent e) {
				try {
					
					DefaultMutableTreeNode selectedNode =(DefaultMutableTreeNode) tree_1.getLastSelectedPathComponent();
					
					if (selectedNode != null && selectedNode.getParent() != null) {
						
						String a=selectedNode.toString();
						
						if(selectedNode.getParent().toString().equals("家电类")) {
							Jiadian.remove(selectedNode);
							tree_1.updateUI();
						}
						else {
							Fuzhuang.remove(selectedNode);
							tree_1.updateUI();
						}
	
						Mysql mysql=new Mysql();
						DBHelper dbHelper =mysql.new DBHelper();
						String sql2 = "delete from jd where  name =?";
						PreparedStatement ps=dbHelper.connection.prepareStatement(sql2);
						ps.setString(1, a);
						
						String sql = "delete from fz where name =?";
						PreparedStatement ps1=dbHelper.connection.prepareStatement(sql);
						ps1.setString(1, a);
						
						ps.executeUpdate();
						ps1.executeUpdate();
						
						dbHelper.downConn();
						
						
						textArea.setText(null);
//						dispose();
//						Controller frame = new Controller("");
//						frame.setVisible(true);
//						Root.remove(selectedNode);
//					    tree_1.updateUI();//更新
					}
					}
				catch(Exception e1) {
					
				}
				

				
			}
		
			
		});

		
		tree_1.setShowsRootHandles(true);
		tree_1.setFont(new Font("宋体", Font.PLAIN, 14));
		tree_1.setBounds(10, 10, 410, 395);
		contentPane.add(tree_1);
		
		
		tree_1.addMouseListener(new MouseAdapter(){//鼠标监听
			public void mouseClicked(MouseEvent e){
//					if(e.getClickCount()==1) {
						try {
							DefaultMutableTreeNode selectedNode =(DefaultMutableTreeNode) tree_1.getLastSelectedPathComponent();
							Mysql mysql=new Mysql();
							DBHelper dbHelper =mysql.new DBHelper();
							
							String sql = "select  * from jd where name="+"'"+selectedNode+"'"; 
							String sql1 = "select * from fz where name="+"'"+selectedNode+"'";
							
							ResultSet rsResultSet = dbHelper.DB(sql);
							while (rsResultSet.next()) {
								xq=rsResultSet.getString("xq");//获取该商品的商品详情
								price=rsResultSet.getString("price");//获取该商品的商品详情
							}

							ResultSet rsResultSet1 = dbHelper.DB(sql1);
							while (rsResultSet1.next()) {
								xq=rsResultSet1.getString("xq");//获取该商品的商品详情
								price=rsResultSet1.getString("price");//获取该商品的商品详情
							}
							
										
							String name=selectedNode.toString();//商品名
							String fenlie=selectedNode.getParent().toString();//商品分类
							
							if((!selectedNode.toString().equals("家电类")) && (!selectedNode.toString().equals("服装类"))) {
							textArea.setText("商品名:"+name+"\n"+"商品价格:"+Double.parseDouble(price)+"\n"+"商品分类:"+fenlie+"\n"+"商品详情:"+xq);
							}
						}
						catch(Exception e1) {
							
//						}
					}
		        }
				});
		btnNewButton_1.addActionListener(new ActionListener() {//编辑
			public void actionPerformed(ActionEvent e) {
				
				
				DefaultMutableTreeNode selectedNode =(DefaultMutableTreeNode) tree_1.getLastSelectedPathComponent();
				
				ControllerEdit frame = new ControllerEdit(selectedNode.toString(),price,selectedNode.getParent().toString(),xq);
				frame.setVisible(true);
				dispose();
				
			}
		});
	
	}
}

在这里插入图片描述管理员界面添加商品功能:

package test2;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import test2.Mysql.DBHelper;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;

public class ControllerAdd extends JFrame {//管理员添加商品窗口

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ControllerAdd frame = new ControllerAdd();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ControllerAdd() {
		setTitle("\u6DFB\u52A0\u5546\u54C1");
		setResizable(false);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 800, 522);
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u5546\u54C1\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, new Font("宋体",Font.PLAIN,20), null));
		panel.setBounds(10, 10, 766, 465);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("\u5546\u54C1\u540D\u79F0");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel.setBounds(64, 60, 110, 33);
		panel.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("\u5546\u54C1\u4EF7\u683C");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel_1.setBounds(64, 120, 110, 33);
		panel.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("\u5546\u54C1\u5206\u7C7B\r\n");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel_2.setBounds(64, 180, 110, 33);
		panel.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("\u5546\u54C1\u8BE6\u60C5");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel_3.setBounds(64, 240, 110, 33);
		panel.add(lblNewLabel_3);
		
		textField = new JTextField();
		textField.setFont(new Font("宋体", Font.PLAIN, 18));
		textField.setBounds(186, 60, 510, 33);
		panel.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setFont(new Font("宋体", Font.PLAIN, 18));
		textField_1.setColumns(10);
		textField_1.setBounds(186, 120, 510, 33);
		panel.add(textField_1);
		
		textField_2 = new JTextField();
		textField_2.setFont(new Font("宋体", Font.PLAIN, 18));
		textField_2.setColumns(10);
		textField_2.setBounds(186, 180, 510, 33);
		panel.add(textField_2);
		
		JTextArea textArea = new JTextArea();
		textArea.setFont(new Font("Monospaced", Font.PLAIN, 18));
		textArea.setBounds(186, 241, 510, 183);
		panel.add(textArea);
		
		JButton btnNewButton = new JButton("\u4FDD\u5B58");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if(textField_2.getText().equals("家电类")) {//获取商品是否为家电类;
					try {
						Mysql mysql=new Mysql();
						DBHelper dbHelper=mysql.new DBHelper();
						String sql2 = "insert into jd (name,xq,price) values(?,?,?)";
						PreparedStatement ps = dbHelper.connection.prepareStatement(sql2);
						ps.setString(1, textField.getText());
				        ps.setString(2, textArea.getText());//向数据库中添加数据
				        ps.setString(3, textField_1.getText());
				        ps.executeUpdate();
				        
				        Controller frame = new Controller();
						frame.setVisible(true);
						
				        dbHelper.downConn();
				        
				        dispose();
					}
					
					catch(Exception e1) {
						
					}
					
				} 
				if(textField_2.getText().equals("服装类")) {//获取商品是否为服装类;
					try {
						Mysql mysql=new Mysql();
						DBHelper dbHelper=mysql.new DBHelper();
						String sql2 = "insert into fz (name,xq,price) values(?,?,?)";
						PreparedStatement ps = dbHelper.connection.prepareStatement(sql2);
						ps.setString(1, textField.getText());
				        ps.setString(2, textArea.getText());//向数据库中添加数据
				        ps.setString(3, textField_1.getText());
				        
				        ps.executeUpdate();
				        
				        Controller frame = new Controller();
						frame.setVisible(true);
						
						dispose();
						
				        dbHelper.downConn();
					}
					catch(Exception e1) {
						
					}
				} 
				
				
				
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton.setBounds(32, 343, 97, 43);
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u53D6\u6D88");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textArea.setText(null);
				textField.setText(null);
				textField_1.setText(null);
				textField_2.setText(null);
			}
		});
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton_1.setBounds(32, 401, 97, 43);
		panel.add(btnNewButton_1);
		
	}

}

在这里插入图片描述

管理员界面编辑商品信息功能:

package test2;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import test2.Mysql.DBHelper;

import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;

public class ControllerEdit extends JFrame {
	
	
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ControllerEdit frame = new ControllerEdit("","","","");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ControllerEdit(String goods,String price,String fenlie,String xq) {
		setResizable(false);
		setTitle("\u7F16\u8F91\u4FE1\u606F");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 796, 517);
		getContentPane().setLayout(null);
		
		JPanel contentPane = new JPanel();
		contentPane.setLayout(null);
		contentPane.setBounds(0, 0, 796, 494);
		getContentPane().add(contentPane);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(null, "\u5546\u54C1\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, new Font("宋体",Font.PLAIN,20), null));
		panel.setBounds(10, 10, 766, 465);
		contentPane.add(panel);
		
		JLabel lblNewLabel = new JLabel("\u5546\u54C1\u540D\u79F0");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel.setBounds(64, 60, 110, 33);
		panel.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("\u5546\u54C1\u4EF7\u683C");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel_1.setBounds(64, 120, 110, 33);
		panel.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("\u5546\u54C1\u5206\u7C7B\r\n");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel_2.setBounds(64, 180, 110, 33);
		panel.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("\u5546\u54C1\u8BE6\u60C5");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel_3.setBounds(64, 240, 110, 33);
		panel.add(lblNewLabel_3);
		
		textField = new JTextField();
		textField.setFont(new Font("宋体", Font.PLAIN, 18));
		textField.setColumns(10);
		textField.setBounds(186, 60, 510, 33);
		panel.add(textField);
		textField.setText(goods);
		
		textField_1 = new JTextField();
		textField_1.setFont(new Font("宋体", Font.PLAIN, 18));
		textField_1.setColumns(10);
		textField_1.setBounds(186, 120, 510, 33);
		panel.add(textField_1);
		textField_1.setText(price);
		
		textField_2 = new JTextField();
		textField_2.setFont(new Font("宋体", Font.PLAIN, 18));
		textField_2.setColumns(10);
		textField_2.setBounds(186, 180, 510, 33);
		panel.add(textField_2);
		textField_2.setText(fenlie);
		
		JTextArea textArea = new JTextArea();
		textArea.setFont(new Font("Monospaced", Font.PLAIN, 18));
		textArea.setBounds(184, 246, 510, 183);
		panel.add(textArea);
		textArea.setText(xq);
		
		JButton btnNewButton = new JButton("\u4FDD\u5B58");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Mysql mysql =new Mysql();
					DBHelper db=mysql.new DBHelper();
					if(fenlie.equals(textField_2.getText())) {//不改变商品分类
						String sql1 = "update jd set name=?,xq=?,price=? where name=?"; //相等就更新家电类数据
						String sql2= "update fz set name=?,xq=?,price=? where name=?";//相等就更新服装类数据
			            PreparedStatement ps1 = db.connection.prepareStatement(sql1);
			            ps1.setString(1, textField.getText());
			            ps1.setString(2, textArea.getText());
			            ps1.setString(3, textField_1.getText());
			            ps1.setString(4, goods);
			            ps1.executeUpdate();
			            
			            PreparedStatement ps2 = db.connection.prepareStatement(sql2);
			            ps2.setString(1, textField.getText());
			            ps2.setString(2, textArea.getText());
			            ps2.setString(3, textField_1.getText());
			            ps2.setString(4, goods);
			            
			            ps2.executeUpdate();

			            db.downConn();
			            
			            dispose();
						Controller frame = new Controller();
						frame.setVisible(true);
					}
					else {//改变商品分类
						if(textField_2.getText().equals("家电类") ){//判断是否改为家电类
							String sql1="delete from fz where  name =?";//删除服装类的这条数据
							PreparedStatement ps1=db.connection.prepareStatement(sql1);
							ps1.setString(1, goods);
							ps1.executeUpdate();
							
							String sql_1 = "insert into jd (name,xq,price) values(?,?,?)";//往家电类添加这条数据
							PreparedStatement ps_1 = db.connection.prepareStatement(sql_1);
							ps_1.setString(1, textField.getText());
							ps_1.setString(2, textArea.getText());
							ps_1.setString(3, textField_1.getText());
							System.out.print(1);
							ps_1.executeUpdate();
							dispose();
							Controller frame = new Controller();
							frame.setVisible(true);
							
						}
						if(textField_2.getText().equals("服装类") ){//判断是否改为服装类
							String sql1="delete from jd where  name =?";//删除家电类的这条数据
							PreparedStatement ps1=db.connection.prepareStatement(sql1);
							ps1.setString(1, goods);
							ps1.executeUpdate();
							
							String sql_1 = "insert into fz (name,xq,price) values(?,?,?)";//往服装类添加这条数据
							PreparedStatement ps_1 = db.connection.prepareStatement(sql_1);
							ps_1.setString(1, textField.getText());
							ps_1.setString(2, textArea.getText());
							ps_1.setString(3, textField_1.getText());
							ps_1.executeUpdate();
							
							dispose();
							
							Controller frame = new Controller();
							frame.setVisible(true);
							
						}
						if((!textField_2.getText().equals("家电类")) && (!textField_2.getText().equals("服装类"))){
							JOptionPane.showMessageDialog(contentPane, "无该商品分类!", "错误提示:",JOptionPane.WARNING_MESSAGE);
						}
					}
					
				}
				catch(Exception e1) {
					
				}
				
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton.setBounds(32, 343, 97, 43);
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u53D6\u6D88");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				Controller frame = new Controller();
				frame.setVisible(true);
			}
		});
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton_1.setBounds(32, 401, 97, 43);
		panel.add(btnNewButton_1);
		

	}

}

在这里插入图片描述连接数据库:

package test2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Mysql {
	class DBHelper{
		public Connection connection;
		public PreparedStatement preparedStatement;
		
		public DBHelper() throws SQLException, ClassNotFoundException{
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1","root","010723");
		}
		
		public ResultSet DB(String sql) throws ClassNotFoundException, SQLException{
			preparedStatement = connection.prepareStatement(sql);
		    ResultSet resultSet = preparedStatement.executeQuery();
		    return resultSet;
		}
		
		public void excuteSql(String sql) throws SQLException{
			preparedStatement = connection.prepareStatement(sql);
		    preparedStatement.executeUpdate();
		}
		
		public void downConn() throws SQLException{
			connection.close();
		}
	}
}

这是老师布置的期末考试的一个项目(大一),如果发现有错误的地方还请大佬们指出!非常感谢!

Logo

本社区面向用户介绍CSDN开发云部门内部产品使用和产品迭代功能,产品功能迭代和产品建议更透明和便捷

更多推荐