By GokiSoft.com|
21:10 09/09/2021|
Java Advanced
[Share Code] Kiểm tra 60 phút - Viết chương trình quản lý lịch thi đấu bóng đá vòng loại 3 World Cup - Lập trình Java nâng cao
B1. Tao database + table
create table schedule (
id int primary key auto_increment,
team_a varchar(50) not null,
team_b varchar(50) not null,
match_day datetime,
score_a int,
score_b int,
referee_name varchar(150),
note varchar(500)
)
B2. Mapping table <-> model trong project
#ScheduleModify.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package java2.lesson12.bt2395;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class ScheduleModify {
static Connection conn = null;
static PreparedStatement statement = null;
static void openConnection() {
try {
conn = DriverManager.getConnection(Config.DB_URL, Config.USERNAME, Config.PASSWORD);
} catch (SQLException ex) {
Logger.getLogger(ScheduleModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
static void closeConnection() {
if(statement != null) {
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(ScheduleModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(ScheduleModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void insert(Schedule schedule) {
openConnection();
String sql = "insert into schedule(team_a, team_b, match_day, score_a, score_b, referee_name, note) "
+ "values(?, ?, ?, ?, ?, ?, ?)";
try {
statement = conn.prepareStatement(sql);
statement.setString(1, schedule.getTeamA());
statement.setString(2, schedule.getTeamB());
statement.setString(3, schedule.getMatchDay());
statement.setInt(4, schedule.getScoreA());
statement.setInt(5, schedule.getScoreB());
statement.setString(6, schedule.getRefereeName());
statement.setString(7, schedule.getNote());
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(ScheduleModify.class.getName()).log(Level.SEVERE, null, ex);
}
closeConnection();
}
public static List<Schedule> getScheduleAfter() {
List<Schedule> dataList = new ArrayList<>();
try {
openConnection();
String sql = "select * from schedule where match_day >= ? order by match_day asc";
statement = conn.prepareStatement(sql);
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, 90);
Date date = c.getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = dateFormat.format(date);
System.out.println("strDate: " + strDate);
statement.setString(1, strDate);
ResultSet resultSet = statement.executeQuery();
while(resultSet.next()) {
Schedule s = new Schedule(
resultSet.getInt("id"),
resultSet.getString("team_a"),
resultSet.getString("team_b"),
resultSet.getString("match_day"),
resultSet.getString("referee_name"),
resultSet.getString("note"),
resultSet.getInt("score_a"),
resultSet.getInt("score_b")
);
dataList.add(s);
}
closeConnection();
} catch (SQLException ex) {
Logger.getLogger(ScheduleModify.class.getName()).log(Level.SEVERE, null, ex);
}
return dataList;
}
public static List<Schedule> getScheduleBefore() {
List<Schedule> dataList = new ArrayList<>();
try {
openConnection();
String sql = "select * from schedule where match_day < ? order by match_day desc";
statement = conn.prepareStatement(sql);
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, -90);
Date date = c.getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = dateFormat.format(date);
System.out.println("strDate: " + strDate);
statement.setString(1, strDate);
ResultSet resultSet = statement.executeQuery();
while(resultSet.next()) {
Schedule s = new Schedule(
resultSet.getInt("id"),
resultSet.getString("team_a"),
resultSet.getString("team_b"),
resultSet.getString("match_day"),
resultSet.getString("referee_name"),
resultSet.getString("note"),
resultSet.getInt("score_a"),
resultSet.getInt("score_b")
);
dataList.add(s);
}
closeConnection();
} catch (SQLException ex) {
Logger.getLogger(ScheduleModify.class.getName()).log(Level.SEVERE, null, ex);
}
return dataList;
}
public static List<Schedule> getSchedule(String searchDate) {
List<Schedule> dataList = new ArrayList<>();
try {
openConnection();
String sql = "select * from schedule where (match_day >= ? and match_day <= ?) or (match_day >= ? and match_day <= ?) order by match_day desc";
statement = conn.prepareStatement(sql);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(searchDate));
c.add(Calendar.MINUTE, -90);
String startTime = sdf.format(c.getTime()); // dt is now the new date
System.out.println("startTime: " + startTime);
c.setTime(sdf.parse(searchDate));
c.add(Calendar.MINUTE, 90); // number of days to add
String endTime = sdf.format(c.getTime()); // dt is now the new date
System.out.println("endTime: " + endTime);
statement.setString(1, startTime);
statement.setString(2, searchDate);
statement.setString(3, searchDate);
statement.setString(4, endTime);
ResultSet resultSet = statement.executeQuery();
while(resultSet.next()) {
Schedule s = new Schedule(
resultSet.getInt("id"),
resultSet.getString("team_a"),
resultSet.getString("team_b"),
resultSet.getString("match_day"),
resultSet.getString("referee_name"),
resultSet.getString("note"),
resultSet.getInt("score_a"),
resultSet.getInt("score_b")
);
dataList.add(s);
}
closeConnection();
} catch (SQLException ex) {
Logger.getLogger(ScheduleModify.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(ScheduleModify.class.getName()).log(Level.SEVERE, null, ex);
}
return dataList;
}
public static void update(String teamA, String teamB, String match_day, int scoreA, int scoreB) {
openConnection();
String sql = "update schedule set score_a = ?, score_b = ? where team_a = ? and team_b = ? and match_day = ?";
try {
statement = conn.prepareStatement(sql);
statement.setInt(1, scoreA);
statement.setInt(2, scoreB);
statement.setString(3, teamA);
statement.setString(4, teamB);
statement.setString(5, match_day);
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(ScheduleModify.class.getName()).log(Level.SEVERE, null, ex);
}
closeConnection();
}
}
#Schedule.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package java2.lesson12.bt2395;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Schedule {
int id;
String teamA, teamB, matchDay, refereeName, note;
int scoreA, scoreB;
public Schedule() {
}
public Schedule(int id, String teamA, String teamB, String matchDay, String refereeName, String note, int scoreA, int scoreB) {
this.id = id;
this.teamA = teamA;
this.teamB = teamB;
this.matchDay = matchDay;
this.refereeName = refereeName;
this.note = note;
this.scoreA = scoreA;
this.scoreB = scoreB;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTeamA() {
return teamA;
}
public void setTeamA(String teamA) {
this.teamA = teamA;
}
public String getTeamB() {
return teamB;
}
public void setTeamB(String teamB) {
this.teamB = teamB;
}
public String getMatchDay() {
return matchDay;
}
public void setMatchDay(String matchDay) {
this.matchDay = matchDay;
}
public String getRefereeName() {
return refereeName;
}
public void setRefereeName(String refereeName) {
this.refereeName = refereeName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public int getScoreA() {
return scoreA;
}
public void setScoreA(int scoreA) {
this.scoreA = scoreA;
}
public int getScoreB() {
return scoreB;
}
public void setScoreB(int scoreB) {
this.scoreB = scoreB;
}
@Override
public String toString() {
return "Schedule{" + "id=" + id + ", teamA=" + teamA + ", teamB=" + teamB + ", matchDay=" + matchDay + ", refereeName=" + refereeName + ", note=" + note + ", scoreA=" + scoreA + ", scoreB=" + scoreB + '}';
}
public void input() {
System.out.println("Nhap thong tin lich thi dau:");
Scanner scan = new Scanner(System.in);
System.out.println("Nhap doi A");
teamA = scan.nextLine();
System.out.println("Nhap doi B");
teamB = scan.nextLine();
System.out.println("Ngay thi dau (yyyy-MM-dd hh:mm:00): ");
matchDay = scan.nextLine();
System.out.println("Nhap ten trong tai: ");
refereeName = scan.nextLine();
}
}
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package java2.lesson12.bt2395;
import java.util.List;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Main {
static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
int choose;
do {
showMenu();
choose = Integer.parseInt(scan.nextLine());
switch(choose) {
case 1:
input();
break;
case 2:
displayAfter();
break;
case 3:
displayBefore();
break;
case 4:
updateMatch();
break;
case 5:
searchByDate();
break;
case 6:
System.out.println("Thoat!!!");
break;
default:
System.out.println("Nhap sai!!!");
break;
}
} while(choose != 6);
}
static void showMenu() {
System.out.println("1. Nhap thong tin lich thi dau");
System.out.println("2. Hien thi lich thi dau sap dien ra");
System.out.println("3. Hien thi tran dau da dien ra");
System.out.println("4. Cap nhat thong tin tran dau");
System.out.println("5. Hien thi cac tran dau dang dien ra");
System.out.println("6. Thoat");
System.out.println("Chon: ");
}
private static void input() {
System.out.println("Nhap so tran dau can them: ");
int N = Integer.parseInt(scan.nextLine());
for (int i = 0; i < N; i++) {
Schedule s = new Schedule();
s.input();
ScheduleModify.insert(s);
}
}
private static void displayAfter() {
List<Schedule> dataList = ScheduleModify.getScheduleAfter();
for (Schedule schedule : dataList) {
System.out.println(schedule);
}
}
private static void displayBefore() {
List<Schedule> dataList = ScheduleModify.getScheduleBefore();
for (Schedule schedule : dataList) {
System.out.println(schedule);
}
}
private static void updateMatch() {
System.out.println("Nhap doi A: ");
String teamA = scan.nextLine();
System.out.println("Nhap doi B: ");
String teamB = scan.nextLine();
System.out.println("Nhap thoi gian thi dau (yyyy-MM-dd hh:mm:ss): ");
String matchDay = scan.nextLine();
System.out.println("Ty so A: ");
int scoreA = Integer.parseInt(scan.nextLine());
System.out.println("Ty so B: ");
int scoreB = Integer.parseInt(scan.nextLine());
ScheduleModify.update(teamA, teamB, matchDay, scoreA, scoreB);
}
private static void searchByDate() {
System.out.println("Nhap ngay can xem (yyyy-MM-dd hh:mm:ss) : ");
String searchDate = scan.nextLine();
List<Schedule> dataList = ScheduleModify.getSchedule(searchDate);
for (Schedule schedule : dataList) {
System.out.println(schedule);
}
}
}
#Config.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package java2.lesson12.bt2395;
/**
*
* @author Diep.Tran
*/
public interface Config {
String DB_URL = "jdbc:mysql://localhost:3306/C2010L";
String USERNAME = "root";
String PASSWORD = "";
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)