完整展示功能见b站视频:https://www.bilibili.com/video/BV1mp4y147Tb
开发工具:eclipse,mysql
数据库配置:一个login(ID,Password),一个wife(Name,firstwife,secondwife,thirdwife,fourthwife)
三张图片。
begin.java
package 后宫管理系统;
public class begin {
// 启动登录界面
public static void main(String[] args) {
new Login();
}
}
Login.java
package 后宫管理系统;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class Login implements ActionListener {
private final JFrame jf;
// 定义输入用户名和密码的标签提示
private final JLabel InputUserName;
private final JLabel InputPassWord;
// 定义输入用户名文本框
private final JTextField UserName;
// 定义输入密码框
private final JPasswordField PassWord;
// 定义登录和取消按钮
private final JButton Login;
private final JButton Cancel;
private final JButton Registered;
// 定义一个数据库操作的实例
private OperationMysql db = null;
Connection con;
PreparedStatement preSql;
ResultSet rs;
Login() {
// 各组件实例化过程
setDB();
jf = new JFrame("Login");
InputUserName = new JLabel(" ID: ");
InputPassWord = new JLabel("password:");
UserName = new JTextField();
PassWord = new JPasswordField();
Login = new JButton("登录");
Registered = new JButton("注册");
Cancel = new JButton("退出");
// 设置主窗口大小、位置和布局
jf.setSize(400, 150);
jf.setLocation(600, 400);
// 设置窗口流式布局
jf.setLayout(new FlowLayout());
// 设置用户名和密码框大小
UserName.setPreferredSize(new Dimension(300, 30));
PassWord.setPreferredSize(new Dimension(300, 30));
// 依次向主窗口添加各组件
jf.getContentPane().add(InputUserName);
jf.getContentPane().add(UserName);
jf.getContentPane().add(InputPassWord);
jf.getContentPane().add(PassWord);
jf.getContentPane().add(Login);
jf.getContentPane().add(Cancel);
jf.getContentPane().add(Registered);
// 设置主窗口不可调节大小
jf.setResizable(false);
// 设置主窗口默认关闭操作
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 给登录和取消按钮添加 Action 监听器
Login.addActionListener(this);
Cancel.addActionListener(this);
Registered.addActionListener(this);
// 设置主窗口可见
jf.setVisible(true);
}
private void setDB() {
db = new OperationMysql();
// 连接 mysql
db.setDburl("jdbc:mysql://localhost:3306/后宫1?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC");
// 加载驱动
db.setDbdriver("com.mysql.cj.jdbc.Driver");
// 这里的用户名和密码是要和你的 mysql 对应的,也是唯一需要更改的地方
db.setUsername("root");
db.setPassword("root1234");
}
@Override
public void actionPerformed(ActionEvent e) {
// 如果单击【退出】按钮则程序退出
if (e.getSource().equals(Cancel)) {
System.exit(0);
}
// 如果单击【登录】按钮则检查用户名和密码是否匹配
else if (e.getSource().equals(Login)) {
// 如果用户名和密码匹配,则打开具体操作面板
try {
con=db.CreateConnection("jdbc:mysql://localhost:3306/后宫1?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root","root1234");
} catch (Exception e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement("select *from login where ID=? and Password=?");
ps.setString(1, UserName.getText());
ps.setString(2, String.valueOf(PassWord.getPassword()));
rs = ps.executeQuery();
if (rs.next()) {
MySQLGUI myS = new MySQLGUI();
myS.initial();
jf.setVisible(false);
jf.dispose();
//new Login();
//jf.removeNotify();//一个函数
} else {
JOptionPane.showOptionDialog(jf, "用户名或密码错误", "登陆失败",
JOptionPane.CLOSED_OPTION,
JOptionPane.ERROR_MESSAGE, null, null, null);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//要执行的SQL语句
else if (e.getSource().equals(Registered)) {
Registered Re=new Registered();
}
}
}
MYSQLGUI.java
package 后宫管理系统;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
public class MySQLGUI extends JFrame implements MouseListener, ItemListener {
// 定义选项卡
private JTabbedPane Base;
// 定义选项卡上的嵌板
/*
* jp1, 添加记录
* jp2, 删除记录
* jp3, 更新记录
* jp4, 查找记录
* */
private JPanel jp1, jp2, jp3, jp4, jp5;
// 定义各按钮
/*
* InsertRecord, 添加记录按钮
* InsertReset, 添加取消按钮
* DeleteRecord, 删除记录按钮
* DeleteReset, 删除取消按钮
* QueryRecord, 查询记录按钮
* UpdateRecord, 更改记录按钮
* UpdateReset, 重置更新框
* */
private JButton InsertRecord, InsertReset, DeleteRecord, DeleteReset,
QueryRecord, UpdateRecord, UpdateReset, CourseQuery, GradeQuery,button1;
// 定义各标签
/*
* InsertNAME1, 插入姓名提示标签
* InsertFirstwife1, 插入大老婆提示标签
* InsertSecondwife1, 插入二老婆成绩提示标签
* InsertThirdwife1, 插入三老婆提示标签
* InsertFourthwife1, 插入四老婆提示标签
* DeleteNAME1, 删除姓名提示标签
* UpdateNAME1, 更新姓名提示标签
* */
private JLabel InsertNAME1, InsertFirstwife1, InsertSecondwife1, InsertThirdwife1,
InsertFourthwife1, DeleteNAME1, UpdateNAME1;
// 定义各文本框
/*
* InsertNAME2, 插入姓名文本框
* InsertFirstwife2, 插入大老婆文本框
* InsertSecondwife2, 插入二老婆文本框
* InsertThirdwife2, 插入三老婆文本框
* InsertFourthwife2, 插入英二老婆本框
* DeleteNAME2, 所要删除姓名的文本框
* UpdateNAME2, 所要更新姓名的文本框
* UpdateContent, 更新内容填写文本框
* NAMECondition, 查询NAME文本框
* FirstwifeCondition, 查询大老婆文本框
* SecondwifeCondition,查询二老婆文本框
* ThirdwifeCondition, 查询三老婆文本框
* FourthwifeCondition,查询英二老婆本框
* */
private JTextField InsertNAME2, InsertFirstwife2, InsertSecondwife2, InsertThirdwife2, InsertFourthwife2,
DeleteNAME2, UpdateNAME2, UpdateContent, NAMECondition, FirstwifeCondition, SecondwifeCondition, ThirdwifeCondition,
FourthwifeCondition;
// 定义显示结果文本域 显示 jp4 jp5 jp6 的查询结果
/*
* QueryRecordResult, 查询后宫信息结果文本域
* CourseQueryResult, 查询课程信息文本域
* */
private JTextArea QueryRecordResult, CourseQueryResult;
// 定义查询选项
/*
* NAME, 选择姓名查询
* Firstwife, 选择大老婆查询
* Secondwife, 选择二老婆查询
* Thirdwife, 选择三老婆查询
* Fourthwife, 选择四老婆查询
* */
private JRadioButton NAME, Firstwife, Secondwife, Thirdwife, Fourthwife;
// 定义一个数据库操作的实例
private OperationMysql db = null;
// 定义滚动条
private JScrollPane scroll = null;
private JScrollPane CourseScroll = null;
// 定义一个复选框用于选择更新的项目
private JComboBox<String> UpdateItem = null;
// 定义复选框用于选择查询的项目
private JComboBox<String> CourseItem = null; // 课程信息复选框
private JComboBox<String> GradeItem = null; // 课程成绩复选框
ImageIcon background1;
JLabel label1;
ImageIcon background2;
JLabel label2;
ImageIcon background3;
JLabel label3;
/*String StringA[]=new String[10];
StringA[0]="散步";
StringA[1]="荡秋千";
StringA[2]="激烈的运动";
StringA[3]="等他回家";
StringA[4]="做饭";
StringA[5]="搞事情";
StringA[6]="追剧";
StringA[7]="看电影";
StringA[8]="刷b站";
StringA[9]="12341234";*/
MySQLGUI() {
// 设置各按钮信息
setButton();
// 设置各标签信息
setLabel();
// 设置各文本框信息
setTextField();
// 设置各面板信息
setPanel();
// 设置布局信息
setLayout();
// 设置选项卡信息
setBase();
// 设置主窗口信息
setThis();
// 设置数据库信息
setDB();
setTitle("后宫信息管理系统");
setBounds(100,100,800,600);
}
// 设置各按钮信息的方法
private void setButton() {
// jp1 上的按钮
//background1 = new ImageIcon("image/567.jpeg"); //创建一个背景图片
//button1 = new JButton(background1); //把背景图片添加到标签里
//button1.setBounds(200, 200, background1.getIconWNameth(), background1.getIconHeight());
InsertRecord = new JButton("添加");
InsertRecord.setFont(new Font("宋体", 1, 20)); // 1 代表加粗,20 代表字体大小
InsertRecord.setBackground(Color.green);
InsertRecord.setBounds(150, 400, 100, 45);
InsertRecord.setMargin(new Insets(0, 0, 0, 0)); // 设置按钮的边缘空白为四个方向全为0,也即让按钮中的文本与按钮边缘贴齐
InsertReset = new JButton("重置");
InsertReset.setFont(new Font("宋体", 1, 20));
InsertReset.setBackground(Color.green);
InsertReset.setBounds(300, 400, 100, 45);
InsertReset.setMargin(new Insets(0, 0, 0, 0));
// jp2 上的按钮
DeleteRecord = new JButton("删除信息");
DeleteRecord.setFont(new Font("宋体", 1, 20));
DeleteRecord.setBackground(Color.CYAN);
DeleteRecord.setBounds(180, 450, 100, 45);
DeleteRecord.setMargin(new Insets(0, 0, 0, 0));
DeleteReset = new JButton("重置");
DeleteReset.setFont(new Font("宋体", 1, 20));
DeleteReset.setBackground(Color.CYAN);
DeleteReset.setBounds(400, 450, 100, 45);
DeleteReset.setMargin(new Insets(0, 0, 0, 0));
// jp3 上的按钮
UpdateRecord = new JButton("更新");
UpdateRecord.setFont(new Font("宋体", 1, 20));
UpdateRecord.setBackground(Color.yellow);
UpdateRecord.setBounds(250, 480, 100, 45);
UpdateReset = new JButton("重置");
UpdateReset.setFont(new Font("宋体", 1, 20));
UpdateReset.setBackground(Color.yellow);
UpdateReset.setBounds(400, 480, 100, 45);
// jp4 上的按钮
NAME = new JRadioButton("姓 名");
NAME.setFont(new Font("宋体", 1, 13));
NAME.setMargin(new Insets(0, 0, 0, 0));
NAME.setBounds(30, 300, 70, 20);
Firstwife = new JRadioButton("大老婆");
Firstwife.setFont(new Font("宋体", 1, 13));
Firstwife.setMargin(new Insets(0, 0, 0, 0));
Firstwife.setBounds(30, 330, 70, 20);
Secondwife = new JRadioButton("二老婆");
Secondwife.setFont(new Font("宋体", 1, 13));
Secondwife.setMargin(new Insets(0, 0, 0, 0));
Secondwife.setBounds(30, 360, 70, 20);
Thirdwife = new JRadioButton("三老婆");
Thirdwife.setFont(new Font("宋体", 1, 13));
Thirdwife.setMargin(new Insets(0, 0, 0, 0));
Thirdwife.setBounds(30, 390, 70, 20);
Fourthwife = new JRadioButton("四老婆");
Fourthwife.setFont(new Font("宋体", 1, 13));
Fourthwife.setMargin(new Insets(0, 0, 0, 0));
Fourthwife.setBounds(30, 420, 70, 20);
QueryRecord = new JButton("查询");
QueryRecord.setFont(new Font("宋体", 1, 20));
QueryRecord.setBackground(Color.CYAN);
QueryRecord.setBounds(600, 400, 80, 45);
// jp5 上的按钮
// 按键监听初始化
initial();
}
// 设置各标签信息的方法
private void setLabel() {
// jp1 上的标签
background1 = new ImageIcon("image/321.jpeg"); //创建一个背景图片
label1 = new JLabel(background1); //把背景图片添加到标签里
label1.setBounds(410, 0, 380, 900); //把标签设置为和图片等高等宽
background2 = new ImageIcon("image/wbb.jpeg"); //创建一个背景图片
label2 = new JLabel(background2); //把背景图片添加到标签里
label2.setBounds(100, 0, 600, 300); //把标签设置为和图片等高等宽
background3 = new ImageIcon("image/123.jpeg"); //创建一个背景图片
label3= new JLabel(background3); //把背景图片添加到标签里
label3.setBounds(100, 0,600, 300); //把标签设置为和图片等高等宽
InsertNAME1 = new JLabel("姓 名:");
InsertNAME1.setFont(new Font("楷体", 1, 22));
InsertNAME1.setBackground(Color.GREEN);
InsertNAME1.setBounds(100, 40, 120, 50);
InsertFirstwife1 = new JLabel("大老婆:");
InsertFirstwife1.setFont(new Font("楷体", 1, 22));
InsertFirstwife1.setBackground(Color.GREEN);
InsertFirstwife1.setBounds(100, 100, 120, 50);
InsertSecondwife1 = new JLabel("二老婆:");
InsertSecondwife1.setFont(new Font("楷体", 1, 22));
InsertSecondwife1.setBackground(Color.GREEN);
InsertSecondwife1.setBounds(100, 160, 120, 50);
InsertThirdwife1 = new JLabel("三老婆:");
InsertThirdwife1.setFont(new Font("楷体", 1, 22));
InsertThirdwife1.setBackground(Color.GREEN);
InsertThirdwife1.setBounds(100, 220, 120, 50);
InsertFourthwife1 = new JLabel("四老婆:");
InsertFourthwife1.setFont(new Font("楷体", 1, 22));
InsertFourthwife1.setBackground(Color.GREEN);
InsertFourthwife1.setBounds(100, 280, 120, 50);
// jp2 上的标签
DeleteNAME1 = new JLabel("姓 名:");
DeleteNAME1.setBounds(100, 300, 100, 50);
DeleteNAME1.setFont(new Font("楷体", 1, 22));
// jp3 上的标签
UpdateNAME1 = new JLabel("姓 名:");
UpdateNAME1.setFont(new Font("楷体", 1, 22));
UpdateNAME1.setBounds(200, 360, 120, 50);
UpdateItem = new JComboBox<>();
UpdateItem.setFont(new Font("楷体", 1, 22));
UpdateItem.setBounds(200, 420, 100, 45);
UpdateItem.addItem("大老婆");
UpdateItem.addItem("二老婆");
UpdateItem.addItem("三老婆");
UpdateItem.addItem("四老婆");
// jp4 上的标签
//...
}
// 设置各文本框信息的方法
private void setTextField() {
// jp1 上的文本框
InsertNAME2 = new JTextField();
InsertNAME2.setFont(new Font("宋体", 1, 23));
InsertNAME2.setBounds(210, 40, 200, 35);
InsertFirstwife2 = new JTextField();
InsertFirstwife2.setFont(new Font("宋体", 1, 23));
InsertFirstwife2.setBounds(210, 100, 200, 35);
InsertSecondwife2 = new JTextField();
InsertSecondwife2.setFont(new Font("宋体", 1, 23));
InsertSecondwife2.setBounds(210, 160, 200, 35);
InsertThirdwife2 = new JTextField();
InsertThirdwife2.setFont(new Font("宋体", 1, 23));
InsertThirdwife2.setBounds(210, 220, 200, 35);
InsertFourthwife2 = new JTextField();
InsertFourthwife2.setFont(new Font("宋体", 1, 23));
InsertFourthwife2.setBounds(210, 280, 200, 35);
// jp2 上的文本框
DeleteNAME2 = new JTextField("输入要删除信息的姓名");
DeleteNAME2.setFont(new Font("楷体", 1, 25));
DeleteNAME2.setBounds(210, 300, 350, 50);
// jp3 上的文本框
UpdateNAME2 = new JTextField();
UpdateNAME2.setFont(new Font("楷体", 1, 20));
UpdateNAME2.setBounds(310, 360, 200, 45);
UpdateContent = new JTextField("更新内容");
UpdateContent.setFont(new Font("楷体", 0, 22));
UpdateContent.setBounds(310, 420, 200, 45);
// jp4 上的文本框
QueryRecordResult = new JTextArea("查询结果:");
QueryRecordResult.setFont(new Font("楷体", 1, 20));
//QueryRecordResult.setBounds(30,30,560,260);
QueryRecordResult.setEditable(false);
QueryRecordResult.setLineWrap(true); // 当一行文字过多时自动换行
scroll = new JScrollPane(QueryRecordResult); // 添加滚动条
scroll.setBounds(30, 30, 560, 260);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); // 当需要垂直滚动条时显示
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);// 当需要水平滚动条时显示
NAMECondition = new JTextField();
NAMECondition.setFont(new Font("宋体", 1, 18));
NAMECondition.setBounds(120, 300, 100, 21);
FirstwifeCondition = new JTextField();
FirstwifeCondition.setFont(new Font("宋体", 1, 18));
FirstwifeCondition.setBounds(120, 330, 100, 21);
SecondwifeCondition = new JTextField();
SecondwifeCondition.setFont(new Font("宋体", 1, 18));
SecondwifeCondition.setBounds(120, 360, 100, 21);
ThirdwifeCondition = new JTextField();
ThirdwifeCondition.setFont(new Font("宋体", 1, 18));
ThirdwifeCondition.setBounds(120, 390, 100, 21);
FourthwifeCondition = new JTextField();
FourthwifeCondition.setFont(new Font("宋体", 1, 18));
FourthwifeCondition.setBounds(120, 420, 100, 21);
NAMECondition.setEditable(false);
FirstwifeCondition.setEditable(false);
SecondwifeCondition.setEditable(false);
ThirdwifeCondition.setEditable(false);
FourthwifeCondition.setEditable(false);
// jp5 上的文本框
}
// 设置各面板信息的方法
private void setPanel() {
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jp4 = new JPanel();
jp5 = new JPanel();
}
// 设置布局信息的方法
@SuppressWarnings("deprecation")
private void setLayout() {
// 添加 jp1 的组件
//this.getContentPane().add(label1);
jp1.setLayout(null);
jp1.add(label1);
jp1.add(InsertRecord);
jp1.add(InsertReset);
jp1.add(InsertNAME1);
jp1.add(InsertFirstwife1);
jp1.add(InsertSecondwife1);
jp1.add(InsertThirdwife1);
jp1.add(InsertFourthwife1);
jp1.add(InsertNAME2);
jp1.add(InsertFirstwife2);
jp1.add(InsertSecondwife2);
jp1.add(InsertThirdwife2);
jp1.add(InsertFourthwife2);
// 添加 jp2 上的组件
jp2.setLayout(null);
jp2.add(label2);
jp2.add(DeleteNAME1);
jp2.add(DeleteNAME2);
jp2.add(DeleteRecord);
jp2.add(DeleteReset);
// 添加 jp3 上的组件
jp3.setLayout(null);
jp3.add(label3);
jp3.add(UpdateNAME1);
jp3.add(UpdateNAME2);
jp3.add(UpdateItem);
jp3.add(UpdateContent);
jp3.add(UpdateRecord);
jp3.add(UpdateReset);
// 添加 jp4 上的组件
jp4.setLayout(null);
// jp4.add(QueryRecordResult);
jp4.add(scroll);
jp4.add(QueryRecord);
jp4.add(NAME);
jp4.add(Firstwife);
jp4.add(Secondwife);
jp4.add(Thirdwife);
jp4.add(Fourthwife);
jp4.add(NAMECondition);
jp4.add(FirstwifeCondition);
jp4.add(SecondwifeCondition);
jp4.add(ThirdwifeCondition);
jp4.add(FourthwifeCondition);
}
// 设置选项卡信息的方法
private void setBase() {
Base = new JTabbedPane(JTabbedPane.TOP);
Base.addTab("添加后宫记录", jp1);
Base.addTab("删除后宫记录", jp2);
Base.addTab("更新后宫记录", jp3);
Base.addTab("查找后宫记录", jp4);
}
// 设置主窗口信息的方法
private void setThis() {
this.add(Base);
this.setTitle("后宫信息管理系统");
this.setLocation(300, 200);
this.setSize(800, 550);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
// 设置数据库信息的方法
private void setDB() {
db = new OperationMysql();
// 连接 mysql
db.setDburl("jdbc:mysql://localhost:3306/后宫1?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC");
// 加载驱动
db.setDbdriver("com.mysql.cj.jdbc.Driver");
// 这里的用户名和密码是要和你的 mysql 对应的,也是唯一需要更改的地方
db.setUsername("root");
db.setPassword("root1234");
}
// 初始化
void initial() {
// 给各按钮添加监听器
// InsertRecord, InsertReset, DeleteRecord, DeleteReset, QueryRecord, UpdateRecord, CourseQuery, GradeQuery;
InsertRecord.addMouseListener(this);
InsertReset.addMouseListener(this);
DeleteRecord.addMouseListener(this);
DeleteReset.addMouseListener(this);
QueryRecord.addMouseListener(this);
UpdateRecord.addMouseListener(this);
UpdateReset.addMouseListener(this);
// 给各复选按钮添加监听器
// NAME,Firstwife, Secondwife, Thirdwife, Fourthwife
NAME.addItemListener(this);
Firstwife.addItemListener(this);
Secondwife.addItemListener(this);
Thirdwife.addItemListener(this);
Fourthwife.addItemListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
// 添加按钮功能
// 点击重置键则清空文本框
if (e.getSource().equals(InsertReset)) {
InsertNAME2.setText("");
InsertNAME2.setFont(new Font("宋体", 1, 23));
InsertFirstwife2.setText("");
InsertFirstwife2.setFont(new Font("宋体", 1, 23));
InsertSecondwife2.setText("");
InsertSecondwife2.setFont(new Font("宋体", 1, 23));
InsertThirdwife2.setText("");
InsertThirdwife2.setFont(new Font("宋体", 1, 23));
InsertFourthwife2.setText("");
InsertFourthwife2.setFont(new Font("宋体", 1, 23));
} else if (e.getSource().equals(InsertRecord)) {
// 添加记录功能
String InsertStuNAME = InsertNAME2.getText();
String InsertStuFirstwife = InsertFirstwife2.getText();
String InsertStuSecondwife = InsertSecondwife2.getText();
String InsertStuThirdwife = InsertThirdwife2.getText();
String InsertStuFourthwife = InsertFourthwife2.getText();
try {
db.setRs(db.executeQuery(InsertStuNAME));
if (!db.getRs().next()) {
db.executeInsert(InsertStuNAME, InsertStuFirstwife, InsertStuSecondwife, InsertStuThirdwife, InsertStuFourthwife);
JOptionPane.showOptionDialog(this, "添加信息成功!", "数据库操作提示",
JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
return;
} else JOptionPane.showOptionDialog(this, "添加失败", "温馨提示",
-1, 1, null, null, null);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
db.CloseRS();
db.CloseStmt();
db.CloseConnection();
}
} else if (e.getSource().equals(DeleteReset)) {
// 删除重置功能
DeleteNAME2.setText("");
DeleteNAME2.setFont(new Font("楷体", 1, 25));
} else if (e.getSource().equals(DeleteRecord)) {
// 删除功能
String DeleteStuNAME = DeleteNAME2.getText();
try {
db.setRs(db.executeQuery(DeleteStuNAME));
if (db.getRs().next()) {
db.executeDelete(DeleteStuNAME);
JOptionPane.showOptionDialog(this, "删除成功!", "数据库操作提示",
-1, 1, null, null, null);
return;
} else JOptionPane.showOptionDialog(this, "删除失败", "温馨提示",
-1, 1, null, null, null);
} catch (Exception exception) {
exception.printStackTrace();
}
} else if (e.getSource().equals(UpdateReset)) {
// 重置更新框功能
UpdateNAME2.setText("");
UpdateNAME2.setFont(new Font("宋体", 1, 20));
UpdateContent.setText("");
UpdateContent.setFont(new Font("宋体", 1, 20));
} else if (e.getSource().equals(UpdateRecord)) {
// 完成更新功能
String UpdateStuNAME = UpdateNAME2.getText();
try {
db.setRs(db.executeQuery(UpdateStuNAME));
if (!db.getRs().next()) {
JOptionPane.showOptionDialog(this, "没有记录无法更新",
"温馨提示", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, null, null);
return;
} else {
String updateItem = null;
// 更新选项是大老婆
if (UpdateItem.getSelectedItem().toString().equals("大老婆")) {
updateItem = "Firstwife";
}
// 更新的是二老婆成绩
else if (UpdateItem.getSelectedItem().toString().equals("二老婆")) {
updateItem = "Secondwife";
}
// 更新的是三老婆成绩
else if (UpdateItem.getSelectedItem().toString().equals("三老婆")) {
updateItem = "Thirdwife";
}
// 更新的是四老婆成绩
else if (UpdateItem.getSelectedItem().toString().equals("四老婆")) {
updateItem = "Fourthwife";
}
db.executeUpdate(UpdateStuNAME, updateItem, UpdateContent.getText());
JOptionPane.showOptionDialog(this, "更新成功!", "数据库操作提示",
-1, 1, null, null, null);
return;
}
} catch (Exception exception) {
exception.printStackTrace();
} finally {
db.CloseRS();
db.CloseStmt();
db.CloseConnection();
}
} else if (e.getSource().equals(QueryRecord)) {
// 完成查询功能
try {
// 默认设置各检索条件均为通配符
String a = "%", b = "%", c = "%", d = "%", f = "%";
// 如果 NAME 选项被选中,则获得该选项的输入内容
if (NAME.isSelected() && !NAMECondition.getText().trim().isEmpty()) {
a = NAMECondition.getText();
}
// 如果 Firstwife 选项被选中,则获得该选项的输入内容
if (Firstwife.isSelected() && !FirstwifeCondition.getText().trim().isEmpty()) {
b = FirstwifeCondition.getText();
}
// 如果 Thirdwife 选项被选中,则获得该选项的输入内容
if (Thirdwife.isSelected() && !ThirdwifeCondition.getText().trim().isEmpty()) {
d = ThirdwifeCondition.getText();
}
// 如果 Fourthwife 选项被选中,则获得该选项的输入内容
if (Fourthwife.isSelected() && !FourthwifeCondition.getText().trim().isEmpty()) {
f = FourthwifeCondition.getText();
}
// 如果 Secondwife 选项被选中,则获得该选项的输入内容
if (Secondwife.isSelected() && !SecondwifeCondition.getText().trim().isEmpty()) {
c = SecondwifeCondition.getText();
}
// 根据各选项检索关键字进行查询,并返回结果集
db.setRs(db.executeQueryByCondition(a, b, c, d, f));
// 定义结果集中记录条数
String StringA[]=new String[10];
StringA[0]="散步";
StringA[1]="荡秋千";
StringA[2]="激烈的运动";
StringA[3]="等他回家";
StringA[4]="做饭";
StringA[5]="搞事情";
StringA[6]="追剧";
StringA[7]="看电影";
StringA[8]="刷b站";
StringA[9]="12341234";
int i = 0;
Random rd=new Random();
int k=0;
QueryRecordResult.setText("查询结果:");
// 输出结果集记录
while (db.getRs().next()) {
++i;
k=rd.nextInt(10);
QueryRecordResult.append("\r\n" + "第" + i + "条记录:" + "\r\n"
+ "姓 名:" + db.getRs().getString(1) + "\r\n"
+ "大老婆:" + db.getRs().getString(2) + "\r\n"
+ "二老婆:" + db.getRs().getString(3) + "\r\n"
+ "三老婆:" + db.getRs().getString(4) + "\r\n"
+ "四老婆:" + db.getRs().getString(5) + "\r\n"
+db.getRs().getString(1)+"的四位老婆正在"+StringA[k]+
("\r\n--------------------------------------"));
}
QueryRecordResult.setText(QueryRecordResult.getText() +
"\r\n" + "共有" + i + "条后宫记录");
} catch (Exception e1) {
e1.printStackTrace();
} finally {
db.CloseRS();
db.CloseStmt();
db.CloseConnection();
}
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void itemStateChanged(ItemEvent e) {
// 如果查询选项 NAME 被选中,则可以输入 NAME 进行查询
if (e.getSource().equals(NAME)) {
NAMECondition.setEditable(NAME.isSelected());
}
// 如果选项大老婆被选中,则可以输入大老婆进行查询
else if (e.getSource().equals(Firstwife)) {
FirstwifeCondition.setEditable(Firstwife.isSelected());
}
// 如果二老婆被选中,则可以输入二老婆成绩进行查询
else if (e.getSource().equals(Secondwife)) {
SecondwifeCondition.setEditable(Secondwife.isSelected());
}
// 如果三老婆选项被选中,则可以输入三老婆成绩查询
else if (e.getSource().equals(Thirdwife)) {
ThirdwifeCondition.setEditable(Thirdwife.isSelected());
}
// 如果四老婆选项被选中,则可以输入四老婆成绩来查询
else if (e.getSource().equals(Fourthwife)) {
FourthwifeCondition.setEditable(Fourthwife.isSelected());
}
}
}
OperationMysql.java
package 后宫管理系统;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class OperationMysql {
// 定义数据库连接url
private String dburl = null;
// 定义数据库连接
private Connection conn = null;
// 定义数据库状态
private PreparedStatement stmt = null;
// 定义数据库返回结果集
private ResultSet rs = null;
// 定义数据库用户名
private String username = null;
// 定义数据库连接密码
private String password = null;
// 定义数据库驱动方式
private String dbdriver = null;
// 设置数据库连接url的方法
public void setDburl(String dburl) {
this.dburl = dburl;
}
// 返回当前实例数据库连接url
public String getDburl() {
return dburl;
}
// 返回当前实例结果集的方法
public ResultSet getRs() {
return rs;
}
// 设置当前实例结果集的方法
public void setRs(ResultSet rs) {
this.rs = rs;
}
// 设置数据库用户名的方法
public void setUsername(String username) {
this.username = username;
}
// 返回当前实例化数据库用户名
public String getUsername() {
return username;
}
// 设置数据库连接的方法
public void setPassword(String password) {
this.password = password;
}
// 返回当前实例数据库连接密码
public String getPassword() {
return password;
}
// 设置数据库驱动方式的方法
public void setDbdriver(String dbdriver) {
this.dbdriver = dbdriver;
}
// 返回当前实例数据库驱动方式的方法
public String getDbdriver() {
return dbdriver;
}
// 创建数据库连接的方法
Connection CreateConnection(String dburl, String username, String password) throws Exception {
setDburl(dburl);
setUsername(username);
setPassword(password);
Class.forName(getDbdriver());
// 根据数据库路径、用户名和密码创建连接并返回该连接
return DriverManager.getConnection(dburl, username, password);
}
// 关闭结果集的方法
public void CloseRS() {
try {
rs.close();
} catch (SQLException e) {
System.out.println("关闭结果集时发生错误!");
}
}
// 关闭状态的方法
public void CloseStmt() {
try {
stmt.close();
} catch (SQLException e) {
System.out.println("关闭状态时发生错误!");
}
}
// 关闭连接的方法
public void CloseConnection() {
try {
conn.close();
} catch (SQLException e) {
System.out.println("关闭连接时发生错误!");
}
}
// 增
void executeInsert(String InsertID, String InsertName, String Chinese, String Math, String English) throws Exception {
try {
conn = CreateConnection(getDburl(), getUsername(), getPassword());
stmt = conn.prepareStatement("insert into wife values(?,?,?,?,?)");
stmt.setString(1, InsertID);
stmt.setString(2, InsertName);
stmt.setString(3, Chinese);
stmt.setString(4, Math);
stmt.setString(5, English);
stmt.executeUpdate();
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
// 删
void executeDelete(String DeleteID) throws Exception {
try {
conn = CreateConnection(getDburl(), getUsername(), getPassword());
stmt = conn.prepareStatement("delete from wife where Name = ?");
stmt.setString(1, DeleteID);
stmt.executeUpdate();
CloseStmt();
CloseConnection();
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
// 查 主键 是否在表中
ResultSet executeQuery(String StuID) throws Exception {
try {
String sql = "select * from wife where Name = ?";
conn = CreateConnection(getDburl(), getUsername(), getPassword());
stmt = conn.prepareStatement(sql);
stmt.setString(1, StuID);
rs = stmt.executeQuery();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
return rs;
}
// 改
void executeUpdate(String UpdateID, String UpdateItem, String UpdateContent) throws Exception {
try {
conn = CreateConnection(getDburl(), getUsername(), getPassword());
String sql = "update wife set " + UpdateItem + " = ? where Name = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, UpdateContent);
stmt.setString(2, UpdateID);
stmt.executeUpdate();
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
// 按条件查询
ResultSet executeQueryByCondition(String stuname, String firstwife, String secondwife, String thirdwife, String fourthwife) throws Exception {
try {
String sql = "select * from wife where Name like ? and firstwife like ? and secondwife like ? " +
"and thirdwife like ? and fourthwife like ? order by Name asc";
conn = CreateConnection(getDburl(), getUsername(), getPassword());
stmt = conn.prepareStatement(sql);
if (stuname.equals("%")) {
stmt.setString(1, "%");
} else {
stmt.setString(1, "%" + stuname + "%");
}
if (firstwife.equals("%")) {
stmt.setString(2, "%");
} else {
stmt.setString(2, "%" + firstwife + "%");
}
if (secondwife.equals("%")) {
stmt.setString(3, "%");
} else {
stmt.setString(3, "%" + secondwife + "%");
}
if (thirdwife.equals("%")) {
stmt.setString(4, "%");
} else {
stmt.setString(4, "%" + thirdwife + "%");
}
if (fourthwife.equals("%")) {
stmt.setString(5, "%");
} else {
stmt.setString(5, "%" + fourthwife + "%");
}
rs = stmt.executeQuery();
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
return rs;
}
}
Registered.java
package 后宫管理系统;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class Registered extends JFrame implements ActionListener {
// 定义输入用户名、密码和确认密码的标签提示
private final JLabel InputUserName;
private final JLabel InputPassWord;
private final JLabel InputPassWordtwo;
// 定义输入用户名文本框
private final JTextField UserName;
// 定义输入密码框
private final JPasswordField PassWord;
private final JPasswordField PassWordtwo;
// 定义登录和取消按钮
private final JButton OK;
private final JButton Cancel;
// 定义一个数据库操作的实例
private OperationMysql db = null;
Connection con;
PreparedStatement preSql;
ResultSet rs;
Registered() {
// 各组件实例化过程
setDB();
InputUserName = new JLabel(" ID: ");
InputPassWord = new JLabel(" password :");
InputPassWordtwo = new JLabel("check password:");
UserName = new JTextField();
PassWord = new JPasswordField();
PassWordtwo = new JPasswordField();
OK = new JButton("确认注册");
Cancel = new JButton("取消");
// 设置主窗口大小、位置和布局
setTitle("注册界面");
setSize(450, 200);
setLocation(600, 400);
// 设置窗口流式布局
setLayout(new FlowLayout());
// 设置用户名和密码框大小
UserName.setPreferredSize(new Dimension(300, 30));
PassWord.setPreferredSize(new Dimension(300, 30));
PassWordtwo.setPreferredSize(new Dimension(300, 30));
// 依次向主窗口添加各组件
getContentPane().add(InputUserName);
getContentPane().add(UserName);
getContentPane().add(InputPassWord);
getContentPane().add(PassWord);
getContentPane().add(InputPassWordtwo);
getContentPane().add(PassWordtwo);
getContentPane().add(OK);
getContentPane().add(Cancel);
// 设置主窗口不可调节大小
setResizable(false);
// 设置主窗口默认关闭操作
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 给登录和取消按钮添加 Action 监听器
OK.addActionListener(this);
Cancel.addActionListener(this);
// 设置主窗口可见
setVisible(true);
}
private void setDB() {
db = new OperationMysql();
// 连接 mysql
db.setDburl("jdbc:mysql://localhost:3306/后宫1?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC");
// 加载驱动
db.setDbdriver("com.mysql.cj.jdbc.Driver");
// 这里的用户名和密码是要和你的 mysql 对应的,也是唯一需要更改的地方
db.setUsername("root");
db.setPassword("root1234");
}
@Override
public void actionPerformed(ActionEvent e) {
// 如果单击【退出】按钮则程序退出
if (e.getSource().equals(Cancel)) {
System.exit(0);
}
// 如果单击【登录】按钮则检查用户名和密码是否匹配
else if (e.getSource().equals(OK)) {
// 如果用户名和密码匹配,则打开具体操作面板
try {
con=db.CreateConnection("jdbc:mysql://localhost:3306/后宫1?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root","root1234");
} catch (Exception e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
String sqlstr="insert into login values(?,?)";
try {
Statement Statement=con.createStatement();
Statement = con.createStatement();
//要执行的SQL语句
String sql="select * from login where ID="+ UserName.getText();
//ResultSet类,用来存放获取的结果集!
ResultSet rs=Statement.executeQuery(sql);
String id=null;
String password=null;
while(rs.next()){
id=rs.getString(1);
password=rs.getString(2);
if(id!=null){
JOptionPane.showOptionDialog(this, "该账号已被注册", "注册失败",
JOptionPane.CLOSED_OPTION,
JOptionPane.ERROR_MESSAGE, null, null, null);
return;
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String s1=String.valueOf(PassWord.getPassword());
String s2=String.valueOf(PassWordtwo.getPassword());
System.out.println(s1);
System.out.println(s2);
if (s1.equals(s2)) {
try {
preSql=con.prepareStatement(sqlstr);
preSql.setString(1,String.valueOf(UserName.getText()) );
preSql.setString(2,String.valueOf(PassWord.getPassword()) );
int ok=preSql.executeUpdate();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else {
JOptionPane.showOptionDialog(this, "两次输入的密码不一致", "注册失败",
JOptionPane.CLOSED_OPTION,
JOptionPane.ERROR_MESSAGE, null, null, null);
}
//rs.close();
//con.close();
}
}
}