๐Ÿ“ฆ 0372hoanghoccode / ko-co

๐Ÿ“„ ChucVuDAO.java ยท 145 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145package DAO;

import connectionSQL.ConnectionManager;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
// import java.sql.Statement;
import java.util.ArrayList;

// --- IMPORT DTO ----
import DTO.CHUCVU;


public class ChucVuDAO implements DAOInterface<CHUCVU>{

    public static ChucVuDAO getInstance() {
        return new ChucVuDAO();
    }

    @Override
    public ArrayList<CHUCVU> getList() {
        ArrayList<CHUCVU> list = new ArrayList<>();
		Connection con = ConnectionManager.getConnection();
        try {
            String sql = "select * from CHUCVU";
            PreparedStatement st = con.prepareStatement(sql);
            ResultSet rs = st.executeQuery();

            while(rs.next()) {
                CHUCVU chucvu = new CHUCVU();
                chucvu.setMaChucVu(rs.getString(1));
                chucvu.setTenChucVu(rs.getString(2));
             
                chucvu.setPhuCapChucVu(rs.getDouble(3));
                chucvu.setNgayNhanChuc(rs.getDate(4).toLocalDate());
                list.add(chucvu);
            }
            ConnectionManager.closeConnection(con);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int insert(CHUCVU t) {
        int result = 0;
        Connection con = ConnectionManager.getConnection();
		try {
			PreparedStatement st = con.prepareStatement("insert CHUCVU values(?,?,?,?)");
			st.setString(1, t.getMaChucVu());
			st.setString(2, t.getTenChucVu());
			st.setDouble(3, t.getPhuCapChucVu());
			st.setDate(4, Date.valueOf(t.getNgayNhanChuc()));
			result = st.executeUpdate();
			ConnectionManager.closeConnection(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return result;
    }

    @Override
    public int update(CHUCVU t) {
        int result = 0;
        Connection con = ConnectionManager.getConnection();
        try {
			PreparedStatement st = con.prepareStatement("update CHUCVU set tenChucVu = ?, phuCapChucVu = ?,ngayNhanChuc = ? where maChucVu = ?");
			st.setString(1, t.getTenChucVu());
			st.setDouble(2, t.getPhuCapChucVu());
			st.setDate(3, Date.valueOf(t.getNgayNhanChuc()));
			st.setString(4, t.getMaChucVu());
			result = st.executeUpdate();
			ConnectionManager.closeConnection(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return result;
    }

    @Override
    public int del(String maChucvu) {
        int result = 0;
        Connection con = ConnectionManager.getConnection();
        try {
			PreparedStatement st = con.prepareStatement("delete CHUCVU where maChucVu = ?");
			st.setString(1, maChucvu);
			st.executeUpdate();
			ConnectionManager.closeConnection(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return result;
    }

    public Object[] getChucVuCongTyTuTen(String tenChucVu) {
		Object data[] = new Object[3];
		Connection con = ConnectionManager.getConnection();
		try {
			Statement st = con.createStatement();
			ResultSet rs  = st.executeQuery("select * from CHUCVUCONGTY where tenChucVu = N'"+tenChucVu+"'");
			while(rs.next()) {
				data[0] = rs.getString("maChucVu");
				data[1] = rs.getString("tenChucVu");
				data[2] = rs.getDouble("phuCapChucVu");
			}
			ConnectionManager.closeConnection(con);
			
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return data;
	}

	public String[] getDanhSachTenChucVuCongTy() {
		String data[] = null;
		Connection con = ConnectionManager.getConnection();
		try {
			Statement st = con.createStatement();
			ResultSet rs  = st.executeQuery("select COUNT(tenChucVu) from CHUCVUCONGTY");
			while(rs.next()) {
				data = new String[rs.getInt(1)];
			}
			rs = st.executeQuery("select * from CHUCVUCONGTY order by phuCapChucVu desc");
			int count =0;
			while(rs.next()) {
				data[count] = rs.getString("tenChucVu");
				count++;
			}
			ConnectionManager.closeConnection(con);
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return data;
	}

}