基于Java swing + MySQL电影院订票与管理系统,分为客户端和服务端
由于问题是关于代码的,我将提供一个简化的示例,展示如何在Java中使用Swing和MySQL创建一个简单的电影票管理系统。
服务器端代码(MySQL数据库连接和简单的数据库操作):
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class MovieTicketManager {
private Connection connect = null;
private PreparedStatement preparedStatement = null;
public MovieTicketManager() {
try {
// 加载MySQL JDBC驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
// 设置数据库连接字符串,用户名和密码
String connectionString = "jdbc:mysql://localhost:3306/movie_db?user=root&password=root";
// 建立连接
connect = DriverManager.getConnection(connectionString);
} catch (Exception e) {
e.printStackTrace();
}
}
public void bookTicket(String movieName, int seatNumber) {
String sql = "INSERT INTO tickets (movie_name, seat_number) VALUES (?, ?)";
try {
preparedStatement = connect.prepareStatement(sql);
preparedStatement.setString(1, movieName);
preparedStatement.setInt(2, seatNumber);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
客户端代码(Swing界面):
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MovieTicketClientUI extends JFrame {
private JTextField movieNameField;
private JTextField seatNumberField;
private JButton bookButton;
public MovieTicketClientUI() {
initComponents();
setSize(300, 200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void initComponents() {
movieNameField = new JTextField(10);
seatNumberField = new JTextField(10);
bookButton = new JButton("Book Ticket");
bookButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String movieName = movieNameField.getText();
int seatNumber = Integer.parseInt(seatNumberField.getText());
// 假设MovieTicketManager已经初始化并连接到数据库
MovieTicketManager movieTicketManager = new MovieTicketManager();
movieTicketManager.bookTicket(movieName, seatNum
评论已关闭