这里写目录标题
一级目录
二级目录
三级目录
项目4 校园二手物品信息管理系统
标题1、 项目概述
设计一个简单的校园二手物品信息管理的程序,管理人员能够使用程序对物品基本信息进行管理,包括数据的添加、修改删除和浏览;
标题2、 项目设计
2.1项目目标
1、程序对物品基本信息进行管理,包括数据的添加、修改删除和浏览;能够对商品进行管理。
2、在添加商品基本信息相关数据时,编号不能重复还有在添加学生选课信息时,要求信息需要完整。
3、应用程序提供操作界面,可以方便用户进行功能选择,实现信息的管理和查询, 并可以清晰地显示相关信息
2.2构建开发环境
【1】系统开发平台:
编程环境:eclipse
数据库:MySQL Workbench 8.0 CE
【2】系统开发语言:
JAVA
【3】运行平台:
eclipse
2.3系统功能结构
【1】 删除功能
用于对指定编号的商品信息进行删除;
【2】 查找功能
可根据数据库中的表中的的属性来查询表内对应信息,可以根据编号、商品名称、新旧程度、价格等等,来查询对应的数据。
【3】 修改功能
对于指定编号的商品的四类主要信息(商品名称、新旧程度、买入价格、出售价格)进行修改。
【4】 增添功能
输入商品的完整信息才能完成数据的增添。
3、系统业务流程图:
1、数据库建立数据库以及表→
2、启动程序→
3、连接数据库→
4、对数据库中对于的表实现增删改查操作;
4、公共类设计
5、详细设计
1、数据库建立;(选择utf8 和utf8-bin)
2、表的建立:
①表名;
②各类属性名(列名);
③各属性的格式类型;
3、对应表的建立生成了对应数据库的操作语言(T-SQL语句)
点击应用即可生成表;
4、对表的内容进行添加;完善每一列信息;
(此处为了简便只写了8条数据)
5、对应表的内容的输入生成了对应的T-SQL语句,点击应用;
6、对程序的的测试部分:
1、添加信息操作,
输入编号(不能重复)以及其他商品的基本信息;
提示添加信息操作成功:
如果增添的信息不完善,例如下面(缺损出售价格)就会报错;
2、查询信息:
点击查询,找到了刚刚添加的充电宝商品以及基本信息:
可以根据商品的(编号、新旧程度、买入、卖出价格来查询)
3、更新信息:
通过输入编号、对指定的属性进行修改:
测试:对编号为12的商品把新旧程度从5修改为10;
查询信息验证更新是否正确:
可以看到编号为12的充电宝的新旧程度已经更改为10;
4、删除信息:
输入商品的编号点击删除就可以成功删除该数据;
5、项目小结
1、信息管理系统涉及:
①编程知识;
②数据库知识;
③程序开发环境与数据库连接的操作;
5、编程主要涉及图形界面涉及和增删改查,连接这几个主要部分。增删改查主要是获取、判断这些基本操作,难度不大。主要是要结合数据库的操作,有些许陌生,难以快速上手,需要参考借鉴已有代码设计。
6、本实验难点是,太过繁琐,而且存在很多地方都需要用try-catch语句来保护语句正常执行。所以建议是新手首先要收悉数据库的操作(SQLSERVER或者MYSQL)而且要学会对数据库的驱动和增删改查的基础操作。
6、参考文献
1、casd https://blog.csdn.net/qq_44543508/article/details/94589868
2、CSDN https://blog.csdn.net/weixin_39880621/article/details/111367599
3、Java数据库程序设计 《Java程序设计教程》程科 潘磊
代码部分:
package datebase;
public class StartMySql {
// 启动登录界面
public static void main(String[] args) {
new Login();
}
}
package datebase;
//Login.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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;
Login() {
// 各组件实例化过程
jf = new JFrame("Login");
InputUserName = new JLabel("用户名:");
InputPassWord = new JLabel(" 密 码:");
UserName = new JTextField();
PassWord = new JPasswordField();
Login = 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.setResizable(false);
// 设置主窗口默认关闭操作
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 给登录和取消按钮添加 Action 监听器
Login.addActionListener(this);
Cancel.addActionListener(this);
// 设置主窗口可见
jf.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// 如果单击【退出】按钮则程序退出
if (e.getSource().equals(Cancel)) {
System.exit(0);
}
// 如果单击【登录】按钮则检查用户名和密码是否匹配
else if (e.getSource().equals(Login)) {
// 如果用户名和密码匹配,则打开具体操作面板
if (UserName.getText().equals("root") && String.valueOf(PassWord.getPassword()).equals("221234")) {
MySQLGUI myS = new MySQLGUI();
myS.initial();
jf.setVisible(false);
jf.dispose();
}
// 如果用户名和密码不匹配,则给出提示对话框
else {
JOptionPane.showOptionDialog(jf, "用户名或密码错误", "登陆失败",
JOptionPane.CLOSED_OPTION,
JOptionPane.ERROR_MESSAGE, null, null, null);
}
}
}
}
package datebase;
//OperationMySql.java
import java.sql.*;
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 STATE, String BUY, String SELL)
throws Exception {
try {
conn = CreateConnection(getDburl(), getUsername(), getPassword());
stmt = conn.prepareStatement("insert into goods values(?,?,?,?,?)");
stmt.setString(1, InsertID);
stmt.setString(2, InsertName);
stmt.setString(3, STATE);
stmt.setString(4, BUY);
stmt.setString(5,SELL);
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 goods where ID = ?");
stmt.setString(1, DeleteID);
stmt.executeUpdate();
CloseStmt();
CloseConnection();
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
// 查 主键 是否在表中
ResultSet executeQuery(String ID) throws Exception {
try {
String sql = "select * from goods where ID = ?";
conn = CreateConnection(getDburl(), getUsername(), getPassword());
stmt = conn.prepareStatement(sql);
stmt.setString(1, ID);
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 goods set " + UpdateItem + " = ? where ID = ?";
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 ID, String NAME, String STATE, String BUY,
String SELL) throws Exception {
try {
String sql = "select * from GOODS where ID like ? and NAME like ? and STATE like ? " +
"and BUY like ? and SELL like ? order by ID asc";
conn = CreateConnection(getDburl(), getUsername(), getPassword());
stmt = conn.prepareStatement(sql);
if (ID.equals("%")) {
stmt.setString(1, "%");
} else {
stmt.setString(1, "%" + ID + "%");
}
if (NAME.equals("%")) {
stmt.setString(2, "%");
} else {
stmt.setString(2, "%" + NAME + "%");
}
if (STATE.equals("%")) {
stmt.setString(3, "%");
} else {
stmt.setString(3, "%" + STATE + "%");
}
if (BUY.equals("%")) {
stmt.setString(4, "%");
} else {
stmt.setString(4, "%" + BUY + "%");
}
if (SELL.equals("%")) {
stmt.setString(5, "%");
} else {
stmt.setString(5, "%" + SELL + "%");
}
rs = stmt.executeQuery();
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
return rs;
}
/*
ResultSet executeQueryByCourse(String course) throws Exception {
try {
conn = CreateConnection(getDburl(), getUsername(), getPassword());
String sql = "select * from course where Course = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, course);
rs = stmt.executeQuery();
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
return rs;
}
*/
/*
ResultSet executeQueryByGrade(String grade) throws Exception {
try {
conn = CreateConnection(getDburl(), getUsername(), getPassword());
String sql = "select * from summary where Course = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1,grade);
rs = stmt.executeQuery();
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
return rs;
}
*/
}
package datebase;
//MySQLGUI.java
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;
public class MySQLGUI extends JFrame implements MouseListener,
ItemListener {
/**
*
*/
private static final long serialVersionUID = 1L;
// 定义选项卡
private JTabbedPane Base;
// 定义选项卡上的嵌板
/*
* jp1, 添加记录
* jp2, 删除记录
* jp3, 更新记录
* jp4, 查找记录
* jp5, 选课记录
* jp6 课程平均分
* */
private JPanel jp1, jp2, jp3, jp4;
// 定义各按钮
/*
* InsertRecord, 添加记录按钮
* InsertReset, 添加取消按钮
* DeleteRecord, 删除记录按钮
* DeleteReset, 删除取消按钮
* QueryRecord, 查询记录按钮
* UpdateRecord, 更改记录按钮
* UpdateReset, 重置更新框
* CourseQuery, 选课表查询按钮
* GradeQuery, 成绩查询按钮
* */
private JButton InsertRecord, InsertReset, DeleteRecord, DeleteReset,
QueryRecord, UpdateRecord, UpdateReset, CourseQuery, GradeQuery;
// 定义各标签
private JLabel InsertID1, InsertNAME1, InsertSTATE1, InsertBUY1,
InsertSELL1, DeleteID1, UpdateID1;
private JTextField InsertID2, InsertNAME2, InsertSTATE2, InsertBUY2, InsertSELL2,
DeleteID2, UpdateID2, UpdateContent, IDCondition, NAMECondition, STATECondition, BUYCondition,
SELLCondition;
// 定义显示结果文本域 显示 jp4 jp5 jp6 的查询结果
private JTextArea QueryRecordResult, GradeQueryResult; // 定义查询选项
private JRadioButton ID, NAME, STATE,BUY,SELL;
// 定义一个数据库操作的实例
private OperationMySql db = null;
// 定义滚动条
private JScrollPane scroll = null;
//private JScrollPane CourseScroll = null;
//private JScrollPane GradeScroll = null;
// 定义一个复选框用于选择更新的项目
private JComboBox<String> UpdateItem = null;
// 定义复选框用于选择查询的项目
private JComboBox<String> CourseItem = null; // 课程信息复选框
private JComboBox<String> GradeItem = null; // 课程成绩复选框
MySQLGUI() {
// 设置各按钮信息
setButton();
// 设置各标签信息
setLabel();
// 设置各文本框信息
setTextField();
// 设置各面板信息
setPanel();
// 设置布局信息
setLayout();
// 设置选项卡信息
setBase();
// 设置主窗口信息
setThis();
// 设置数据库信息
setDB();
}
// 设置各按钮信息的方法
private void setButton() {
// jp1 上的按钮
InsertRecord = new JButton("添加");
InsertRecord.setFont(new Font("宋体", 1, 20)); // 1 代表加粗,20 代表字体大小
InsertRecord.setBackground(Color.CYAN);
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.CYAN);
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(150, 350, 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(300, 350, 100, 45);
DeleteReset.setMargin(new Insets(0, 0, 0, 0));
// jp3 上的按钮
UpdateRecord = new JButton("更新");
UpdateRecord.setFont(new Font("宋体", 1, 20));
UpdateRecord.setBackground(Color.CYAN);
UpdateRecord.setBounds(250, 400, 100, 45);
UpdateReset = new JButton("重置");
UpdateReset.setFont(new Font("宋体", 1, 20));
UpdateReset.setBackground(Color.CYAN);
UpdateReset.setBounds(400, 400, 100, 45);
// jp4 上的按钮
ID = new JRadioButton("编号");
ID.setFont(new Font("宋体", 1, 15));
ID.setMargin(new Insets(0, 0, 0, 0));
ID.setBounds(30, 300, 55, 20);
NAME = new JRadioButton("商品名称");
NAME.setFont(new Font("宋体", 1, 15));
NAME.setMargin(new Insets(0, 0, 0, 0));
NAME.setBounds(30, 330, 55, 20);
STATE = new JRadioButton("新旧程度");
STATE.setFont(new Font("宋体", 1, 15));
STATE.setMargin(new Insets(0, 0, 0, 0));
STATE.setBounds(30, 360, 55, 20);
BUY = new JRadioButton("买入价格");
BUY.setFont(new Font("宋体", 1, 15));
BUY.setMargin(new Insets(0, 0, 0, 0));
BUY.setBounds(30, 390, 55, 20);
SELL = new JRadioButton("出售价格");
SELL.setFont(new Font("宋体", 1, 15));
SELL.setMargin(new Insets(0, 0, 0, 0));
SELL.setBounds(30, 420, 55, 20);
QueryRecord = new JButton("查询");
QueryRecord.setFont(new Font("宋体", 1, 20));
QueryRecord.setBackground(Color.CYAN);
QueryRecord.setBounds(600, 400, 80, 45);
// jp5 上的按钮
CourseQuery = new JButton("查询");
CourseQuery.setFont(new Font("宋体", 1, 20));
CourseQuery.setBackground(Color.CYAN);
CourseQuery.setBounds(600, 400, 80, 45);
// jp6 上的按钮
GradeQuery = new JButton("查询");
GradeQuery.setFont(new Font("宋体", 1, 20));
GradeQuery.setBackground(Color.PINK);
GradeQuery.setBounds(600, 400, 80, 45);
// 按键监听初始化
initial();
}
// 设置各标签信息的方法
private void setLabel() {
// jp1 上的标签
InsertID1 = new JLabel("编 号:");
InsertID1.setFont(new Font("楷体", 1, 22));
InsertID1.setBackground(Color.GREEN);
InsertID1.setBounds(100, 40, 120, 50);
InsertNAME1 = new JLabel("商品名称:");
InsertNAME1.setFont(new Font("楷体", 1, 22));
InsertNAME1.setBackground(Color.GREEN);
InsertNAME1.setBounds(100, 100, 120, 50);
InsertSTATE1 = new JLabel("新旧程度:");
InsertSTATE1.setFont(new Font("楷体", 1, 22));
InsertSTATE1.setBackground(Color.GREEN);
InsertSTATE1.setBounds(100, 160, 120, 50);
InsertBUY1 = new JLabel("买入价格:");
InsertBUY1.setFont(new Font("楷体", 1, 22));
InsertBUY1.setBackground(Color.GREEN);
InsertBUY1.setBounds(100, 220, 120, 50);
InsertSELL1 = new JLabel("出售价格:");
InsertSELL1.setFont(new Font("楷体", 1, 22));
InsertSELL1.setBackground(Color.GREEN);
InsertSELL1.setBounds(100, 280, 120, 50);
// jp2 上的标签
DeleteID1 = new JLabel("编 号:");
DeleteID1.setBounds(100, 100, 100, 50);
DeleteID1.setFont(new Font("楷体", 1, 22));
// jp3 上的标签
UpdateID1 = new JLabel("编 号:");
UpdateID1.setFont(new Font("楷体", 1, 22));
UpdateID1.setBounds(200, 60, 120, 50);
UpdateItem = new JComboBox<>();
UpdateItem.setFont(new Font("楷体", 1, 22));
UpdateItem.setBounds(200, 200, 100, 45);
UpdateItem.addItem("商品名称");
UpdateItem.addItem("新旧程度");
UpdateItem.addItem("买入价格");
UpdateItem.addItem("出售价格");
// jp4 上的标签
//...
// jp5 上的标签
CourseItem = new JComboBox<>();
CourseItem.setFont(new Font("楷体", 1, 22));
CourseItem.setBounds(100, 40, 140, 45);
CourseItem.addItem("新旧程度");
CourseItem.addItem("买入价格");
CourseItem.addItem("出售价格");
// jp6 上的标签
GradeItem = new JComboBox<>();
GradeItem.setFont(new Font("楷体", 1, 22));
GradeItem.setBounds(100, 40, 140, 45);
GradeItem.addItem("新旧程度");
GradeItem.addItem("买入价格");
GradeItem.addItem("出售价格");
}
// 设置各文本框信息的方法
private void setTextField() {
// jp1 上的文本框
InsertID2 = new JTextField();
InsertID2.setFont(new Font("宋体", 1, 23));
InsertID2.setBounds(210, 40, 200, 35);
InsertNAME2 = new JTextField();
InsertNAME2.setFont(new Font("宋体", 1, 23));
InsertNAME2.setBounds(210, 100, 200, 35);
InsertSTATE2 = new JTextField();
InsertSTATE2.setFont(new Font("宋体", 1, 23));
InsertSTATE2.setBounds(210, 160, 200, 35);
InsertBUY2 = new JTextField();
InsertBUY2.setFont(new Font("宋体", 1, 23));
InsertBUY2.setBounds(210, 220, 200, 35);
InsertSELL2 = new JTextField();
InsertSELL2.setFont(new Font("宋体", 1, 23));
InsertSELL2.setBounds(210, 280, 200, 35);
// jp2 上的文本框
DeleteID2 = new JTextField("输入要删除物品的编号");
DeleteID2.setFont(new Font("楷体", 1, 25));
DeleteID2.setBounds(210, 100, 350, 50);
// jp3 上的文本框
UpdateID2 = new JTextField();
UpdateID2.setFont(new Font("楷体", 1, 20));
UpdateID2.setBounds(310, 60, 200, 45);
UpdateContent = new JTextField("更新内容");
UpdateContent.setFont(new Font("楷体", 0, 22));
UpdateContent.setBounds(310, 200, 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);// 当需要水平滚动条时显示
IDCondition = new JTextField();
IDCondition.setFont(new Font("宋体", 1, 18));
IDCondition.setBounds(90, 300, 100, 21);
NAMECondition = new JTextField();
NAMECondition.setFont(new Font("宋体", 1, 18));
NAMECondition.setBounds(90, 330, 100, 21);
STATECondition = new JTextField();
STATECondition.setFont(new Font("宋体", 1, 18));
STATECondition.setBounds(90, 360, 100, 21);
BUYCondition = new JTextField();
BUYCondition.setFont(new Font("宋体", 1, 18));
BUYCondition.setBounds(90, 390, 100, 21);
SELLCondition = new JTextField();
SELLCondition.setFont(new Font("宋体", 1, 18));
SELLCondition.setBounds(90, 420, 100, 21);
IDCondition.setEditable(false);
NAMECondition.setEditable(false);
STATECondition.setEditable(false);
BUYCondition.setEditable(false);
SELLCondition.setEditable(false);
}
// 设置各面板信息的方法
private void setPanel() {
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jp4 = new JPanel();
// jp5 = new JPanel();
// jp6 = new JPanel();
}
// 设置布局信息的方法
private void setLayout() {
// 添加 jp1 的组件
jp1.setLayout(null);
jp1.add(InsertRecord);
jp1.add(InsertReset);
jp1.add(InsertID1);
jp1.add(InsertNAME1);
jp1.add(InsertSTATE1);
jp1.add(InsertBUY1);
jp1.add(InsertSELL1);
jp1.add(InsertID2);
jp1.add(InsertNAME2);
jp1.add(InsertSTATE2);
jp1.add(InsertBUY2);
jp1.add(InsertSELL2);
// 添加 jp2 上的组件
jp2.setLayout(null);
jp2.add(DeleteID1);
jp2.add(DeleteID2);
jp2.add(DeleteRecord);
jp2.add(DeleteReset);
// 添加 jp3 上的组件
jp3.setLayout(null);
jp3.add(UpdateID1);
jp3.add(UpdateID2);
jp3.add(UpdateItem);
jp3.add(UpdateContent);
jp3.add(UpdateRecord);
jp3.add(UpdateReset);
jp4.setLayout(null);
jp4.add(scroll);
jp4.add(QueryRecord);
jp4.add(ID);
jp4.add(NAME);
jp4.add(STATE);
jp4.add(BUY);
jp4.add(SELL);
jp4.add(IDCondition);
jp4.add(NAMECondition);
jp4.add(STATECondition);
jp4.add(BUYCondition);
jp4.add(SELLCondition);
}
// 设置选项卡信息的方法
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/StudentInfo?"
+ "useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC");
// 加载驱动
db.setDbdriver("com.mysql.cj.jdbc.Driver");
// 这里的用户名和密码是要和你的 mysql 对应的,也是唯一需要更改的地方
db.setUsername("root");
db.setPassword("221234");
}
// 初始化
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);
CourseQuery.addMouseListener(this);
GradeQuery.addMouseListener(this);
// 给各复选按钮添加监听器
ID.addItemListener(this);
NAME.addItemListener(this);
STATE.addItemListener(this);
BUY.addItemListener(this);
SELL.addItemListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
// 添加按钮功能
// 点击重置键则清空文本框
if (e.getSource().equals(InsertReset)) {
InsertID2.setText("");
InsertID2.setFont(new Font("宋体", 1, 23));
InsertNAME2.setText("");
InsertNAME2.setFont(new Font("宋体", 1, 23));
InsertSTATE2.setText("");
InsertSTATE2.setFont(new Font("宋体", 1, 23));
InsertBUY2.setText("");
InsertBUY2.setFont(new Font("宋体", 1, 23));
InsertSELL2.setText("");
InsertSELL2.setFont(new Font("宋体", 1, 23));
} else if (e.getSource().equals(InsertRecord)) {
// 添加记录功能
String InsertStuID = InsertID2.getText();
String InsertStuName = InsertNAME2.getText();
String InsertStuChinese = InsertSTATE2.getText();
String InsertStuMath = InsertBUY2.getText();
String InsertStuEnglish = InsertSELL2.getText();
try {
db.setRs(db.executeQuery(InsertStuID));
if (!db.getRs().next()) {
db.executeInsert(InsertStuID, InsertStuName, InsertStuChinese, InsertStuMath, InsertStuEnglish);
JOptionPane.showOptionDialog(this, "添加信息成功!", "数据库操作提示",
JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
} 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)) {
// 删除重置功能
DeleteID2.setText("");
DeleteID2.setFont(new Font("楷体", 1, 25));
}
else if (e.getSource().equals(DeleteRecord)) {
// 删除功能
String DeleteStuID = DeleteID2.getText();
try {
db.setRs(db.executeQuery(DeleteStuID));
if (db.getRs().next()) {
db.executeDelete(DeleteStuID);
JOptionPane.showOptionDialog(this, "删除成功!", "数据库操作提示",
-1, 1, null, null, null);
}
else JOptionPane.showOptionDialog(this, "删除失败", "温馨提示",
-1, 1, null, null, null);
}
catch (Exception exception) {
exception.printStackTrace();
}
}
else if (e.getSource().equals(UpdateReset)) {
// 重置更新框功能
UpdateID2.setText("");
UpdateID2.setFont(new Font("宋体", 1, 20));
UpdateContent.setText("");
UpdateContent.setFont(new Font("宋体", 1, 20));
}
else if (e.getSource().equals(UpdateRecord)) {
// 完成更新功能
String UpdateStuID = UpdateID2.getText();
try {
db.setRs(db.executeQuery(UpdateStuID));
if (!db.getRs().next()) {
JOptionPane.showOptionDialog(this, "没有记录无法更新",
"温馨提示", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, null, null);
}
else {
String updateItem = null;
if (UpdateItem.getSelectedItem().toString().equals("商品名称")) {
updateItem = "NAME";
}
else if (UpdateItem.getSelectedItem().toString().equals("新旧程度")) {
updateItem = "STATE";
}
else if (UpdateItem.getSelectedItem().toString().equals("买入价格")) {
updateItem = "BUY";
}
else if (UpdateItem.getSelectedItem().toString().equals("出售价格")) {
updateItem = "SELL";
}
db.executeUpdate(UpdateStuID, updateItem, UpdateContent.getText());
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(QueryRecord)) {
// 完成查询功能
try {
// 默认设置各检索条件均为通配符
String a = "%", b = "%", c = "%", d = "%", f = "%";
// 如果 ID 选项被选中,则获得该选项的输入内容
if (ID.isSelected() && !IDCondition.getText().trim().isEmpty()) {
a = IDCondition.getText();
}
// 如果 Name 选项被选中,则获得该选项的输入内容
if (NAME.isSelected() && !NAMECondition.getText().trim().isEmpty()) {
b = NAMECondition.getText();
}
// 如果 Math 选项被选中,则获得该选项的输入内容
if (BUY.isSelected() && !BUYCondition.getText().trim().isEmpty()) {
d = BUYCondition.getText();
}
// 如果 English 选项被选中,则获得该选项的输入内容
if (SELL.isSelected() && !SELLCondition.getText().trim().isEmpty()) {
f = SELLCondition.getText();
}
// 如果 Chinese 选项被选中,则获得该选项的输入内容
if (STATE.isSelected() && !STATECondition.getText().trim().isEmpty()) {
c = STATECondition.getText();
}
// 根据各选项检索关键字进行查询,并返回结果集
db.setRs(db.executeQueryByCondition(a, b, c, d, f));
// 定义结果集中记录条数
int i = 0;
QueryRecordResult.setText("查询结果:");
// 输出结果集记录
while (db.getRs().next()) {
++i;
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--------------------------------------"));
}
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) {
if (e.getSource().equals(ID)) {
IDCondition.setEditable(ID.isSelected());
}
else if (e.getSource().equals(NAME)) {
NAMECondition.setEditable(NAME.isSelected());
}
else if (e.getSource().equals(STATE)) {
STATECondition.setEditable(STATE.isSelected());
}
else if (e.getSource().equals(BUY)) {
BUYCondition.setEditable(BUY.isSelected());
}
else if (e.getSource().equals(SELL)) {
SELLCondition.setEditable(SELL.isSelected());
}
}
}