-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseHandler.java
More file actions
256 lines (235 loc) · 9.63 KB
/
DatabaseHandler.java
File metadata and controls
256 lines (235 loc) · 9.63 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package library.database;
import java.sql.DriverManager; // Gestion des pilotes pour la base de données
import java.sql.Connection; // créer la connexion avec la BD
import java.sql.ResultSet; // Pour Collecter les données
import java.sql.SQLException; // Gérer les Erreurs
import java.sql.Statement; // Gérer les Modfications
import java.sql.DatabaseMetaData; // Gestion generale de la BD
import javax.swing.JOptionPane; // fenetres pour afficher les messages
/**
*
* @author you1-
*/
public final class DatabaseHandler {
private static DatabaseHandler handler;
private static final String DB_URL = "jdbc:derby:database;create=true";
private static Connection conn = null;
private static Statement stmt = null;
public DatabaseHandler() {
createConnection();
setupProjectTable();
setupPHTable();
}
@SuppressWarnings("deprecation")
private static void createConnection() {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
conn = DriverManager.getConnection(DB_URL);
}
catch (ClassNotFoundException | SQLException e) {
JOptionPane.showMessageDialog(null, "Cant load database", "Database"
+ " Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
void setupProjectTable() {
String TABLE_NAME = "PROJECTS";
try {
stmt = conn.createStatement();
DatabaseMetaData dbm = conn.getMetaData();
ResultSet tables = dbm.getTables(null, null,
TABLE_NAME.toUpperCase(), null);
if (tables.next()) {
System.out.println("Table " + TABLE_NAME + " already exists. "
+ "Ready for go!");
} else {
stmt.execute("CREATE TABLE " + TABLE_NAME + "("
+ " id INT,\n"
+ " name varchar(200),\n"
+ " type varchar(200),\n"
+ " numShifts INT,\n"
+ " hoursPerMonth DOUBLE,\n"
+ " layUpRatio DOUBLE,\n"
+ " Linelength INT,\n"
+ " Line_Config varchar(200),\n"
+ " Utilized_sides INT,\n"
+ " charge DOUBLE,\n"
+ " capacity DOUBLE,\n"
+ " numOfVerticalLines DOUBLE,\n"
+ " supportLength DOUBLE,\n"
+ " NumFinTab DOUBLE,\n"
+ " numPackStat DOUBLE,\n"
+ " numKitRack DOUBLE,\n"
+ " numWRack DOUBLE,\n"
+ " numKanRack DOUBLE,\n"
+ " numMecTable DOUBLE,\n"
+ " surSup2m DOUBLE,\n"
+ " surFinTab DOUBLE,\n"
+ " surPackStat DOUBLE,\n"
+ " surKitRack DOUBLE,\n"
+ " surWRack DOUBLE,\n"
+ " surKanRack DOUBLE,\n"
+ " surAsseyBH DOUBLE,\n"
+ " surMecTable DOUBLE,\n"
+ " month DATE,\n"
+ " Productivity DOUBLE,\n"
+ " Backlog DOUBLE,\n"
+ " numOfSupp_R DOUBLE,\n"
+ " NumFinTab_R DOUBLE,\n"
+ " numPackStat_R DOUBLE,\n"
+ " numKitRack_R DOUBLE,\n"
+ " numWRack_R DOUBLE,\n"
+ " numKanRack_R DOUBLE,\n"
+ " numMecTable_R DOUBLE,\n"
+ " numAsseyBH_R DOUBLE,\n"
+ " surSup2m_R DOUBLE,\n"
+ " surFinTab_R DOUBLE,\n"
+ " surPackStat_R DOUBLE,\n"
+ " surKitRack_R DOUBLE,\n"
+ " surWRack_R DOUBLE,\n"
+ " surKanRack_R DOUBLE,\n"
+ " surAsseyBH_R DOUBLE,\n"
+ " surMecTable_R DOUBLE,\n"
+ " total_surface_needed DOUBLE"
+ " )");
System.out.println("Table"+TABLE_NAME+" created");
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Error:" + e.getMessage(),
"Error Occured", JOptionPane.ERROR_MESSAGE);
System.out.println("Exception at execQuery:dataHandler" +
e.getLocalizedMessage());
} finally {
}
}
// void setupProjPivotT(){
// String TABLE_NAME = "pivot_table";
// try {
// stmt = conn.createStatement();
// DatabaseMetaData dbm = conn.getMetaData();
// ResultSet tables = dbm.getTables(null, null, TABLE_NAME.toUpperCase(), null);
// if (tables.next()) {
// System.out.println("Table " + TABLE_NAME + " already exists. Ready for go!");
// } else {
// stmt.execute( "SELECT * FROM \n"
// + "(\n"
// + " SELECT \n"
// + " month, \n"
// + " total_surface_needed,\n"
// + " name\n"
// + " FROM \n"
// + " PROJECTS\n"
// + ") t \n"
// + "pivot(\n"
// + " total_surface_needed\n"
// + " FOR month IN (\n"
// + " [2022-01-01]) \n"
// + ") AS pivot_table;");
// System.out.println("pivot_table");
// }
// } catch (SQLException e) {
// System.err.println(e.getMessage() + " --- setupDatabase");
// } finally {
// }
// }
void setupPHTable(){
String TABLE_NAME = "PH";
try {
stmt = conn.createStatement();
DatabaseMetaData dbm = conn.getMetaData();
ResultSet tables = dbm.getTables(null, null, TABLE_NAME.toUpperCase
(), null);
if (tables.next()) {
System.out.println("Table " + TABLE_NAME + " already exists."
+ " Ready for go!");
} else {
stmt.execute("CREATE TABLE " + TABLE_NAME + "("
+ " P_id INT ,\n"
+ " PH_id varchar(200),\n"
+ " zone varchar(200),\n"
+ " phts INT,\n"
+ " At_H DOUBLE,\n"
+ " numOp INT,\n"
+ " numShift INT,\n"
+ " lengthPH DOUBLE,\n"
+ " numBoard DOUBLE"
+ " )");
System.out.println("PH Table created");
}
} catch (SQLException e) {
System.err.println(e.getMessage() + " --- setupDatabase");
} finally {
}
}
void dropTable(String T){
try{
Statement stmt = conn.createStatement();
String sql = "DROP TABLE "+T;
stmt.executeUpdate(sql);
System.out.println("Table deleted in given database...");
} catch (SQLException e) {
e.printStackTrace();
}
}
public ResultSet execQuery(String query) {
ResultSet result;
try {
stmt = conn.createStatement();
result = stmt.executeQuery(query);
} catch (SQLException ex) {
System.out.println("Exception at execQuery:dataHandler" +
ex.getLocalizedMessage());
return null;
} finally {
}
return result;
}
public boolean execAction(String qu) {
try {
stmt = conn.createStatement();
stmt.execute(qu);
return true;
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Error:" + ex.getMessage(),
"Error Occured", JOptionPane.ERROR_MESSAGE);
System.out.println("Exception at execQuery:dataHandler" +
ex.getLocalizedMessage());
return false;
} finally {
}
}
public static DatabaseHandler getInstance() {
if (handler == null) {
handler = new DatabaseHandler();
}
return handler;
}
public static void main(String[] args) throws Exception {
DatabaseHandler.getInstance();
}
public Connection getConnection() {
return conn;
}
public static void shutdownDB()
{
try
{
if (stmt != null)
{
stmt.close();
}
if (conn != null)
{
DriverManager.getConnection(DB_URL + ";shutdown=true");
conn.close();
}
}
catch (SQLException sqlExcept)
{
}
}
}