计算机软件实习日志(四)校园二手物品信息管理系统

本文主要是介绍计算机软件实习日志(四)校园二手物品信息管理系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 前言
    • 一、项目设计
        • 1.1 开发环境
        • 1.2 项目要求
        • 1.3 项目功能
    • 二、公共类的设计
    • 三.设计流程
    • 四、对数据库进行测试
    • 五、源代码
    • 谢谢观看!!


前言

在现实生活中,越来越多的人对自己的二手物品具有出售的愿望,利用传统的方式进行出售存在一定的局限性。利用网络这个巨大的资源可以构建出一个廉价方便的交易平台,并找到广泛的顾客群,以此来达到出售目的。伴随电子商务的迅猛发展,网上二手商品交易管理系统应运而生。


一、项目设计

1.1 开发环境

【1】系统开发平台:
编程环境:IDEA
数据库:MySQL Workbench 8.0 CE

【2】系统开发语言:
JAVA

【3】运行平台:
IDEA

1.2 项目要求

1、程序对物品基本信息进行管理,包括数据的添加、修改删除和浏览;能够对商品进行管理。
2、在添加商品基本信息相关数据时,编号不能重复还有在添加学生选课信息时,要求信息需要完整。
3、应用程序提供操作界面,可以方便用户进行功能选择,实现信息的管理和查询, 并可以清晰地显示相关信息

1.3 项目功能

功能图
在这里插入图片描述

【1】 增添功能
输入商品的完整信息才能完成数据的增添。

【2】删除功能
用于对指定编号的商品信息进行删除;

【3】 修改功能
对于指定编号的商品的四类主要信息(商品名称、新旧程度、买入价格、出售价格)进行修改。

【4】 查找功能
可根据数据库中的表中的的属性来查询表内对应信息,可以根据编号、商品名称、新旧程度、价格等等,来查询对应的数据。

二、公共类的设计

在这里插入图片描述
在这里插入图片描述

三.设计流程

1、创建一个market数据库
在这里插入图片描述

2、创建一个goods表
在这里插入图片描述
3、数据库创建完成
在这里插入图片描述

四、对数据库进行测试

1、添加信息操作,
输入编号(不能重复)以及其他商品的基本信息;
在这里插入图片描述
在这里插入图片描述
2、查询信息:
点击查询,找到了刚刚添加的充电宝商品以及基本信息:
在这里插入图片描述
可以根据商品的(编号、新旧程度、买入、卖出价格来查询)
在这里插入图片描述
3、更新信息:
通过输入编号、对指定的属性进行修改:
测试:对编号为12的商品把新旧程度从5修改为10;
在这里插入图片描述
查询信息验证更新是否正确:
可以看到编号为12的充电宝的新旧程度已经更改为10;
在这里插入图片描述
4、删除信息:
输入商品的编号点击删除就可以成功删除该数据;
在这里插入图片描述

五、源代码

StartMySql.java代码如下:

package datebase;
public class StartMySql {// 启动登录界面public static void main(String[] args) {new Login();}
}

Login.java代码如下:

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);}@Overridepublic 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("952666")) {MySQLGUI myS = new MySQLGUI();myS.initial();jf.setVisible(false);jf.dispose();}// 如果用户名和密码不匹配,则给出提示对话框else {JOptionPane.showOptionDialog(jf, "用户名或密码错误", "登陆失败",JOptionPane.CLOSED_OPTION,JOptionPane.ERROR_MESSAGE, null, null, null);}}}
}

OperationMySql.java代码如下:

package datebase;
//OperationMySql.javaimport java.sql.*;public class OperationMySql {// 定义数据库连接urlprivate 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;}// 返回当前实例数据库连接urlpublic 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;}*/
}

