下面我会给出一份详细的攻略,帮助你快速了解如何通过使用Eclipse、Java、Swing和Mysql来实现电影购票系统。
准备工作
- 安装 JDK 和 Eclipse
- 下载该电影购票系统所需的Java类库和驱动程序
mysql-connector-java-5.1.47-bin.jar
,并在Eclipse的项目中添加这些类库 - 搭建Mysql数据库
设计数据库
在Mysql中创建数据库,在该数据库中创建三张表格来存储所有电影、影院、和订单信息。三张表格的设计如下:
- movies表:
属性名 | 类型 | 约束 |
---|---|---|
id | int | primary key |
name | varchar(50) | not null |
director | varchar(50) | not null |
actors | varchar(100) | not null |
release_date | date | not null |
rating | decimal(3,1) | not null |
description | varchar(500) |
- cinemas表:
属性名 | 类型 | 约束 |
---|---|---|
id | int | primary key |
name | varchar(50) | not null |
address | varchar(100) | not null |
phone | varchar(20) | not null |
- orders表:
属性名 | 类型 | 约束 |
---|---|---|
id | int | primary key |
movie_id | int | foreign key(movies.id) |
cinema_id | int | foreign key(cinemas.id) |
time | datetime | not null |
user_name | varchar(50) |
代码实现
连接数据库
在程序入口Main.java
中,通过如下代码来连接数据库:
Class.forName("com.mysql.jdbc.Driver"); // 加载Mysql驱动
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/movie_ticket_system", "root", ""); // 建立连接
查询电影和电影院信息
通过如下代码来查询所有电影和电影院信息:
// 查询所有电影信息
PreparedStatement moviesStmt = conn.prepareStatement("SELECT * FROM movies ORDER BY id");
ResultSet moviesRs = moviesStmt.executeQuery();
while (moviesRs.next()) {
int id = moviesRs.getInt("id");
String name = moviesRs.getString("name");
String director = moviesRs.getString("director");
String actors = moviesRs.getString("actors");
Date release_date = moviesRs.getDate("release_date");
BigDecimal rating = moviesRs.getBigDecimal("rating");
String description = moviesRs.getString("description");
// Add movie to moviesList
}
// 查询所有电影院信息
PreparedStatement cinemasStmt = conn.prepareStatement("SELECT * FROM cinemas ORDER BY id");
ResultSet cinemasRs = cinemasStmt.executeQuery();
while (cinemasRs.next()) {
int id = cinemasRs.getInt("id");
String name = cinemasRs.getString("name");
String address = cinemasRs.getString("address");
String phone = cinemasRs.getString("phone");
// Add cinema to cinemasList
}
插入订单信息
将用户购买电影票的订单信息插入到orders表中,可使用类似如下代码实现:
PreparedStatement orderStmt = conn.prepareStatement("INSERT INTO orders (movie_id, cinema_id, time, user_name)" + " VALUES (?, ?, ?, ?)");
orderStmt.setInt(1, selectedMovieId);
orderStmt.setInt(2, selectedCinemaId);
orderStmt.setTimestamp(3, new java.sql.Timestamp(currentDate.getTime()));
orderStmt.setString(4, nameText.getText().trim());
orderStmt.executeUpdate();
示例说明
- 示例1:如何添加/修改/删除电影
在程序入口中,通过Swing的GUI设计一个添加/修改/删除电影的界面。在addMovieButton、updateMovieButton、deleteMovieButton的ActionListener中,分别执行SQL语句实现添加、修改、删除电影操作。示例代码如下:
- 添加电影
public void actionPerformed(ActionEvent e) {
try {
String name = nameField.getText();
String director = directorField.getText();
String actors = actorsField.getText();
Date release_date = releaseDateChooser.getDate();
BigDecimal rating = BigDecimal.valueOf(Double.parseDouble(ratingField.getText()));
String description = descriptionField.getText();
PreparedStatement stmt = conn.prepareStatement("INSERT INTO movies (name, director, actors, release_date, rating, description) VALUES (?, ?, ?, ?, ?, ?)");
stmt.setString(1, name);
stmt.setString(2, director);
stmt.setString(3, actors);
stmt.setDate(4, new java.sql.Date(release_date.getTime()));
stmt.setBigDecimal(5, rating);
stmt.setString(6, description);
stmt.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
}
- 修改电影
public void actionPerformed(ActionEvent e) {
try {
String name = nameField.getText();
String director = directorField.getText();
String actors = actorsField.getText();
Date release_date = releaseDateChooser.getDate();
BigDecimal rating = BigDecimal.valueOf(Double.parseDouble(ratingField.getText()));
String description = descriptionField.getText();
PreparedStatement stmt = conn.prepareStatement("UPDATE movies SET name=?, director=?, actors=?, release_date=?, rating=?, description=? WHERE id=?");
stmt.setString(1, name);
stmt.setString(2, director);
stmt.setString(3, actors);
stmt.setDate(4, new java.sql.Date(release_date.getTime()));
stmt.setBigDecimal(5, rating);
stmt.setString(6, description);
stmt.setInt(7, selectedMovie.getId());
stmt.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
}
- 删除电影
public void actionPerformed(ActionEvent e) {
try {
PreparedStatement stmt = conn.prepareStatement("DELETE FROM movies WHERE id=?");
stmt.setInt(1, selectedMovie.getId());
stmt.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
}
- 示例2:如何添加电影院
在程序入口中,通过Swing的GUI设计一个添加电影院的界面。在addCinemaButton的ActionListener中,执行SQL语句实现添加电影院操作。示例代码如下:
public void actionPerformed(ActionEvent e) {
try {
String name = nameField.getText();
String address = addressField.getText();
String phone = phoneField.getText();
PreparedStatement stmt = conn.prepareStatement("INSERT INTO cinemas (name, address, phone) VALUES (?, ?, ?)");
stmt.setString(1, name);
stmt.setString(2, address);
stmt.setString(3, phone);
stmt.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
}
总结
在上述攻略中,我们通过Eclipse、Java、Swing和Mysql实现了一个电影购票系统,并探讨了如何连接数据库、查询电影和电影院信息以及插入订单信息。我们同时给出了两个示例说明,帮助读者更好地理解这个电影购票系统的实现过程。由此可见,使用Eclipse、Java、Swing和Mysql来实现类似的应用程序是一种非常有效和方便的方法。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Eclipse+Java+Swing+Mysql实现电影购票系统(详细代码) - Python技术站