javagui 做一个 用java和MySQL做停车场管理系统

下面是一个用Java和MySQL编写的停车场管理系统的示例代码:

java 复制代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class ParkingManagementSystem extends JFrame {
    private JLabel carNumberLabel;
    private JTextField carNumberTextField;
    private JButton enterButton;
    private JButton exitButton;

    private Connection connection;

    public ParkingManagementSystem() {
        setTitle("停车场管理系统");
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        carNumberLabel = new JLabel("车牌号:");
        carNumberTextField = new JTextField(10);
        enterButton = new JButton("进入");
        exitButton = new JButton("离开");

        enterButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String carNumber = carNumberTextField.getText();
                enter(carNumber);
            }
        });

        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String carNumber = carNumberTextField.getText();
                exit(carNumber);
            }
        });

        add(carNumberLabel);
        add(carNumberTextField);
        add(enterButton);
        add(exitButton);

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/parking", "username", "password");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void enter(String carNumber) {
        try {
            PreparedStatement statement = connection.prepareStatement("INSERT INTO parking_cars (car_number, enter_time) VALUES (?, NOW())");
            statement.setString(1, carNumber);
            statement.executeUpdate();
            JOptionPane.showMessageDialog(null, "车辆进入停车场");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private void exit(String carNumber) {
        try {
            PreparedStatement statement = connection.prepareStatement("UPDATE parking_cars SET exit_time = NOW() WHERE car_number = ? AND exit_time IS NULL");
            statement.setString(1, carNumber);
            int rowsAffected = statement.executeUpdate();
            if (rowsAffected > 0) {
                JOptionPane.showMessageDialog(null, "车辆离开停车场");
            } else {
                JOptionPane.showMessageDialog(null, "未找到该车辆或该车辆已离开停车场");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ParkingManagementSystem().setVisible(true);
            }
        });
    }
}

上述代码使用了Java的Swing库创建图形用户界面。它包含一个文本框用于输入车牌号码,一个"进入"按钮和一个"离开"按钮。当用户点击"进入"按钮时,程序将将车牌号码和当前时间插入到MySQL数据库的parking_cars表中。当用户点击"离开"按钮时,程序将更新parking_cars表中与输入车牌号码匹配且exit_time为空的记录的exit_time字段,并给出相应的提示。

请注意,上述代码中的数据库连接URL、用户名和密码需要根据实际情况进行修改。

相关推荐
没有bug.的程序员9 小时前
服务网格 Service Mesh:微服务通信的终极进化
java·分布式·微服务·云原生·service_mesh
川石课堂软件测试9 小时前
MySQL数据库之DBA命令
数据库·网络协议·mysql·http·单元测试·prometheus·dba
ybb_ymm11 小时前
mysql8在linux下的默认规则修改
linux·运维·数据库·mysql
南尘NCA866612 小时前
企业微信防封防投诉拦截系统:从痛点解决到技术实现
java·网络·企业微信
程序新视界12 小时前
为什么要尽量将MySQL表字段要设置为NOT NULL?
数据库·mysql·dba
怪兽201412 小时前
SQL优化手段有哪些
java·数据库·面试
ss27312 小时前
手写MyBatis第107弹:@MapperScan原理与SqlSessionTemplate线程安全机制
java·开发语言·后端·mybatis
Deschen13 小时前
设计模式-原型模式
java·设计模式·原型模式
麦麦鸡腿堡13 小时前
Java的动态绑定机制(重要)
java·开发语言·算法