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、用户名和密码需要根据实际情况进行修改。

相关推荐
L-影2 分钟前
单体项目结构
java·开发语言
wuqingshun31415923 分钟前
MySQL 如何解决深度分页问题?
java·mysql·面试
buhuizhiyuci30 分钟前
【算法篇】位运算 —— 基础篇
java·数据库·算法
川石课堂软件测试34 分钟前
安全测试|服务器安全加固方法
服务器·功能测试·测试工具·jmeter·mysql·web安全·单元测试
laboratory agent开发1 小时前
企业AI Agent落地前,先回答四个工程问题
java·前端·人工智能
微三云 - 廖会灵 (私域系统开发)1 小时前
电商系统国际化架构设计:多语言、多币种、多时区、多税制的全链路实现
java·前端·数据库
XS0301061 小时前
Spring AI 第二课-流式输出 & 运行时动态参数配置
java·人工智能·spring
额恩661 小时前
阶段五:HttpOnly Cookie 登录持久化
java·spring
唐青枫1 小时前
Java Netty 实战指南:从 NIO 线程模型到 TCP 编解码和心跳机制
java
CoderYanger2 小时前
A.每日一题:1967题+1331题
java·程序人生·算法·leetcode·面试·职场和发展·学习方法