-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplicationDB.java
381 lines (314 loc) · 16.2 KB
/
ApplicationDB.java
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package timelineManager.model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* This class has been designed for the project course in computer science at the Linnaeus University year 2017.
* This class represents the connection between the program and the database located into
* main root of application. Using JDBC or Java Database Connectivity is necessary.
* Note: This Database class, tables and it's controllers made for a specific goals, because of that tables and other prerequisite data can be seen in this class.
* If the user needs to use the database for other purposes rather than the current tables and columns, I strongly suggest a new design.
* @author Ahmadreza Vakilalroayayi
*/
public class ApplicationDB {
private static Connection con;
private boolean hasTimelineTable = false;
private boolean hasTaskTable = false;
/**
* This void method use JDBC(Java Database Connectivity) to create a connection with the database.
* By using this method if there will be no DB file,a new connection to database contains of two tables will be made.
* If there is a connection, the DB file or tables will not be replaced.
* @exception SQLException SQL query is not correct or the JDBC class can not be found
* @throws ClassNotFoundException JDBC class not exist
*/
public void connectToDatabase() throws ClassNotFoundException, SQLException {
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:TimeLineManagerDatabase.db");
initialiseTimeLinesTable();
initialiseTasksTable();
}
/**
* Find Timeline tasks and delete them by Timeline id.
* deleteAllTaskByID(); will delete all tasks WHERE Tasks id refer to it's own Timeline id.
* @param id Specified Timeline id number to delete the tasks with the same id.
*/
public void deleteAllTaskByID(int id) {
String sql = "DELETE FROM TasksTable WHERE TimeLineID = '"+id+"'";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
// preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println("timelineManager.model.ApplicationDB.deleteAllTaskByID()");
System.out.println(e.getMessage());
}
}
/**
* Find tasks and delete it by it's unique id.
* deleteTaskByTaskID(); will delete the unique task WHERE Task id==id.
* @param id Specified Task unique id number to delete the task with the id.
*/
public void deleteTaskByTaskID(int id) {
String sql = "DELETE FROM TasksTable WHERE TaskUniqueID = '"+id+"'";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println("test delete task");
System.out.println(e.getMessage());
}
}
/**
* Find Timelines and delete it by it's unique id.
* By deleting a Timeline data(row) the tasks that belong to that Timeline will also be gone.
* deleteTimeLineByID(int id); will delete the unique Timeline WHERE Timeline id is equal to id parameter.
* @param id Specified Timeline unique id.
*/
public void deleteTimeLineByID(int id) {
String sql = "DELETE FROM TimeLinesTable WHERE TimeLineID = ?";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println("timelineManager.model.ApplicationDB.deleteTimeLineByID()");
System.out.println(e.getMessage());
}
}
/**
* Adding data(Task) as a row to the database into TasksTable.
* @param UniqueName used to assign a unique number for the task.
* @param Title used to assign a title for the task.
* @param TaskDescription used to assign a related description for the task.
* @param TaskStartDate used to assign the start date of the task.
* @param TaskEndDate used to assign the end date of the task.
* @param TimelineID id number of the Timeline that this task should belongs to.
* @exception SQLException SQL query is not correct or the JDBC class can not be found
* @throws ClassNotFoundException JDBC class not exist
*/
public void addTask(int UniqueName, String Title, String TaskDescription, String TaskStartDate, String TaskEndDate, int TimelineID) throws ClassNotFoundException, SQLException {
if(con == null) {
// get connection
connectToDatabase();
}
PreparedStatement prep = con
.prepareStatement("insert into TasksTable values(?,?,?,?,?,?,?);");
prep.setInt(2, UniqueName);
prep.setString(3, Title);
prep.setString(4, TaskDescription);
prep.setString(5, TaskStartDate);
prep.setString(6, TaskEndDate);
prep.setInt(7, TimelineID);
prep.execute();
}
/**
* Adding data(Timeline) as a row to the database and table TimeLinesTable.
* All the parameters in this method will be used with an index number for the database tables columns.
* This method will create a row into the TimeLinesTable table. Each row has different columns to categorize the details of the Timeline.
* Values of the rows(?), TimeLinesTable needs 6 values to be full filled.
* If the connection is not null, a new PreparedStatement will be created with SQL commands.
* All the values will be assigned into the specified index number and the PreparedStatement will be executed and the last.
* @param TimeLineID id number of the Timeline.
* @param TimeLineTitle used to assign a title for the Timeline.
* @param TimelineDescription used to assign a related description for the Timeline.
* @param TimelineStartDate used to assign the start date of the Timeline.
* @param TimeLineEndDate used to assign the end date of the Timeline.
* @exception SQLException SQL query is not correct or the JDBC class can not be found
* @throws ClassNotFoundException JDBC class not exist
*/
public void addTimeLine(int TimeLineID, String TimeLineTitle, String TimelineDescription, String TimelineStartDate, String TimeLineEndDate) throws ClassNotFoundException, SQLException {
if(con == null) {
// get connection
connectToDatabase();
}
PreparedStatement prep = con
.prepareStatement("insert into TimeLinesTable values(?,?,?,?,?,?);");
prep.setInt(2, TimeLineID);
prep.setString(3, TimeLineTitle);
prep.setString(4, TimelineDescription);
prep.setString(5, TimelineStartDate);
prep.setString(6, TimeLineEndDate);
prep.execute();
}
/**
* Create a SQL statement and execute the query as a ResultSet from the TasksTable.
* If the connection is equal to null a new connection will be created into database and tables.
* SQL Query description in this method:
* SELECT: Find the data.
* *: Means all the data.
* from: From target table.
* It is possible to change the method to SELECT a specified column name in the table.
* @exception SQLException SQL query is not correct or the JDBC class can not be found
* @throws ClassNotFoundException JDBC class not exist
* @return A set of all values based on the column names by SELECT TaskUniqueID, TTitle, TaskDesc, TaskStart, TaskEnd, TimeLineID from TasksTable
*/
public ResultSet displaySetOfAllTasks() throws SQLException, ClassNotFoundException {
if(con == null) {
// get connection
connectToDatabase();
}
Statement state = con.createStatement();
ResultSet res = state.executeQuery("select TaskUniqueID, TTitle, TaskDesc, TaskStart, TaskEnd, TimeLineID from TasksTable");
return res;
}
/**
* Create a SQL statement and execute the query as a ResultSet from the TimeLinesTable.
* If the connection is equal to null a new connection will be created into database and tables.
* SQL Query description in this method:
* SELECT: Find the data.
* *: Means all the data.
* from: From target table.
* It is possible to change the method to SELECT a specified column name in the table.
* @exception SQLException SQL query is not correct or the JDBC class can not be found
* @throws ClassNotFoundException JDBC class not exist
* @return A set of all values based on the column names by SELECT TimeLineID, TimeLineTitle, TimeLineDesc, TimeLineStart, TimeLineEnd from TimeLinesTable.
*/
public ResultSet displaySetOfAllTimeLines() throws SQLException, ClassNotFoundException {
if(con == null) {
// get connection
connectToDatabase();
}
Statement state = con.createStatement();
ResultSet res = state.executeQuery("select TimeLineID, TimeLineTitle, TimeLineDesc, TimeLineStart, TimeLineEnd from TimeLinesTable");
return res;
}
/**
* Print all the tasks by creating ResultSet.
* ResultSet might return incomparable column name if the tables are designed with different column names.
* while the ResultSet.next()!=false , ResultSet.getString("column name") will return the value of that column based on the current .next() pointer.
*/
public void printAllTasks(){
try {
ResultSet rs = this.displaySetOfAllTasks();
int TotalTasks=1;
System.out.println("Showing tasks:");
while (rs.next()) {
System.out.println("\tTask number "+TotalTasks+":");
System.out.println("\n\t\tTask unique ID: " +rs.getString("TaskUniqueID") + "\n\t\tTask Title: " + rs.getString("TTitle")+ "\n\t\tTask Description: " + rs.getString("TaskDesc")+"\n\t\tTask Starting Day: "+rs.getString("TaskStart")+"\n\t\tTask Ending Day: "+rs.getString("TaskEnd")+"\n\tThis Task Is Belongs To TimeLine With This Unique ID: "+rs.getString("TimeLineID")+"\n");
TotalTasks++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Print all the Timelines by iterating through the database Timeline table.
*/
public void printAllTimelines(){
try {
ResultSet rs = this.displaySetOfAllTimeLines();
int TotalTimeline=1;
System.out.println("Showing timelines:");
while (rs.next()) {
System.out.println("\nTimeLine number "+TotalTimeline+":");
System.out.println("\n\tTimeLine unique ID: " +rs.getString("TimeLineID") + "\n\t\tTimeLine Title: " + rs.getString("TimeLineTitle")+ "\n\t\tTimeLine Description: " + rs.getString("TimeLineDesc")+"\n\t\tTimeLine Starting Day: "+rs.getString("TimeLineStart")+"\n\t\tTimeLine Ending Day: "+rs.getString("TimeLineEnd")+"\n");
TotalTimeline++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Initialize table if not exist.
* @exception SQLException SQL query is not correct or the JDBC class can not be found
* @throws ClassNotFoundException JDBC class not exist
* Create a new type of 'table' into the database with the name of 'TasksTable'.
* Type of the columns can be assigned with "NAME integer". Here integer is the type of the variable NAME.
*/
private void initialiseTasksTable() throws SQLException {
if( !hasTaskTable ) {
hasTaskTable = true;
// check for database table
Statement TaskSt = con.createStatement();
ResultSet Task = TaskSt.executeQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='TasksTable'");
if( !Task.next()) {
// need to build the table
Statement state2 = con.createStatement();
state2.executeUpdate("create table TasksTable(id integer,"
+ "TaskUniqueID integer," + "TTitle varchar(60)," + "TaskDesc varchar(60)," + "TaskStart varchar(60)," + "TaskEnd varchar(60),"+ "TimeLineID integer," + "primary key (id));");
}
}
}
/**
* Initialize table if not exist.
* @exception SQLException SQL query is not correct or the JDBC class can not be found
* @throws ClassNotFoundException JDBC class not exist
* Create a new type of 'table' into the database with the name of 'TimeLinesTable'.
* Type of the columns can be assigned with "NAME integer". Here integer is the type of the variable NAME.
*/
private void initialiseTimeLinesTable() throws SQLException {
if( !hasTimelineTable ) {
hasTimelineTable = true;
// check for database table
Statement TimelineSt = con.createStatement();
ResultSet TimeLine = TimelineSt.executeQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='TimeLinesTable'");
// Statement TimelineSt = con.createStatement();
// ResultSet TimeLine = TimelineSt.executeQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='TimelinesTable'");
if( !TimeLine.next()) {
// need to build the table
Statement state2 = con.createStatement();
state2.executeUpdate("create table TimeLinesTable(id integer,"
+ "TimeLineID integer," + "TimeLineTitle varchar(60)," + "TimeLineDesc varchar(60)," + "TimeLineStart varchar(60)," + "TimeLineEnd varchar(60),"+ "primary key (id));");
}
}
}
/**
* Return all the Tasks that belongs to a Timeline by long id number.
* @exception SQLException SQL query is not correct or the JDBC class can not be found
* @throws ClassNotFoundException JDBC class not exist
* @param id The id number of the Timeline
* @return ResultSet A set of all data in the TaskTable by executing a SQL query
*/
public ResultSet getAllTaskofTheTimeline(long id) throws SQLException, ClassNotFoundException {
if(con == null) {
// get connection
connectToDatabase();
}
int i=(int) id;
Statement state = con.createStatement();
ResultSet res = state.executeQuery("select * from TasksTable where TimelineID='"+i+"'");
return res;
}
/**
* Remove all the data from TimeLinesTable.
* @exception SQLException SQL query is not correct or the JDBC class can not be found
* @throws ClassNotFoundException JDBC class not exist
*/
public void cleanTimelineTable() throws ClassNotFoundException, SQLException{
if(con==null){
connectToDatabase();
}
else{
Statement state2 = con.createStatement();
state2.executeUpdate("DELETE FROM TimeLinesTable");
}
}
/**
* Remove all the data from TasksTable.
* @exception SQLException SQL query is not correct or the JDBC class can not be found
* @throws ClassNotFoundException JDBC class not exist
*/
public void cleanTaskTable() throws ClassNotFoundException, SQLException{
if(con==null){
connectToDatabase();
}
else{
Statement state2 = con.createStatement();
state2.executeUpdate("DELETE FROM TasksTable");
}
}
/**
* @return Connection if not null.
* @exception SQLException Statement query should be correct at the time of executing and connection variable should be implemented correctly.
* @throws Exception There is no connection to get
*/
public Connection getConnection() throws Exception{
if(con!=null)
return con;
else
throw new Exception("There is no connection to get");
}
}