目錄
檔案說明:
一、語言和環境
二、實作功能
三、資料庫設計
四、具體要求及推薦實作步驟
五、注意事項
六、評分標準
實作代碼:
一、資料庫:
二、Java Swing:
檔案說明:
一、語言和環境
1. 實作語言:Java,
2. 開發環境:MyEclipse+MySQL,
二、實作功能
使用 Java 技術開發一個影院售票系統,具體實作功能如下:
1. 程式啟動顯示主界面,點擊查詢按鈕,顯示所有影片資訊,如圖 1 所示,
2. 點擊“新增”按鈕,彈出新增影片視窗,可以錄入新增影片相關資訊,如圖 2 所示
3. 在新增影片視窗,點擊“新增”按鈕,將影片資訊保存到資料庫中,保存成功給予
用戶提示,并清空所有文本框內容,點擊“回傳”按鈕則關閉視窗,在影片串列窗
口顯示最新添加的影片資訊,如圖 3 所示,
三、資料庫設計
1. 創建資料庫(MovieDB)
2. 創建影片資訊表(movies),結構如下:
| 欄位名 | 說明 | 欄位型別 | 長度 | 備注 |
| movieID | 影片編號 | int |
| 主鍵,自動增長 |
| name | 影片名稱 | varchar | 64 | 非空 |
| duration | 影片時長 | int |
| 非空 |
| area | 影片產地 | varchar | 32 |
|
| date | 上映日期 | date |
|
|
| price | 票價 | decimal(6,2) |
|
|
四、具體要求及推薦實作步驟
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. 將整個專案打包提交
六、評分標準
| 題目:影院售票系統 | ||
| 該專案的評分標準如下: | ||
| 20 | 資料庫 | |
|
| 5 | 創建資料庫 |
|
| 5 | 添加至少 3 條測驗資料 |
|
|
10 | 正確創建 BaseDAO 類,封裝 2 個方法
|
| 30 | 專案框架搭建 | |
|
| 5 | 正確添加并參考 JDBC 依賴 jar 包 |
|
| 5 | 正確撰寫物體類 |
|
| 10 | 結合 BaseDAO 正確撰寫查詢資料的方法 |
|
| 10 | 結合 BaseDao 正確撰寫新增資料的方法 |
| 20 | 顯示所有影片資訊 | |
|
| 15 | 點擊查詢按鈕,展示所有影片資訊 |
|
| 5 | 上映日期列需以指定格式顯示“YYYY-MM-DD” |
| 20 | 新增影片資訊 | |
|
| 5 | 新增前判斷所有專案是否填寫完畢, |
|
| 10 | 正確將資料添加到資料庫并在串列視窗顯示新增加的記錄 |
|
| 5 | 新增完畢提示用戶(2 分),清空文本框內容(3 分) |
| 10 | 總體編程技術 | |
|
| 5 | 程式邏輯分明,有一定注釋 |
|
| 5 | 變數命名符合規范,可讀性好,編碼書寫有縮進,有例外處理 |
| 總分 | 100 分 | |
實作代碼:
一、資料庫:
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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/160869.html
標籤:其他
