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


前言

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


一、项目设计

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);
    }

    @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("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.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;
 }
 */
}

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();
        // 连接 mysql
        db.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);
    }

    @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());
        }
    }
}

谢谢观看!!

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值