基于java+mysql的swing+mysql电子商城(java+swing+mysql+gui)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
管理员管理商品、用户购买商品等
eclipse运行:
idea运行:
前台用户:
后台管理员:
技术框架
Java Swing GUI
基于java+mysql的Swing+MySQL电子商城(java+swing+mysql+gui)
private final JTextField field1 = new JTextField(10);
private final JTextField field2 = new JTextField(10);
private final JLabel label = new JLabel();
private final JLabel label0 = new JLabel();
private final JLabel label1 = new JLabel();
private final JLabel label2 = new JLabel();
private final JLabel label3 = new JLabel();
private final JLabel label4 = new JLabel();
private final JPasswordField password1 = new JPasswordField();//密码文本框
private final JButton button = new JButton();
private final JButton button1 = new JButton();
private final Box base = Box.createVerticalBox();
private final Box box = Box.createHorizontalBox();
private final Box box0 = Box.createHorizontalBox();
private final Box box1 = Box.createHorizontalBox();
private final Box box2 = Box.createHorizontalBox();
private final Box box3 = Box.createHorizontalBox();
private final Box box4 = Box.createHorizontalBox();
private final Box box5 = Box.createHorizontalBox();
private final Box box6 = Box.createHorizontalBox();
private final Box box7 = Box.createHorizontalBox();
private final Box box8 = Box.createHorizontalBox();
private String username, password, address, useremail, card;
private int age;
private String adminname;
public void getName(String name) {
this.adminname = name;
}
public AdminRegion(String adminname) {
setTitle("欢迎使用 在线 电子商城");
setBounds(300,100,900,900);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contenctPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
/***查看商城商品***/
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == button3) {
dispose();//销毁窗体
GoodsLook look = new GoodsLook();
}
}
});
/***查看我购买的商品***/
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == button4) {
dispose();//销毁窗体
UserLogin login = new UserLogin();
}
}
});
/***管理员登录***/
button5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == button5) {
dispose();//销毁窗体
AdminLogin admin = new AdminLogin();
}
}
});
/***找回账户、密码***/
button6.addActionListener(new ActionListener() {
contenctPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
contenctPanel.setBackground(Color.WHITE);
label.setText("在 线 电 子 商 城");
label.setFont(new Font("微软雅黑",1,32));
label.setForeground(Color.BLUE);
label0.setText("删 除 用 户 信 息 界 面\n");
label0.setFont(new Font("微软雅黑",1,25));
label1.setText("欢迎 " + adminname + " 管理员");
label1.setFont(new Font("微软雅黑",0,20));
text.setSize(800, 800);
Util db = new Util();
conn = db.getConnection();
try {
pres = conn.prepareStatement("select * from user");
res = pres.executeQuery(); //执行查询语句
text.setText("----用 户 序 号-----"+"---用 户 名------"+"-------用 户 密 码------"+"-------用 户 身 份 证 号-----------"+"--用 户 年 龄------"+"--用 户 邮 箱-----------"+"--用 户 地 址----");
while(res.next()){
int id = res.getInt("userId");
String name = res.getString("userName");
String pass = res.getString("userPass");
String card = res.getString("userCard");
int age = res.getInt("userAge");
String email = res.getString("userEmail");
String address = res.getString("userAddress");
text.append("\n"+" "+id+" \t "+name+"\t "+pass+" "+card+"\t "+age+" \t "+email+" "+address+"\n");
}
res.close();
pres.close();
conn.close();
}catch(Exception e) {
e.printStackTrace();
}
JScrollPane scroll = new JScrollPane(text);
scroll.setSize(700, 700);
label2.setText("这是所有的用户信息,删除用户只需要输入用户序号!");
private final JPanel contenctPanel = new JPanel();
private final JTextField field = new JTextField(10);//姓名
private final JTextField field1 = new JTextField(10);//密码
private final JTextField field2 = new JTextField(10);//身份证号码
private final JTextField field3 = new JTextField(10);//年龄
private final JTextField field4 = new JTextField(10);//邮箱地址
//private final JPasswordField password = new JPasswordField();//密码文本框
private final JLabel label = new JLabel();//文本标签
private final JLabel label0 = new JLabel();//文本标签
private final JLabel label1 = new JLabel();//用户名
private final JLabel label2 = new JLabel();//密码
private final JLabel label3 = new JLabel();//身份证号码
private final JLabel label4 = new JLabel();//年龄
private final JLabel label5 = new JLabel();//邮箱地址
private final JLabel label6 = new JLabel();//联系地址
private final JButton button = new JButton();
private final JButton button1 = new JButton();
private final Box base = Box.createVerticalBox();
private final Box box = Box.createHorizontalBox();
private final Box box0 = Box.createHorizontalBox();
private final Box box1 = Box.createHorizontalBox();
private final Box box2 = Box.createHorizontalBox();
private final Box box3 = Box.createHorizontalBox();
private final Box box4 = Box.createHorizontalBox();
private final Box box5 = Box.createHorizontalBox();
private final Box box6 = Box.createHorizontalBox();
private final Box box7 = Box.createHorizontalBox();
public void getName(String name) {
this.adminname = name;
}
public AdminGoodsInsert(String adminname) {
setTitle("欢迎使用 在线 电子商城");
setBounds(300,100,900,900);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contenctPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
contenctPanel.setBackground(Color.WHITE);
label.setText("在 线 电 子 商 城");
label.setFont(new Font("微软雅黑",1,32));
label.setForeground(Color.BLUE);
button1.setText("返回管理员服务主界面");
button1.setFont(new Font("微软雅黑",1,18));
button1.setBorderPainted(false);
button1.setBackground(Color.WHITE);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == button1) {
dispose();
AdminSelect select = new AdminSelect(adminname);
select.getName(adminname);
}
}
});
box.add(label);
box1.add(label0);
box2.add(label1);
box3.add(scroll);
box4.add(label2);
box5.add(label3);
box5.add(field);
box6.add(label4);
box6.add(field1);
box7.add(label5);
box7.add(field2);
box8.add(label6);
box8.add(field3);
box9.add(label7);
box9.add(field4);
box10.add(label8);
box10.add(field5);
box11.add(button);
box11.add(button1);
base.add(box);
base.add(Box.createVerticalStrut(10));
base.add(box0);
base.add(Box.createVerticalStrut(10));
base.add(box1);
base.add(Box.createVerticalStrut(10));
base.add(box2);
base.add(Box.createVerticalStrut(10));
base.add(box3);
base.add(Box.createVerticalStrut(30));
base.add(box4);
base.add(Box.createVerticalStrut(30));
base.add(box5);
base.add(Box.createVerticalStrut(30));
base.add(box6);
base.add(Box.createVerticalStrut(30));
private final Box box3 = Box.createHorizontalBox();
private final Box box4 = Box.createHorizontalBox();
private final Box box5 = Box.createHorizontalBox();
private final Box box6 = Box.createHorizontalBox();
private final Box box7 = Box.createHorizontalBox();
public void getName(String name) {
this.adminname = name;
}
public AdminGoodsInsert(String adminname) {
setTitle("欢迎使用 在线 电子商城");
setBounds(300,100,900,900);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contenctPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
contenctPanel.setBackground(Color.WHITE);
label.setText("在 线 电 子 商 城");
label.setFont(new Font("微软雅黑",1,32));
label.setForeground(Color.BLUE);
label0.setText("添 加 商 品 信 息 界 面\n");
label0.setFont(new Font("微软雅黑",1,25));
label6.setText("欢迎 " + adminname + " 管理员");
label6.setFont(new Font("微软雅黑",0,20));
label1.setText("商 品 名 称:");
label1.setFont(new Font("微软雅黑",0,18));
field.addActionListener(new Myaction());
label2.setText("生 产 日 期:");
label2.setFont(new Font("微软雅黑",0,18));
field1.addActionListener(new Myaction());
label3.setText("会 员 价 格:");
label3.setFont(new Font("微软雅黑",0,18));
field2.addActionListener(new Myaction());
label4.setText("非会员价格:");
label4.setFont(new Font("微软雅黑",0,18));
/***管理员注册界面***/
public class AdminRegion extends JFrame {
private final JPanel contenctPanel = new JPanel();
private final JTextField field = new JTextField(10);
private final JTextField field1 = new JTextField(10);
private final JTextField field2 = new JTextField(10);
private final JLabel label = new JLabel();
private final JLabel label0 = new JLabel();
private final JLabel label1 = new JLabel();
private final JLabel label2 = new JLabel();
private final JLabel label3 = new JLabel();
private final JLabel label4 = new JLabel();
private final JPasswordField password1 = new JPasswordField();//密码文本框
private final JButton button = new JButton();
private final JButton button1 = new JButton();
private final Box base = Box.createVerticalBox();
private final Box box = Box.createHorizontalBox();
private final Box box0 = Box.createHorizontalBox();
private final Box box1 = Box.createHorizontalBox();
private final Box box2 = Box.createHorizontalBox();
private final Box box3 = Box.createHorizontalBox();
private final Box box4 = Box.createHorizontalBox();
private final Box box5 = Box.createHorizontalBox();
private final Box box6 = Box.createHorizontalBox();
private final Box box7 = Box.createHorizontalBox();
private final Box box8 = Box.createHorizontalBox();
private String username, password, address, useremail, card;
private int age;
private String adminname;
public void getName(String name) {
this.adminname = name;
}
public AdminRegion(String adminname) {
setTitle("欢迎使用 在线 电子商城");
base.add(box);
base.add(Box.createVerticalStrut(40));
base.add(box0);
base.add(Box.createVerticalStrut(20));
base.add(box1);
base.add(Box.createVerticalStrut(30));
base.add(box2);
base.add(Box.createVerticalStrut(30));
base.add(box3);
base.add(Box.createVerticalStrut(30));
contenctPanel.add(base);
add(contenctPanel);
setVisible(true);
}
}
/***查看商城商品信息界面***/
public class GoodsLook extends JFrame {
static Connection conn; //声明Connection对象
static PreparedStatement pres; //向数据库发送SQL语句
static ResultSet res; //执行数据库查询
private final JPanel contenctPanel = new JPanel();
//private final JPasswordField password = new JPasswordField();//密码文本框
private final JLabel label = new JLabel();
private final JLabel label0 = new JLabel();
private final JLabel label1 = new JLabel();
private final JLabel label2 = new JLabel();
private final JButton button = new JButton();
private final JButton button1 = new JButton();
private final Box base = Box.createVerticalBox();
private final Box box = Box.createHorizontalBox();
box9.add(label1);
box1.add(button1);
box2.add(button2);
box3.add(button3);
box4.add(button4);
box5.add(button5);
box6.add(button6);
box7.add(button7);
base.add(box);
base.add(Box.createVerticalStrut(40));
base.add(box0);
base.add(Box.createVerticalStrut(40));
base.add(box9);
base.add(Box.createVerticalStrut(40));
base.add(box1);
base.add(Box.createVerticalStrut(40));
base.add(box2);
base.add(Box.createVerticalStrut(40));
base.add(box3);
base.add(Box.createVerticalStrut(40));
base.add(box4);
base.add(Box.createVerticalStrut(40));
base.add(box5);
base.add(Box.createVerticalStrut(40));
base.add(box6);
base.add(Box.createVerticalStrut(40));
base.add(box7);
base.add(Box.createVerticalStrut(40));
contenctPanel.add(base);
add(contenctPanel);
}
}
/***商城主界面***/
button4.setText("4、查 看 我 购 买 的 商 品");
button4.setBackground(Color.WHITE);
button4.setFont(new Font("微软雅黑",0,18));
button4.setBorderPainted(false);
button5.setText("5、管 理 员 登 录");
button5.setBackground(Color.WHITE);
button5.setFont(new Font("微软雅黑",0,18));
button5.setBorderPainted(false);
button6.setText("6、找 回 账 户、密 码");
button6.setBackground(Color.WHITE);
button6.setFont(new Font("微软雅黑",0,18));
button6.setBorderPainted(false);
button7.setText("7、退出 在线 电子商城");
button7.setBackground(Color.WHITE);
button7.setFont(new Font("微软雅黑",0,18));
button7.setBorderPainted(false);
box8.add(label);
box9.add(label1);
box1.add(button1);
box1.add(Box.createVerticalStrut(40));
box2.add(button2);
box2.add(Box.createVerticalStrut(40));
box3.add(button3);
box3.add(Box.createVerticalStrut(40));
box4.add(button4);
box4.add(Box.createVerticalStrut(40));
box5.add(button5);
box5.add(Box.createVerticalStrut(40));
box6.add(button6);
box6.add(Box.createVerticalStrut(40));
box7.add(button7);
box7.add(Box.createVerticalStrut(40));
base.add(box8);
base.add(Box.createVerticalStrut(40));
base.add(box9);
base.add(Box.createVerticalStrut(40));
base.add(box1);
base.add(Box.createVerticalStrut(40));
base.add(box2);
base.add(Box.createVerticalStrut(40));
base.add(box3);
base.add(Box.createVerticalStrut(40));
base.add(box4);
base.add(Box.createVerticalStrut(40));
base.add(box5);
base.add(Box.createVerticalStrut(40));
base.add(box6);
if(n != num) {
JOptionPane.showMessageDialog(null,"验证码不正确", "错误",JOptionPane.ERROR_MESSAGE);
field2.setText("");
}
}
}
});
//登录按钮
button.setText("登 录");
button.setFont(new Font("微软雅黑",0,18));
button.addActionListener(new Myaction());
//返回商城主界面
button1.setText(" 返回商城主界面");
button1.setFont(new Font("微软雅黑",1,18));
button1.setBorderPainted(false);
button1.setBackground(Color.WHITE);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == button1) {
dispose();
IndexWelcome welcome = new IndexWelcome();
}
}
});
box.add(label);
box0.add(label0);
box1.add(label1);
box1.add(field);
box2.add(label2);
box2.add(password1);
box3.add(label3);
box3.add(field2);
box4.add(button);
box4.add(button1);
String address = res.getString("userAddress");
text.append("\n"+" "+id+" \t "+name+"\t "+pass+" "+card+"\t "+age+" \t "+email+" "+address+"\n");
}
res.close();
pres.close();
conn.close();
}catch(Exception e) {
e.printStackTrace();
}
JScrollPane scroll = new JScrollPane(text);
scroll.setSize(700, 700);
button1.setText("返回管理员服务主界面");
button1.setFont(new Font("微软雅黑",1,18));
button1.setBorderPainted(false);
button1.setBackground(Color.WHITE);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == button1) {
AdminSelect select = new AdminSelect(adminname);
select.getName(adminname);
dispose();
}
}
});
box.add(label);
box0.add(label0);
box1.add(label1);
box2.add(scroll);
box3.add(button1);
base.add(box);
base.add(Box.createVerticalStrut(40));
base.add(box0);
base.add(Box.createVerticalStrut(20));
base.add(box1);
base.add(Box.createVerticalStrut(30));
base.add(box2);
base.add(Box.createVerticalStrut(30));
base.add(box3);
public class AdminRegion extends JFrame {
private final JPanel contenctPanel = new JPanel();
private final JTextField field = new JTextField(10);
private final JTextField field1 = new JTextField(10);
private final JTextField field2 = new JTextField(10);
private final JLabel label = new JLabel();
private final JLabel label0 = new JLabel();
private final JLabel label1 = new JLabel();
private final JLabel label2 = new JLabel();
private final JLabel label3 = new JLabel();
private final JLabel label4 = new JLabel();
private final JPasswordField password1 = new JPasswordField();//密码文本框
private final JButton button = new JButton();
private final JButton button1 = new JButton();
private final Box base = Box.createVerticalBox();
private final Box box = Box.createHorizontalBox();
private final Box box0 = Box.createHorizontalBox();
private final Box box1 = Box.createHorizontalBox();
private final Box box2 = Box.createHorizontalBox();
private final Box box3 = Box.createHorizontalBox();
private final Box box4 = Box.createHorizontalBox();
private final Box box5 = Box.createHorizontalBox();
private final Box box6 = Box.createHorizontalBox();
private final Box box7 = Box.createHorizontalBox();
private final Box box8 = Box.createHorizontalBox();
private String username, password, address, useremail, card;
private int age;
private String adminname;
public void getName(String name) {
public class AdminLogin extends JFrame {
private final JPanel contenctPanel = new JPanel();
private final JTextField field = new JTextField(10);//姓名
private final JTextField field1 = new JTextField(10);//密码
private final JTextField field2 = new JTextField(10);//随机验证码
private final JPasswordField password1 = new JPasswordField();//密码文本框
private final JLabel label = new JLabel();//文本标签
private final JLabel label0 = new JLabel();//文本标签
private final JLabel label1 = new JLabel();//用户名
private final JLabel label2 = new JLabel();//密码
private final JLabel label3 = new JLabel();//随机验证码
private final JButton button = new JButton();
private final JButton button1 = new JButton();
private final Box base = Box.createVerticalBox();
private final Box box = Box.createHorizontalBox();
private final Box box0 = Box.createHorizontalBox();
private final Box box1 = Box.createHorizontalBox();
private final Box box2 = Box.createHorizontalBox();
private final Box box3 = Box.createHorizontalBox();
private final Box box4 = Box.createHorizontalBox();
private String username, password, address, useremail, card;
private int age;
public AdminLogin() {
setTitle("欢迎使用 在线 电子商城");
setBounds(300,100,900,900);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contenctPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
contenctPanel.setBackground(Color.WHITE);
label.setText("在 线 电 子 商 城");
label.setFont(new Font("微软雅黑",1,32));
label.setForeground(Color.BLUE);
label0.setText("管 理 员 登 录 界 面\n");
label0.setFont(new Font("微软雅黑",1,25));
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == button3) {
dispose();
AdminUserDel del = new AdminUserDel(adminname);
del.getName(adminname);
}
}
});
button4.setText("4、删 除 商 品 信 息");
button4.setBackground(Color.WHITE);
button4.setFont(new Font("微软雅黑",1,18));
button4.setBorderPainted(false);
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == button4) {
dispose();
AdminGoodsDel del = new AdminGoodsDel(adminname);
del.getName(adminname);
}
}
});
button5.setText("5、添 加 商 品 信 息");
button5.setBackground(Color.WHITE);
button5.setFont(new Font("微软雅黑",1,18));
button5.setBorderPainted(false);
button5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == button5) {
dispose();
AdminGoodsInsert insert = new AdminGoodsInsert(adminname);
insert.getName(adminname);
}
}
label0.setText("查 看 用 户 信 息 界 面\n");
label0.setFont(new Font("微软雅黑",1,25));
label1.setText("欢迎 " + adminname + " 管理员");
label1.setFont(new Font("微软雅黑",0,20));
text.setSize(700, 700);
//text.setFont(new Font("微软雅黑",0,13));
Util db = new Util();
conn = db.getConnection();
try {
pres = conn.prepareStatement("select * from user");
res = pres.executeQuery(); //执行查询语句
text.setText("----用 户 序 号-----"+"---用 户 名------"+"-------用 户 密 码------"+"-------用 户 身 份 证 号-----------"+"--用 户 年 龄------"+"--用 户 邮 箱-----------"+"--用 户 地 址----");
while(res.next()){
int id = res.getInt("userId");
String name = res.getString("userName");
String pass = res.getString("userPass");
String card = res.getString("userCard");
int age = res.getInt("userAge");
String email = res.getString("userEmail");
String address = res.getString("userAddress");
text.append("\n"+" "+id+" \t "+name+"\t "+pass+" "+card+"\t "+age+" \t "+email+" "+address+"\n");
}
res.close();
pres.close();
conn.close();
}catch(Exception e) {
e.printStackTrace();
}
JScrollPane scroll = new JScrollPane(text);
scroll.setSize(700, 700);
button1.setText("返回管理员服务主界面");
button1.setFont(new Font("微软雅黑",1,18));
button1.setBorderPainted(false);
button1.setBackground(Color.WHITE);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == button1) {
AdminSelect select = new AdminSelect(adminname);
select.getName(adminname);
dispose();
}
}
});
box.add(label);
box0.add(label0);
box1.add(label1);
box7.add(label5);
box7.add(field2);
box8.add(label6);
box8.add(field3);
box9.add(label7);
box9.add(field4);
box10.add(label8);
box10.add(field5);
box11.add(button);
box11.add(button1);
base.add(box);
base.add(Box.createVerticalStrut(10));
base.add(box0);
base.add(Box.createVerticalStrut(10));
base.add(box1);
base.add(Box.createVerticalStrut(10));
base.add(box2);
base.add(Box.createVerticalStrut(10));
base.add(box3);
base.add(Box.createVerticalStrut(30));
base.add(box4);
base.add(Box.createVerticalStrut(30));
base.add(box5);
base.add(Box.createVerticalStrut(30));
base.add(box6);
base.add(Box.createVerticalStrut(30));
base.add(box7);
base.add(Box.createVerticalStrut(30));
base.add(box8);
base.add(Box.createVerticalStrut(30));
base.add(box9);
base.add(Box.createVerticalStrut(30));
base.add(box10);
base.add(Box.createVerticalStrut(40));
base.add(box11);
base.add(Box.createVerticalStrut(40));
contenctPanel.add(base);
add(contenctPanel);
setVisible(true);
public class AdminUserLook extends JFrame {
private String adminname;
static Connection conn; //声明Connection对象
static PreparedStatement pres; //向数据库发送SQL语句
static ResultSet res; //执行数据库查询
private final JPanel contenctPanel = new JPanel();
//private final JPasswordField password = new JPasswordField();//密码文本框
private final JLabel label = new JLabel();
private final JLabel label0 = new JLabel();
private final JLabel label1 = new JLabel();
private final JButton button = new JButton();
private final JButton button1 = new JButton();
private final Box base = Box.createVerticalBox();
private final Box box = Box.createHorizontalBox();
private final Box box0 = Box.createHorizontalBox();
private final Box box1 = Box.createHorizontalBox();
private final Box box2 = Box.createHorizontalBox();
private final Box box3 = Box.createHorizontalBox();
private JTextArea text= new JTextArea();
public void getName(String name) {
this.adminname = name;
}
public AdminUserLook(String adminname) {
setTitle("欢迎使用 在线 电子商城");
setBounds(300,100,900,900);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DBuser user = new DBuser();
contenctPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
contenctPanel.setBackground(Color.WHITE);
label.setText("在 线 电 子 商 城");
label.setFont(new Font("微软雅黑",1,32));
label.setForeground(Color.BLUE);