MySQLGUI.java代码如下:


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();// 连接 mysqldb.setDburl("jdbc:mysql://localhost:3306/market?"+ "useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC");// 加载驱动db.setDbdriver("com.mysql.cj.jdbc.Driver");// 这里的用户名和密码是要和你的 mysql 对应的,也是唯一需要更改的地方db.setUsername("root");db.setPassword("952666");}// 初始化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);}@Overridepublic 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();}}}@Overridepublic void mousePressed(MouseEvent e) {}@Overridepublic void mouseReleased(MouseEvent e) {}@Overridepublic void mouseEntered(MouseEvent e) {}@Overridepublic void mouseExited(MouseEvent e) {}@Overridepublic 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());}}
}

谢谢观看!!

这篇关于计算机软件实习日志(四)校园二手物品信息管理系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/746528

相关文章

SpringBoot日志配置SLF4J和Logback的方法实现

《SpringBoot日志配置SLF4J和Logback的方法实现》日志记录是不可或缺的一部分,本文主要介绍了SpringBoot日志配置SLF4J和Logback的方法实现,文中通过示例代码介绍的非... 目录一、前言二、案例一:初识日志三、案例二:使用Lombok输出日志四、案例三:配置Logback一

golang 日志log与logrus示例详解

《golang日志log与logrus示例详解》log是Go语言标准库中一个简单的日志库,本文给大家介绍golang日志log与logrus示例详解,感兴趣的朋友一起看看吧... 目录一、Go 标准库 log 详解1. 功能特点2. 常用函数3. 示例代码4. 优势和局限二、第三方库 logrus 详解1.

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

SpringBoot项目使用MDC给日志增加唯一标识的实现步骤

《SpringBoot项目使用MDC给日志增加唯一标识的实现步骤》本文介绍了如何在SpringBoot项目中使用MDC(MappedDiagnosticContext)为日志增加唯一标识,以便于日... 目录【Java】SpringBoot项目使用MDC给日志增加唯一标识,方便日志追踪1.日志效果2.实现步

SQL Server清除日志文件ERRORLOG和删除tempdb.mdf

《SQLServer清除日志文件ERRORLOG和删除tempdb.mdf》数据库再使用一段时间后,日志文件会增大,特别是在磁盘容量不足的情况下,更是需要缩减,以下为缩减方法:如果可以停止SQLSe... 目录缩减 ERRORLOG 文件(停止服务后)停止 SQL Server 服务:找到错误日志文件:删除

grom设置全局日志实现执行并打印sql语句

《grom设置全局日志实现执行并打印sql语句》本文主要介绍了grom设置全局日志实现执行并打印sql语句,包括设置日志级别、实现自定义Logger接口以及如何使用GORM的默认logger,通过这些... 目录gorm中的自定义日志gorm中日志的其他操作日志级别Debug自定义 Loggergorm中的

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

Spring Boot整合log4j2日志配置的详细教程

《SpringBoot整合log4j2日志配置的详细教程》:本文主要介绍SpringBoot项目中整合Log4j2日志框架的步骤和配置,包括常用日志框架的比较、配置参数介绍、Log4j2配置详解... 目录前言一、常用日志框架二、配置参数介绍1. 日志级别2. 输出形式3. 日志格式3.1 PatternL

开启mysql的binlog日志步骤详解

《开启mysql的binlog日志步骤详解》:本文主要介绍MySQL5.7版本中二进制日志(bin_log)的配置和使用,文中通过图文及代码介绍的非常详细,需要的朋友可以参考下... 目录1.查看是否开启bin_log2.数据库会把日志放进logs目录中3.查看log日志总结 mysql版本5.71.查看

C++中实现调试日志输出

《C++中实现调试日志输出》在C++编程中,调试日志对于定位问题和优化代码至关重要,本文将介绍几种常用的调试日志输出方法,并教你如何在日志中添加时间戳,希望对大家有所帮助... 目录1. 使用 #ifdef _DEBUG 宏2. 加入时间戳:精确到毫秒3.Windows 和 MFC 中的调试日志方法MFC