By GokiSoft.com| 20:46 07/09/2021|
Java Advanced

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

#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.lesson11.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 {
    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(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    static void closeConnection() {
        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 insert(PatternInfo info) {
        openConnection();
        
        try {
            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);
        }
        
        closeConnection();
    }
    
    public static void update(String patternNo, int result) {
        openConnection();
        
        try {
            String sql = "update pattern_info set result = ? where pattern_no = ?";
            statement = conn.prepareStatement(sql);
            
            statement.setInt(1, result);
            statement.setString(2, patternNo);
            
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(PatternInfoModify.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        closeConnection();
    }
    
    public static List<PatternInfo> searchByDate(String searchData) {
        List<PatternInfo> dataList = new ArrayList<>();
        
        openConnection();
        
        try {
            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(searchData));
            c.add(Calendar.DATE, 1);  // number of days to add
            String nextDate = sdf.format(c.getTime());  // dt is now the new date
            
            statement.setString(1, searchData);
            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("fullname"), 
                        resultSet.getString("pattern_no"), 
                        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);
        }
        
        closeConnection();
        
        return dataList;
    }
    
    public static List<PatternInfo> searchByAddress(String address) {
        List<PatternInfo> dataList = new ArrayList<>();
        
        openConnection();
        
        try {
            String sql = "select * from pattern_info where address_test like ?";
            statement = conn.prepareStatement(sql);
            
            statement.setString(1, address);
            
            ResultSet resultSet = statement.executeQuery();
            
            while(resultSet.next()) {
                PatternInfo info = new PatternInfo(
                        resultSet.getInt("id"), 
                        resultSet.getInt("birthday"), 
                        resultSet.getInt("result"), 
                        resultSet.getString("fullname"), 
                        resultSet.getString("pattern_no"), 
                        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);
        }
        
        closeConnection();
        
        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.lesson11.bt2390;

import java.util.Scanner;

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

    public PatternInfo() {
    }

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

    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 getFullname() {
        return fullname;
    }

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

    public String getPatternNo() {
        return patternNo;
    }

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

    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 + ", fullname=" + fullname + ", patternNo=" + patternNo + ", career=" + career + ", address=" + address + ", addressTest=" + addressTest + ", createdAt=" + createdAt + ", updatedAt=" + updatedAt + '}';
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten: ");
        fullname = scan.nextLine();
        
        System.out.println("Nhap mau TEST: ");
        patternNo = scan.nextLine();
        
        System.out.println("Nhap nam sinh: ");
        birthday = Integer.parseInt(scan.nextLine());
        
        System.out.println("Nhap nghe nghiep: ");
        career = scan.nextLine();
        
        System.out.println("Nhap dia chi: ");
        address = scan.nextLine();
        
        System.out.println("Nhap dia chi lay mau: ");
        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.lesson11.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 = Integer.parseInt(scan.nextLine());

            switch (choose) {
                case 1:
                    input();
                    break;
                case 2:
                    searchByDate();
                    break;
                case 3:
                    searchByAddress();
                    break;
                case 4:
                    updatePattern();
                    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 showMenu() {
        System.out.println("1. Nhap");
        System.out.println("2. Hien thi theo ngay");
        System.out.println("3. Hien thi theo dia chi");
        System.out.println("4. Cap nhat ket qua");
        System.out.println("5. Bao cao");
        System.out.println("6. Xuat du lieu");
        System.out.println("7. Thoat");
        System.out.println("Chon: ");
    }

    private static void input() {
        String option;
        do {
            PatternInfo info = new PatternInfo();
            info.input();
            
            PatternInfoModify.insert(info);
            System.out.println("Ban co tiep tuc nhap hay ko Y/n?");
            option = scan.nextLine();
        } while(!option.equalsIgnoreCase("N"));
    }

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

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

    private static void updatePattern() {
        System.out.println("Nhap mau TEST: ");
        String patternNo = scan.nextLine();
        
        System.out.println("Nhap ket qua: ");
        int result = Integer.parseInt(scan.nextLine());
        
        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.lesson11.bt2390;

/**
 *
 * @author Diep.Tran
 */
public interface Config {
    String DB_URL = "jdbc:mysql://localhost:3306/C2010G";
    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)