-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDB_Connection.java
More file actions
executable file
·316 lines (255 loc) · 7.08 KB
/
DB_Connection.java
File metadata and controls
executable file
·316 lines (255 loc) · 7.08 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class DB_Connection {
private Connection db;
private String curUserIDConnectedToDB;
private PreparedStatement ps;
private ResultSet rs;
public boolean setDBConnection(String id, String pass)
{
this.curUserIDConnectedToDB = id;
try {
Class.forName("oracle.jdbc.OracleDriver");
this.db = DriverManager.getConnection("jdbc:oracle:thin:" + "@localhost:1521:XE", id, pass);
System.out.println("Connected to DB - id: " + id);
return true;
} catch (SQLException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println("Exception:" + e);
}
return false;
}
@SuppressWarnings("static-access")
public int tableUpdate_employee( int id, String name, String position,
int total_performance)
{
int n = -1;
String query = "insert into employee values(?, ?, ?, ?)";
ps = null;
try {
ps = db.prepareStatement(query);
ps.setString(1, Integer.toString(id));
ps.setString(2, name);
ps.setString(3, position);
ps.setInt(4, total_performance);
n = ps.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
JOptionPane warning = new JOptionPane();
warning.showMessageDialog(null, "Only supervisor can register an employee.","failed to register", warning.WARNING_MESSAGE);
}
return n;
}
@SuppressWarnings("static-access")
public int tableUpdate_customer(int id, String name, String birth_day,
String phone, int tpp, String grade)
{
int n = -1;
String query = "insert into customer values(?, ?, ?, ?, ?, ?)";
ps = null;
try {
ps = db.prepareStatement(query);
ps.setString(1, Integer.toString(id));
ps.setString(2, name);
ps.setString(3, birth_day);
ps.setString(4, phone);
ps.setInt(5, tpp);
ps.setString(6, grade);
n = ps.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
JOptionPane warning = new JOptionPane();
warning.showMessageDialog(null, "Only supervisor can register a customer.", "failed to register", warning.WARNING_MESSAGE);
}
return n;
}
public int tableUpdate_menu(String menuName, int price)
{
int n = -1;
String query = "insert into menu values(?, ?)";
ps = null;
try {
ps = db.prepareStatement(query);
ps.setString(1, menuName);
ps.setInt(2, price);
n = ps.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
JOptionPane warning = new JOptionPane();
warning.showMessageDialog(null, "Only supervisor can register a menu.", "failed to register", warning.WARNING_MESSAGE);
}
return n;
}
public int tableUpdate_sales(String custID, String menuName,
int order_quantity, int year, int month,
int day, int hour, int minute)
{
int n = -1;
String query = "insert into menu values(?, ?, ?, ?, ?, ?, ?, ?)";
ps = null;
try {
ps = db.prepareStatement(query);
ps.setString(1, custID);
ps.setString(2, menuName);
ps.setInt(3, order_quantity);
ps.setInt(4, year);
ps.setInt(5, month);
ps.setInt(6, day);
ps.setInt(7, hour);
ps.setInt(8, minute);
n = ps.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return n;
}
public int insertTupleToSales(int id, String name, String menuName, int year,
int month, int day, int hour, int minute){
int n = -1;
String query = "insert into sales values(?, ?, ?, ?, ?, ?)";
ps = null;
String date = Integer.toString(year)+"-"+Integer.toString(month)
+"-"+Integer.toString(day);
try {
ps = db.prepareStatement(query);
ps.setInt(1, id);
ps.setString(2, name);
ps.setString(3, menuName);
ps.setDate(4, java.sql.Date.valueOf(date));
ps.setInt(5, hour);
ps.setInt(6, minute);
n = ps.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return n;
}
public int updateTotalPurchasingPrice(String custName, int tpp)
{
int n = -1;
String query = "update customer set total_purchasing_price = ? "
+ "where name = ?";
ps = null;
try {
ps = db.prepareStatement(query);
ps.setInt(1, tpp);
ps.setString(2, custName);
n = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}
return n;
}
public int updateGrade(String custName, String grade)
{
int n = -1;
String query = "update customer set grade = ? "
+ "where name = ?";
ps = null;
try {
ps = db.prepareStatement(query);
ps.setString(1, grade);
ps.setString(2, custName);
n = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}
return n;
}
public int updateTotalPerformance(int total_price)
{
String query = "update employee set total_performance = ? where name = ?";
ps = null;
int n = -1;
try {
ps = db.prepareStatement(query);
ps.setInt(1, total_price);
ps.setString(2, this.curUserIDConnectedToDB);
n = ps.executeUpdate();
} catch (SQLException e) {
e.getMessage();
e.printStackTrace();
}
return n;
}
public ResultSet getTPPAndIdFromDB(String custName)
{
ps = null;
rs = null;
String query = "select total_purchasing_price, id from customer "
+ "where name = ?";
try {
ps = db.prepareStatement(query);
ps.setString(1, custName);
rs = ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public String getCurUserIDConnectedToDB()
{
return this.curUserIDConnectedToDB;
}
public int executeUpdate(String query)
{
ps = null;
int n = -1;
try{
ps = db.prepareStatement(query);
n = ps.executeUpdate();
}catch(SQLException e){
System.out.println(e.getMessage());
}
return n;
}
public ResultSet executeQuery(String query) throws SQLException
{
rs = null;
ps = null;
try{
ps = db.prepareStatement(query);
rs = ps.executeQuery();
}catch(SQLException se) {
System.out.println(se.getMessage());
}
return rs;
}
public void closeStatementResultSet()
{
try{
if(this.ps != null) this.ps.close();
if(this.rs != null) this.rs.close();
}catch(SQLException e)
{
e.getMessage();
}
}
public void commit()
{
try {
db.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void setCommitMode(boolean set)
{
try {
db.setAutoCommit(set);
} catch (SQLException e) {
e.printStackTrace();
}
}
}