-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepartmentDatabase.java
More file actions
170 lines (146 loc) · 3.95 KB
/
DepartmentDatabase.java
File metadata and controls
170 lines (146 loc) · 3.95 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import java.sql.*;
import javax.swing.*;
public class DepartmentDatabase
{
public static boolean writeDepartment(String departmentName)
{
ConnectDatabase database = new ConnectDatabase();
try
{
database.getStatement().executeUpdate("INSERT INTO Department (departmentName) VALUES ('" + departmentName + "')");
database.close();
return true;
}
catch (SQLException e)
{
database.close();
e.printStackTrace();
return false;
}
}
public static Department readDepartment(String departmentName)
{
ConnectDatabase database = new ConnectDatabase();
try
{
ResultSet result = database.getStatement().executeQuery("SELECT * FROM Department WHERE departmentName = '" + departmentName + "'");
result.next();
database.close();
return new Department(departmentName, result.getString("departmentHead"));
}
catch (SQLException e)
{
database.close();
e.printStackTrace();
return null;
}
}
public static boolean editDepartment(Department department)
{
ConnectDatabase database = new ConnectDatabase();
try
{
database.getStatement().executeUpdate("UPDATE Department SET departmentHead = '" + department.getDepartmentHead() + "' WHERE departmentName = '" + department.getDepartmentName() + "'");
database.close();
return true;
}
catch (SQLException e)
{
database.close();
e.printStackTrace();
return false;
}
}
public static boolean deleteDepartment(String departmentName)
{
ConnectDatabase database = new ConnectDatabase();
try
{
database.getStatement().executeUpdate("DELETE FROM Department WHERE departmentName = '" + departmentName + "'");
database.close();
return true;
}
catch (SQLException e)
{
database.close();
e.printStackTrace();
return false;
}
}
public static JTable getDepartmentJTable()
{
ConnectDatabase database = new ConnectDatabase();
try
{
ResultSet resultSet = database.getStatement().executeQuery("SELECT * FROM Department");
resultSet.last();
int numRows = resultSet.getRow();
resultSet.first();
if (numRows == 0)
throw new SQLException();
ResultSetMetaData meta = resultSet.getMetaData();
String[] colNames = new String[meta.getColumnCount()];
colNames[0] = "Department Name"; colNames[1] = "Department Head";
String[][] tableData = new String[numRows][meta.getColumnCount()];
for (int row = 0; row < numRows; row++)
{
for (int col = 0; col < meta.getColumnCount(); col++)
tableData[row][col] = resultSet.getString(col + 1);
resultSet.next();
}
database.close();
return new JTable(tableData, colNames);
}
catch (SQLException e)
{
database.close();
e.printStackTrace();
return null;
}
}
public static boolean isEmpty()
{
ConnectDatabase database = new ConnectDatabase();
try
{
ResultSet resultSet = database.getStatement().executeQuery("SELECT * FROM Department");
resultSet.last();
int numRows = resultSet.getRow();
database.close();
if (numRows == 0)
return true;
else
return false;
}
catch (SQLException e)
{
database.close();
e.printStackTrace();
return true;
}
}
public static String[] getDepartmentNames()
{
ConnectDatabase database = new ConnectDatabase();
try
{
ResultSet resultSet = database.getStatement().executeQuery("SELECT * FROM Department");
resultSet.last();
int numRows = resultSet.getRow();
resultSet.beforeFirst();
if (numRows == 0)
throw new SQLException();
String[] departmentNames = new String[numRows];
for (int i = 0; resultSet.next(); i++)
departmentNames[i] = resultSet.getString("departmentName");
database.close();
return departmentNames;
}
catch (SQLException e)
{
database.close();
e.printStackTrace();
return null;
}
}
}