-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBranchDAO.java
More file actions
136 lines (105 loc) · 5.33 KB
/
BranchDAO.java
File metadata and controls
136 lines (105 loc) · 5.33 KB
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
package services;
import DB.DBConnection;
import model.Branch;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BranchDAO {
public List<Branch> getAllBranches() {
List<Branch> branches = new ArrayList<>();
String query = "SELECT name, city, address, phone, isActive, employeeCount, branchcode FROM branch";
try (Connection connection = DBConnection.getInstance().getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query);
ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
Branch branch = new Branch();
branch.setName(resultSet.getString("name"));
branch.setCity(resultSet.getString("city"));
branch.setAddress(resultSet.getString("address"));
branch.setPhone(resultSet.getString("phone"));
branch.setActive(resultSet.getBoolean("isActive"));
branch.setEmployeeCount(resultSet.getInt("employeeCount"));
branch.setBranchCode(resultSet.getString("branchcode"));
branches.add(branch);
}
} catch (SQLException e) {
e.printStackTrace();
}
return branches;
}
// Fetch a branch by its branchcode
public Branch getBranchByCode(String branchcode) {
Branch branch = null;
String query = "SELECT name, city, address, phone, isActive, employeeCount, branchcode FROM branch WHERE branchcode = ?";
try (Connection connection = DBConnection.getInstance().getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, branchcode);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
branch = new Branch();
branch.setName(resultSet.getString("name"));
branch.setCity(resultSet.getString("city"));
branch.setAddress(resultSet.getString("address"));
branch.setPhone(resultSet.getString("phone"));
branch.setActive(resultSet.getBoolean("isActive"));
branch.setEmployeeCount(resultSet.getInt("employeeCount"));
branch.setBranchCode(resultSet.getString("branchcode"));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return branch;
}
// Add a new branch to the database
public boolean addBranch(Branch branch) {
String query = "INSERT INTO branch (name, city, address, phone, isActive, employeeCount, branchcode) VALUES (?, ?, ?, ?, ?, ?, ?)";
try (Connection connection = DBConnection.getInstance().getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, branch.getName());
preparedStatement.setString(2, branch.getCity());
preparedStatement.setString(3, branch.getAddress());
preparedStatement.setString(4, branch.getPhone());
preparedStatement.setBoolean(5, branch.isActive());
preparedStatement.setInt(6, branch.getEmployeeCount());
preparedStatement.setString(7, branch.getBranchCode());
int rowsInserted = preparedStatement.executeUpdate();
return rowsInserted > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
// Update an existing branch
public boolean updateBranch(Branch branch) {
String query = "UPDATE branch SET name = ?, city = ?, address = ?, phone = ?, isActive = ?, employeeCount = ? WHERE branchcode = ?";
try (Connection connection = DBConnection.getInstance().getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, branch.getName());
preparedStatement.setString(2, branch.getCity());
preparedStatement.setString(3, branch.getAddress());
preparedStatement.setString(4, branch.getPhone());
preparedStatement.setBoolean(5, branch.isActive());
preparedStatement.setInt(6, branch.getEmployeeCount());
preparedStatement.setString(7, branch.getBranchCode());
int rowsUpdated = preparedStatement.executeUpdate();
return rowsUpdated > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
// Delete a branch by its branchcode
public boolean deleteBranch(String branchcode) {
String query = "DELETE FROM branch WHERE branchcode = ?";
try (Connection connection = DBConnection.getInstance().getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, branchcode);
int rowsDeleted = preparedStatement.executeUpdate();
return rowsDeleted > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}