基于Java+Swing+Mysql影院购票系统
发布人:shili8
发布时间:2024-11-20 08:45
阅读次数:0
**影院购票系统**
### 系统概述本系统是一个基于Java、Swing和MySQL的影院购票系统。该系统允许用户在线预订电影票,管理影院信息和电影信息。
### 系统功能1. **用户登录注册**: 用户可以注册账号或使用已有的账号登录系统。
2. **电影信息管理**: 管理员可以添加、修改、删除电影信息。
3. **影院信息管理**: 管理员可以添加、修改、删除影院信息。
4. **预订电影票**: 用户可以在线预订电影票,选择座位和支付费用。
5. **订单管理**: 系统会自动记录用户的订单信息。
### 系统设计#### 数据库设计我们使用MySQL数据库来存储系统的数据。以下是数据库表结构:
sqlCREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE `movie` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `description` text NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE `cinema` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `address` varchar(255) NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE `order` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `movie_id` int(11) NOT NULL, `cinema_id` int(11) NOT NULL, `seat` varchar(255) NOT NULL, `price` decimal(10,2) NOT NULL, PRIMARY KEY (`id`) );
#### Java代码以下是系统的Java代码:
javaimport javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class MainFrame extends JFrame { private JTabbedPane tabbedPane = new JTabbedPane(); public MainFrame() { super("影院购票系统"); setSize(800,600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 添加登录注册界面 JComponent loginPanel = new LoginPanel(); tabbedPane.addTab("登录", loginPanel); // 添加电影信息管理界面 JComponent moviePanel = new MoviePanel(); tabbedPane.addTab("电影信息管理", moviePanel); // 添加影院信息管理界面 JComponent cinemaPanel = new CinemaPanel(); tabbedPane.addTab("影院信息管理", cinemaPanel); // 添加预订电影票界面 JComponent orderPanel = new OrderPanel(); tabbedPane.addTab("预订电影票", orderPanel); add(tabbedPane); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { MainFrame frame = new MainFrame(); frame.setVisible(true); } }); } } class LoginPanel extends JPanel { private JTextField usernameField; private JPasswordField passwordField; public LoginPanel() { super(); // 添加用户名和密码输入框 JLabel usernameLabel = new JLabel("用户名:"); usernameField = new JTextField(20); JLabel passwordLabel = new JLabel("密码:"); passwordField = new JPasswordField(20); JButton loginButton = new JButton("登录"); loginButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 验证用户名和密码 String username = usernameField.getText(); String password = new String(passwordField.getPassword()); // 连接数据库 Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456"); // 查询用户信息 PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM user WHERE username=? AND password=?"); pstmt.setString(1, username); pstmt.setString(2, password); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { // 登录成功 JOptionPane.showMessageDialog(this, "登录成功!"); } else { // 登录失败 JOptionPane.showMessageDialog(this, "登录失败!"); } } catch (Exception ex) { // 处理异常 JOptionPane.showMessageDialog(this, "登录失败!"); } finally { try { conn.close(); } catch (SQLException e1) { // ignore } } } }); add(usernameLabel); add(usernameField); add(passwordLabel); add(passwordField); add(loginButton); } } class MoviePanel extends JPanel { private JTable movieTable; public MoviePanel() { super(); // 添加电影信息表格 String[] columnNames = {"电影名称", "电影描述"}; Object[][] data = {{"《哈利波特》", "一部奇幻冒险片"}, {"《星际旅行》", "一部科幻片"}}; movieTable = new JTable(data, columnNames); // 添加添加、修改、删除按钮 JButton addButton = new JButton("添加"); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 添加电影信息 String name = JOptionPane.showInputDialog(this, "请输入电影名称:"); String description = JOptionPane.showInputDialog(this, "请输入电影描述:"); // 连接数据库 Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456"); // 插入数据 PreparedStatement pstmt = conn.prepareStatement("INSERT INTO movie (name, description) VALUES (?, ?)"); pstmt.setString(1, name); pstmt.setString(2, description); pstmt.executeUpdate(); // 刷新表格 DefaultTableModel model = (DefaultTableModel) movieTable.getModel(); model.addRow(new Object[]{name, description}); } catch (Exception ex) { // 处理异常 JOptionPane.showMessageDialog(this, "添加失败!"); } finally { try { conn.close(); } catch (SQLException e1) { // ignore } } } }); JButton modifyButton = new JButton("修改"); modifyButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 修改电影信息 int row = movieTable.getSelectedRow(); if (row != -1) { String name = JOptionPane.showInputDialog(this, "请输入新电影名称:"); String description = JOptionPane.showInputDialog(this, "请输入新电影描述:"); // 连接数据库 Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456"); // 更新数据 PreparedStatement pstmt = conn.prepareStatement("UPDATE movie SET name=?, description=? WHERE id=?"); pstmt.setString(1, name); pstmt.setString(2, description); int id = (int) movieTable.getValueAt(row,0); pstmt.setInt(3, id); pstmt.executeUpdate(); // 刷新表格 DefaultTableModel model = (DefaultTableModel) movieTable.getModel(); model.setValueAt(name, row,0); model.setValueAt(description, row,1); } catch (Exception ex) { // 处理异常 JOptionPane.showMessageDialog(this, "修改失败!"); } finally { try { conn.close(); } catch (SQLException e1) { // ignore } } } } }); JButton deleteButton = new JButton("删除"); deleteButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 删除电影信息 int row = movieTable.getSelectedRow(); if (row != -1) { // 连接数据库 Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456"); // 删除数据 PreparedStatement pstmt = conn.prepareStatement("DELETE FROM movie WHERE id=?"); int id = (int) movieTable.getValueAt(row,0); pstmt.setInt(1, id); pstmt.executeUpdate(); // 刷新表格 DefaultTableModel model = (DefaultTableModel) movieTable.getModel(); model.removeRow(row); } catch (Exception ex) { // 处理异常 JOptionPane.showMessageDialog(this, "删除失败!"); } finally { try { conn.close(); } catch (SQLException e1) { // ignore } } } } }); add(movieTable); add(addButton); add(modifyButton); add(deleteButton); } } class CinemaPanel extends JPanel { private JTable cinemaTable; public CinemaPanel() { super(); // 添加电影院信息表格 String[] columnNames = {"电影院名称", "地址"};