文档说明:
一、语言和环境
1. 实现语言:Java。
2. 开发环境:MyEclipse+MySQL。
二、实现功能
使用 Java 技术开发一个影院售票系统,具体实现功能如下:
1. 程序启动显示主界面,点击查询按钮,显示所有影片信息,如图 1 所示。
图 1 影片信息列表
2. 点击“新增”按钮,弹出新增影片窗口,可以录入新增影片相关信息,如图 2 所示
图 2 录入新增影片
3. 在新增影片窗口,点击“新增”按钮,将影片信息保存到数据库中,保存成功给予
用户提示,并清空所有文本框内容,点击“返回”按钮则关闭窗口。在影片列表窗
口显示最新添加的影片信息。如图 3 所示。
图 3 新增影片成功提示
三、数据库设计
1. 创建数据库(MovieDB)
2. 创建影片信息表(movies),结构如下:
四、具体要求及推荐实现步骤
1. 按以上数据库要求建库建表,并参考图 1 添加至少 3 条测试数据
2. 搭建系统框架
(1)编写实体类。
(2)创建数据访问层,并编写 BaseDAO 和数据访问层代码。
(3)创建窗体。
3. 创建主窗体
(1)按照图 1,创建窗体 MainFrame,并添加相应的组件
(2)表格中的数据均来源于数据库,点击“查询”按钮显示所有数据。
(3)实现点击“新增”按钮,显示新增影片窗体。
4. 创建新增影片信息窗体第 3 页 共 3 页
(1)按照图 2,创建窗体 AddFrame,并添加相应的组件。
(2)实现点击“新增”按钮,将影片信息保存至数据库中。
(3)新增完毕,提示用户,并清空文本框输入内容。
(4)实现点击“返回”按钮,关闭新增影片信息窗口。
五、注意事项
1. 将数据库导出成 sql 脚本文件
2. 将整个项目打包提交
六、评分标准
实现代码:
一、数据库:
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for movies -- ---------------------------- DROP TABLE IF EXISTS `movies`; CREATE TABLE `movies` ( `movieID` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(64) NOT NULL, `duration` int(11) NOT NULL, `area` varchar(32) DEFAULT NULL, `date` date DEFAULT NULL, `price` decimal(6,2) DEFAULT NULL, PRIMARY KEY (`movieID`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of movies -- ---------------------------- INSERT INTO `movies` VALUES ('1', '复仇者联盟4', '181', '美国', '2019-05-24', '59.00'); INSERT INTO `movies` VALUES ('2', '大侦探皮卡丘', '104', '美国', '2019-06-10', '39.00'); INSERT INTO `movies` VALUES ('3', '反贪风暴4', '98', '中国香港', '2019-05-04', '28.00'); INSERT INTO `movies` VALUES ('4', '撞死了一只羊', '87', '中国大陆', '2019-05-26', '25.00'); INSERT INTO `movies` VALUES ('5', '惊奇队长', '124', '美国', '2019-04-08', '48.00');
二、Java Swing:
com.ynavc.Bean
Movies.java
package com.ynavc.Base; public class Movies { int movieId; String name; int duration; String area; String date; double price; public Movies(int movieId, String name, int duration, String area, String date, double price) { super(); this.movieId = movieId; this.name = name; this.duration = duration; this.area = area; this.date = date; this.price = price; } public Movies() { super(); } public int getMovieId() { return movieId; } public void setMovieId(int movieId) { this.movieId = movieId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDuration() { return duration; } public void setDuration(int duration) { this.duration = duration; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
com.ynavc.Dao
DbConnection.java
package com.ynavc.Dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.swing.JOptionPane; import com.mysql.jdbc.Statement; public class DbConnection { //驱动类的类名 private static final String DRIVERNAME="com.mysql.jdbc.Driver"; //连接数据的URL路径 private static final String URL="jdbc:mysql://127.0.0.1:3306/moviedb"; //数据库登录账号 private static final String USER="root"; //数据库登录密码 private static final String PASSWORD="root123"; //加载驱动 static{ try { Class.forName(DRIVERNAME); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //获取数据库连接 public static Connection getConnection() { try { return DriverManager.getConnection(URL,USER,PASSWORD); } catch (SQLException e) { e.printStackTrace(); } return null; } //查询 public static ResultSet query(String sql) { System.out.println(sql); //获取连接 Connection connection=getConnection(); PreparedStatement psd; try { psd = connection.prepareStatement(sql); return psd.executeQuery(); } catch (SQLException e) { JOptionPane.showMessageDialog(null,"执行语句出错\n"+e.toString()); e.printStackTrace(); } return null; } //增、删、改、查 public static int updataInfo(String sql) { System.out.println(sql); //获取连接 Connection connection=getConnection(); try { PreparedStatement psd=connection.prepareStatement(sql); return psd.executeUpdate(); } catch (SQLException e) { JOptionPane.showMessageDialog(null,"执行语句出错\n"+e.toString()); e.printStackTrace(); } return 0; } //关闭连接 public static void colse(ResultSet rs,Statement stmt,Connection conn) throws Exception{ try { if (rs != null){ rs.close(); } if (stmt != null){ stmt.cancel(); } if (conn != null) { conn.close(); } } catch (Exception e) { e.printStackTrace(); throw new Exception(); } } }
com.ynavc.Controller
Update.java
package com.ynavc.Controller; import com.ynavc.Dao.DbConnection; public class Update { //添加数据 public int addData(String sql) { return DbConnection.updataInfo(sql); } }
Select.java
package com.ynavc.Controller; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import com.ynavc.Base.Movies; import com.ynavc.Dao.DbConnection; public class Select { public Object[][] getMianSelect() { String sql = "SELECT movieid,name,duration,area,date,price FROM movies;"; ResultSet re = DbConnection.query(sql); ArrayList<Movies> list = new ArrayList<Movies>(); try { while (re.next()) { Movies movies = new Movies(); movies.setMovieId(re.getInt(1)); movies.setName(re.getString(2)); movies.setDuration(re.getInt(3)); movies.setArea(re.getString(4)); movies.setDate(re.getString(5)); movies.setPrice(re.getDouble(6)); list.add(movies); } } catch (SQLException e) { e.printStackTrace(); } Object[][] ob = new Object[list.size()][6]; for (int i = 0; i < list.size(); i++) { ob[i][0] = list.get(i).getMovieId(); ob[i][1] = list.get(i).getName(); ob[i][2] = list.get(i).getDuration()+"分钟"; ob[i][3] = list.get(i).getArea(); ob[i][4] = list.get(i).getDate(); ob[i][5] = list.get(i).getPrice(); } return ob; } }
com.ynavc.Vive
MainFrame.Java
package com.ynavc.Vive; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import com.ynavc.Controller.Select; public class MainFrame extends JFrame { Select select = new Select(); Object[] header = {"序号","片名","时长","地区","上映时间","价格"}; Object[][] data = select.getMianSelect(); DefaultTableModel df = new DefaultTableModel(data,header); public MainFrame() { super("影视售票系统"); this.setBounds(0, 0, 700, 450); this.setLocationRelativeTo(null); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(null); JButton button1 = new JButton("查询"); button1.setBounds(349, 13, 100, 27); getContentPane().add(button1); button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Object[][] data = select.getMianSelect(); df.setDataVector(data,header); } }); JButton button2 = new JButton("增加"); button2.setBounds(494, 13, 100, 27); getContentPane().add(button2); button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new AddFrame().setVisible(true); } }); JTable jTable = new JTable(df); JScrollPane jsPane = new JScrollPane(jTable); jsPane.setBounds(25, 50, 650, 350); getContentPane().add(jsPane); } public static void main(String[] args) { new MainFrame().setVisible(true); } }
AddFrame.Java
package com.ynavc.Vive; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import com.ynavc.Base.Movies; import com.ynavc.Controller.Update; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; public class AddFrame extends JFrame { private JTextField textField_movieName; private JTextField textField_duration; private JTextField textField_area; private JTextField textField_releateDate; private JTextField textField_price; Update update = new Update(); public AddFrame() { super("新增影片信息"); this.setBounds(0, 0, 400, 450); this.setLocationRelativeTo(null); this.setResizable(false); getContentPane().setLayout(null); JLabel movieName = new JLabel("片名称"); movieName.setBounds(55, 59, 66, 18); getContentPane().add(movieName); textField_movieName = new JTextField(); textField_movieName.setBounds(150, 56, 183, 24); getContentPane().add(textField_movieName); textField_movieName.setColumns(10); JLabel duration = new JLabel("时长"); duration.setBounds(55, 100, 66, 18); getContentPane().add(duration); textField_duration = new JTextField(); textField_duration.setColumns(10); textField_duration.setBounds(150, 97, 183, 24); getContentPane().add(textField_duration); JLabel area = new JLabel("地区"); area.setBounds(55, 142, 66, 18); getContentPane().add(area); textField_area = new JTextField(); textField_area.setColumns(10); textField_area.setBounds(150, 139, 183, 24); getContentPane().add(textField_area); JLabel releaseDate = new JLabel("上映日期"); releaseDate.setBounds(55, 188, 66, 18); getContentPane().add(releaseDate); textField_releateDate = new JTextField(); textField_releateDate.setColumns(10); textField_releateDate.setBounds(150, 185, 183, 24); getContentPane().add(textField_releateDate); textField_releateDate.setText("2020-10-04"); JLabel price = new JLabel("票价"); price.setBounds(55, 232, 66, 18); getContentPane().add(price); textField_price = new JTextField(); textField_price.setColumns(10); textField_price.setBounds(150, 229, 183, 24); getContentPane().add(textField_price); JButton btnNewButton_add = new JButton("新增"); btnNewButton_add.setBounds(58, 309, 113, 27); getContentPane().add(btnNewButton_add); btnNewButton_add.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String name = textField_movieName.getText(); int duration = Integer.parseInt(textField_duration.getText()); String area = textField_area.getText(); String date = textField_releateDate.getText(); double price = Double.parseDouble(textField_price.getText()); String sql = "INSERT INTO `movies` VALUES ("+null+", '"+name+"', '"+duration+"', '"+area+"', '"+date+"', '"+price+"');"; int result = update.addData(sql); if (result>0) { JOptionPane.showMessageDialog(null, "新增影片成功!"); } else { JOptionPane.showMessageDialog(null, "添加失败!"); } } }); JButton btnNewButton_last = new JButton("返回"); btnNewButton_last.setBounds(220, 309, 113, 27); getContentPane().add(btnNewButton_last); btnNewButton_last.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dispose(); } }); } public static void main(String[] args) { new AddFrame().setVisible(true); } }
com.ynavc.Test
test.java
package com.ynavc.Test; import com.ynavc.Vive.MainFrame; public class Test { public static void main(String[] args) { new MainFrame().setVisible(true); } }