By GokiSoft.com| 23:20 29/10/2021|
Java Advanced

[Video] Chương trình quản lý Test nhanh PCR - COVID19 - Lập trình Java nâng cao - C2010G

Bài tập - Chương trình quản lý Test nhanh PCR - COVID19 - Lập trình Java nâng cao.



B1. Xay dung database -> C2010G

create table pattern_info (
	id int primary key auto_increment,
	pattern_no varchar(50),
	fullname varchar(50),
	birthday int,
	career varchar(150),
	address varchar(200),
	address_test varchar(200),
	created_at datetime,
	updated_at datetime,
	result int default 0
)

B2. Tao du an + tai thu vien jdbc mysql driver maven -> add project
B3. Mapping Models <-> Tables
B4. DAO (Modify) - Insert, Update, Delete, Find (Lay danh sach PattermInfo)




#Utility.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.bt2390;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Utility {
    public static int scanInt(Scanner scan) {
        int value;
        while(true) {
            try {
                value = Integer.parseInt(scan.nextLine());
                return value;
            } catch(NumberFormatException e) {
                System.out.println("Nhap lai: ");
            }
        }
    }
}


#PatternInfoModify.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.bt2390;

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 PatternInfoModify {

    public static void insert(PatternInfo info) {
        Connection conn = null;
        PreparedStatement statement = null;

        try {
            conn = DriverManager.getConnection(Config.BD_URL, Config.USERNAME, Config.PASSWORD);

            String sql = "insert into pattern_info(pattern_no, fullname, birthday, career, address, address_test, created_at, updated_at, result) "
                    + "values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
            statement = conn.prepareStatement(sql);

            Date date = Calendar.getInstance().getTime();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String strDate = dateFormat.format(date);
            
            statement.setString(1, info.getPatternNo());
            statement.setString(2, info.getFullname());
            statement.setInt(3, info.getBirthday());
            statement.setString(4, info.getCareer());
            statement.setString(5, info.getAddress());
            statement.setString(6, info.getAddressTest());
            statement.setString(7, strDate);
            statement.setString(8, strDate);
            statement.setInt(9, info.getResult());

            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static void update(String patternNo, int result) {
        Connection conn = null;
        PreparedStatement statement = null;

        try {
            conn = DriverManager.getConnection(Config.BD_URL, Config.USERNAME, Config.PASSWORD);

            String sql = "update pattern_info set result = ?, updated_at = ? where pattern_no = ?";
            statement = conn.prepareStatement(sql);

            Date date = Calendar.getInstance().getTime();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String strDate = dateFormat.format(date);

            statement.setInt(1, result);
            statement.setString(2, strDate);
            statement.setString(3, patternNo);

            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static List<PatternInfo> searchByDate(String searchDate) {
        List<PatternInfo> dataList = new ArrayList<>();
        
        Connection conn = null;
        PreparedStatement statement = null;

        try {
            conn = DriverManager.getConnection(Config.BD_URL, Config.USERNAME, Config.PASSWORD);

            String sql = "select * from pattern_info where created_at >= ? and created_at < ?";
            statement = conn.prepareStatement(sql);
            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(sdf.parse(searchDate));
            c.add(Calendar.DATE, 1);  // number of days to add
            String nextDate = sdf.format(c.getTime());  // dt is now the new date
            //Tang them 1 ngay cho myDate
            

            statement.setString(1, searchDate);
            statement.setString(2, nextDate);

            ResultSet resultSet = statement.executeQuery();
            
            while(resultSet.next()) {
                PatternInfo info = new PatternInfo(
                        resultSet.getInt("id"), 
                        resultSet.getInt("birthday"), 
                        resultSet.getInt("result"), 
                        resultSet.getString("pattern_no"), 
                        resultSet.getString("fullname"), 
                        resultSet.getString("career"), 
                        resultSet.getString("address"), 
                        resultSet.getString("address_test"), 
                        resultSet.getString("created_at"), 
                        resultSet.getString("updated_at")
                );
                dataList.add(info);
            }
        } catch (SQLException ex) {
            Logger.getLogger(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        return dataList;
    }
    
    public static List<PatternInfo> searchByAddress(String addressTest) {
        List<PatternInfo> dataList = new ArrayList<>();
        
        Connection conn = null;
        PreparedStatement statement = null;

        try {
            conn = DriverManager.getConnection(Config.BD_URL, Config.USERNAME, Config.PASSWORD);

            String sql = "select * from pattern_info where address_test like ?";
            statement = conn.prepareStatement(sql);
            
            addressTest = "%" + addressTest + "%";
            statement.setString(1, addressTest);

            ResultSet resultSet = statement.executeQuery();
            
            while(resultSet.next()) {
                PatternInfo info = new PatternInfo(
                        resultSet.getInt("id"), 
                        resultSet.getInt("birthday"), 
                        resultSet.getInt("result"), 
                        resultSet.getString("pattern_no"), 
                        resultSet.getString("fullname"), 
                        resultSet.getString("career"), 
                        resultSet.getString("address"), 
                        resultSet.getString("address_test"), 
                        resultSet.getString("created_at"), 
                        resultSet.getString("updated_at")
                );
                dataList.add(info);
            }
        } catch (SQLException ex) {
            Logger.getLogger(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        return dataList;
    }
}


#PatternInfo.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.bt2390;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class PatternInfo {
    int id, birthday, result;
    String patternNo, fullname, career, address, addressTest, createdAt, updatedAt;

    public PatternInfo() {
    }

    public PatternInfo(int id, int birthday, int result, String patternNo, String fullname, String career, String address, String addressTest, String createdAt, String updatedAt) {
        this.id = id;
        this.birthday = birthday;
        this.result = result;
        this.patternNo = patternNo;
        this.fullname = fullname;
        this.career = career;
        this.address = address;
        this.addressTest = addressTest;
        this.createdAt = createdAt;
        this.updatedAt = updatedAt;
    }

    public PatternInfo(int birthday, int result, String patternNo, String fullname, String career, String address, String addressTest) {
        this.birthday = birthday;
        this.result = result;
        this.patternNo = patternNo;
        this.fullname = fullname;
        this.career = career;
        this.address = address;
        this.addressTest = addressTest;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getBirthday() {
        return birthday;
    }

    public void setBirthday(int birthday) {
        this.birthday = birthday;
    }

    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }

    public String getPatternNo() {
        return patternNo;
    }

    public void setPatternNo(String patternNo) {
        this.patternNo = patternNo;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getCareer() {
        return career;
    }

    public void setCareer(String career) {
        this.career = career;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddressTest() {
        return addressTest;
    }

    public void setAddressTest(String addressTest) {
        this.addressTest = addressTest;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public String getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(String updatedAt) {
        this.updatedAt = updatedAt;
    }

    @Override
    public String toString() {
        return "PatternInfo{" + "id=" + id + ", birthday=" + birthday + ", result=" + result + ", patternNo=" + patternNo + ", fullname=" + fullname + ", career=" + career + ", address=" + address + ", addressTest=" + addressTest + ", createdAt=" + createdAt + ", updatedAt=" + updatedAt + '}';
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap mau TEST: ");
        patternNo = scan.nextLine();
        
        System.out.println("Nhap ten: ");
        fullname = scan.nextLine();
        
        System.out.println("Nhap nam sinh: ");
        birthday = Utility.scanInt(scan);
        
        System.out.println("Nhap nghe nghiep: ");
        career = scan.nextLine();
        
        System.out.println("Nhap dia chi: ");
        address = scan.nextLine();
        
        System.out.println("Nhap dia chi TEST: ");
        addressTest = 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.bt2390;

import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    static Scanner scan = new Scanner(System.in);
    public static void main(String[] args) {
        int choose;
        
        do {
            showMenu();
            choose = Utility.scanInt(scan);
            
            switch(choose) {
                case 1:
                    input();
                    break;
                case 2:
                    searchByDate();
                    break;
                case 3:
                    searchByAddress();
                    break;
                case 4:
                    inputResult();
                    break;
                case 5:
                    
                    break;
                case 6:
                    
                    break;
                case 7:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while(choose != 7);
    }
    
    static void input() {
        String option;
        do {
            PatternInfo info = new PatternInfo();
            info.input();
            PatternInfoModify.insert(info);
            
            System.out.println("Tiep tuc nhap Y/n? ");
            option = scan.nextLine();
        } while (!option.equalsIgnoreCase("N"));
    }
    
    static void showMenu() {
        System.out.println("1. Nhap");
        System.out.println("2. Tim theo ngay");
        System.out.println("3. Tim theo dia chi");
        System.out.println("4. Nhap ket qua");
        System.out.println("5. Bao cao");
        System.out.println("6. Xuat bao cao ket qua txt");
        System.out.println("7. Thoat");
        System.out.println("Chon: ");
    }

    private static void searchByDate() {
        System.out.println("Nhap ngay can xem ket qua (yyyy-MM-dd): ");
        String searchData = scan.nextLine();
        
        List<PatternInfo> dataList = PatternInfoModify.searchByDate(searchData);
        
        for (PatternInfo patternInfo : dataList) {
            System.out.println(patternInfo);
        }
    }

    private static void searchByAddress() {
        System.out.println("Nhap dai chi can xem ket qua: ");
        String address = scan.nextLine();
        
        List<PatternInfo> dataList = PatternInfoModify.searchByAddress(address);
        
        for (PatternInfo patternInfo : dataList) {
            System.out.println(patternInfo);
        }
    }

    private static void inputResult() {
        System.out.println("Nhap mau TEST: ");
        String patternNo = scan.nextLine();
        
        System.out.println("Ket qua TEST (0: Binh Thuong, 1: Duong Tinh COVID-19): ");
        int result = Utility.scanInt(scan);
        
        PatternInfoModify.update(patternNo, result);
    }
}


#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.bt2390;

/**
 *
 * @author Diep.Tran
 */
public interface Config {
    String BD_URL = "jdbc:mysql://localhost:3306/C2010G";
    String USERNAME = "root";
    String PASSWORD = "";
}




Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó