├── .DS_Store
├── .gitignore
├── .idea
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── libraries
│ ├── Maven__junit_junit_4_11.xml
│ ├── Maven__mysql_mysql_connector_java_5_1_21.xml
│ └── Maven__org_hamcrest_hamcrest_core_1_3.xml
├── misc.xml
├── modules.xml
├── uiDesigner.xml
└── vcs.xml
├── Java-Calendar.iml
├── README.md
├── java_calendar.sql
├── pom.xml
├── src
├── .DS_Store
├── main
│ ├── .DS_Store
│ └── java
│ │ ├── .DS_Store
│ │ └── edu
│ │ ├── .DS_Store
│ │ └── avans
│ │ ├── .DS_Store
│ │ └── library
│ │ ├── businesslogic
│ │ └── CalendarManager.java
│ │ ├── datastorage
│ │ ├── AppointmentDAO.java
│ │ ├── CategoryDAO.java
│ │ └── DatabaseConnection.java
│ │ ├── domain
│ │ ├── Appointment.java
│ │ ├── CCalendar.java
│ │ ├── CDay.java
│ │ ├── CMonth.java
│ │ ├── CWeek.java
│ │ ├── CYear.java
│ │ └── Category.java
│ │ ├── main
│ │ └── Main.java
│ │ └── presentation
│ │ ├── AppointmentFrame.java
│ │ ├── AppointmentPanel.java
│ │ ├── CalendarPanel.java
│ │ ├── DayDetailPanel.java
│ │ ├── DayPanel.java
│ │ ├── MainFrame.java
│ │ ├── MainPanel.java
│ │ ├── MonthPanel.java
│ │ └── SpringUtilities.java
└── test
│ └── java
│ ├── calendar
│ └── CalendarTest.java
│ ├── day
│ └── DayTest.java
│ ├── month
│ └── MonthTest.java
│ └── week
│ └── WeekTest.java
└── target
├── classes
└── edu
│ └── avans
│ └── library
│ ├── businesslogic
│ └── CalendarManager.class
│ ├── datastorage
│ ├── AppointmentDAO.class
│ ├── CategoryDAO.class
│ └── DatabaseConnection.class
│ ├── domain
│ ├── Appointment.class
│ ├── CCalendar.class
│ ├── CDay.class
│ ├── CMonth.class
│ ├── CWeek.class
│ ├── CYear.class
│ └── Category.class
│ ├── main
│ └── Main.class
│ └── presentation
│ ├── AppointmentFrame.class
│ ├── AppointmentPanel$saveAppointmentHandler.class
│ ├── AppointmentPanel.class
│ ├── CalendarPanel.class
│ ├── DayDetailPanel$deleteAppointmentHandler.class
│ ├── DayDetailPanel.class
│ ├── DayPanel$addAppointmentsButtonHandler.class
│ ├── DayPanel$viewAppointmentsButtonHandler.class
│ ├── DayPanel.class
│ ├── MainFrame$1.class
│ ├── MainFrame.class
│ ├── MainPanel$currentMonthButtonHandler.class
│ ├── MainPanel$dateFieldHandler.class
│ ├── MainPanel$nextMonthButtonHandler.class
│ ├── MainPanel$prevMonthButtonHandler.class
│ ├── MainPanel$resizeListener.class
│ ├── MainPanel.class
│ ├── MonthPanel.class
│ └── SpringUtilities.class
└── test-classes
├── appointment
└── AppointmentTest.class
├── calendar
└── CalendarTest.class
├── day
└── DayTest.class
└── week
└── WeekTest.class
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bramdehart/java-calendar/fc4973e95ded13790010163849fc4319d2715fb9/.DS_Store
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | .idea/workspace.xml
3 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__junit_junit_4_11.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__mysql_mysql_connector_java_5_1_21.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
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 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/uiDesigner.xml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Java-Calendar.iml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Java Calendar made with the Swing GUI toolkit
2 |
3 | ## Installation
4 | An SQL-import is required before using the application. See attached SQL-file.
5 |
6 | ## Navigation
7 | Navigation between different monthpanels can be done in two ways:
8 | * Entering a date in the toppanel
9 | * Using the navigation buttons (previous / current / next month)
10 |
11 | ## Add an appointment
12 | An appointment can be added by clicking on "Add" within the desired daypanel.
13 | The fields "Name", "Start time" and "End time" are required. The others are optional.
14 |
15 | ## Delete / View an appointment
16 | To delete an appointment, click on "View" within the desired daypanel. The daydetail-panel on the right will show the appointments of the selected day. Confirm your removal by clicking on "Delete".
17 |
18 | ## Color descriptions
19 | * Yellow: 0 - 5 appointments
20 | * Orange: 5 - 10 appointments
21 | * Red: > 10 appointments
22 | * Green: current day
23 | * Blue: active day
24 |
25 | ## Screenshots
26 | 
27 |
--------------------------------------------------------------------------------
/java_calendar.sql:
--------------------------------------------------------------------------------
1 | # ************************************************************
2 | # Sequel Pro SQL dump
3 | # Version 4499
4 | #
5 | # http://www.sequelpro.com/
6 | # https://github.com/sequelpro/sequelpro
7 | #
8 | # Host: 127.0.0.1 (MySQL 5.6.14)
9 | # Database: javaCalendar
10 | # Generation Time: 2017-01-26 19:18:28 +0000
11 | # ************************************************************
12 |
13 |
14 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
15 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
16 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
17 | /*!40101 SET NAMES utf8 */;
18 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
19 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
20 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
21 |
22 |
23 | # Dump of table appointment
24 | # ------------------------------------------------------------
25 |
26 | DROP TABLE IF EXISTS `appointment`;
27 |
28 | CREATE TABLE `appointment` (
29 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
30 | `title` varchar(255) NOT NULL DEFAULT '',
31 | `description` varchar(255) DEFAULT NULL,
32 | `location` varchar(255) DEFAULT NULL,
33 | `date` date NOT NULL,
34 | `startTime` time NOT NULL,
35 | `endTime` time NOT NULL,
36 | PRIMARY KEY (`id`)
37 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
38 |
39 | LOCK TABLES `appointment` WRITE;
40 | /*!40000 ALTER TABLE `appointment` DISABLE KEYS */;
41 |
42 | INSERT INTO `appointment` (`id`, `title`, `description`, `location`, `date`, `startTime`, `endTime`)
43 | VALUES
44 | (79,'Event 1',NULL,NULL,'2016-12-21','09:00:00','10:00:00'),
45 | (80,'Event 2',NULL,NULL,'2016-12-21','10:00:00','11:00:00'),
46 | (81,'Event 3',NULL,NULL,'2016-12-21','12:00:00','13:00:00'),
47 | (82,'Event 4',NULL,NULL,'2016-12-21','13:00:00','14:00:00'),
48 | (83,'Event 5',NULL,NULL,'2016-12-21','15:00:00','16:00:00'),
49 | (84,'Event 6',NULL,NULL,'2016-12-21','16:00:00','17:00:00'),
50 | (85,'Event 7',NULL,NULL,'2016-12-21','17:00:00','18:00:00'),
51 | (86,'Event 8',NULL,NULL,'2016-12-21','18:00:00','19:00:00'),
52 | (87,'Event 9',NULL,NULL,'2016-12-21','19:00:00','20:00:00'),
53 | (88,'Event 10',NULL,NULL,'2016-12-21','20:00:00','21:00:00'),
54 | (89,'Event 11',NULL,NULL,'2016-12-21','21:00:00','22:00:00'),
55 | (90,'Event 1',NULL,NULL,'2016-12-09','09:00:00','10:00:00'),
56 | (91,'Event 2',NULL,NULL,'2016-12-09','10:00:00','11:00:00'),
57 | (92,'Event 3',NULL,NULL,'2016-12-09','12:00:00','13:00:00'),
58 | (93,'Event 4',NULL,NULL,'2016-12-09','13:00:00','14:00:00'),
59 | (94,'Event 5',NULL,NULL,'2016-12-09','15:00:00','16:00:00'),
60 | (95,'Event 6',NULL,NULL,'2016-12-09','16:00:00','17:00:00'),
61 | (96,'Event 7',NULL,NULL,'2016-12-09','17:00:00','18:00:00'),
62 | (97,'Event 1',NULL,NULL,'2016-12-26','09:00:00','10:00:00'),
63 | (98,'Event 2',NULL,NULL,'2016-12-26','10:00:00','11:00:00'),
64 | (99,'Event 3',NULL,NULL,'2016-12-26','12:00:00','13:00:00'),
65 | (100,'Event 4',NULL,NULL,'2016-12-26','13:00:00','14:00:00'),
66 | (101,'Event 1',NULL,NULL,'2016-11-30','09:00:00','10:00:00'),
67 | (103,'Event 1',NULL,NULL,'2016-11-28','09:00:00','10:00:00'),
68 | (104,'Event 2',NULL,NULL,'2016-11-28','10:00:00','11:00:00'),
69 | (105,'Event 1',NULL,NULL,'2017-01-04','09:00:00','10:00:00'),
70 | (107,'Event 1',NULL,NULL,'2017-01-05','09:00:00','10:00:00'),
71 | (108,'Event 2',NULL,NULL,'2017-01-05','10:00:00','11:00:00'),
72 | (109,'Event 3',NULL,NULL,'2017-01-05','11:00:00','12:00:00'),
73 | (110,'Event 4',NULL,NULL,'2017-01-05','12:00:00','13:00:00'),
74 | (111,'Event 5',NULL,NULL,'2017-01-05','13:00:00','14:00:00'),
75 | (112,'Event 6',NULL,NULL,'2017-01-05','14:00:00','15:00:00'),
76 | (113,'Event 7',NULL,NULL,'2017-01-05','15:00:00','16:00:00'),
77 | (114,'Event 8',NULL,NULL,'2017-01-05','16:00:00','17:00:00'),
78 | (115,'New event',NULL,NULL,'2016-11-17','09:00:00','10:00:00'),
79 | (118,'jty','rty','ghj','2017-01-02','10:00:00','11:00:00');
80 |
81 | /*!40000 ALTER TABLE `appointment` ENABLE KEYS */;
82 | UNLOCK TABLES;
83 |
84 |
85 | # Dump of table category
86 | # ------------------------------------------------------------
87 |
88 | DROP TABLE IF EXISTS `category`;
89 |
90 | CREATE TABLE `category` (
91 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
92 | `title` varchar(255) NOT NULL DEFAULT '',
93 | `color` varchar(6) NOT NULL DEFAULT '',
94 | PRIMARY KEY (`id`)
95 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
96 |
97 |
98 |
99 |
100 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
101 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
102 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
103 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
104 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
105 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
106 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 | edu.avans.library
6 | JavaCalendar
7 | JavaCalendar
8 | jar
9 | 1.0.0.RELEASE
10 |
11 | UTF-8
12 | 1.8
13 |
14 |
15 |
16 |
17 | junit
18 | junit
19 | 4.11
20 | test
21 |
22 |
23 |
24 |
25 | mysql
26 | mysql-connector-java
27 | 5.1.21
28 |
29 |
30 |
31 |
32 |
33 | JavaCalendar
34 |
35 |
36 |
37 | org.apache.maven.plugins
38 | maven-compiler-plugin
39 | 2.3.2
40 |
41 | ${java-version}
42 | ${java-version}
43 | -Xlint:all
44 | true
45 | true
46 |
47 |
48 |
49 | org.codehaus.mojo
50 | exec-maven-plugin
51 | 1.2.1
52 |
53 | org.test.int1.Main
54 |
55 |
56 |
57 | org.apache.maven.plugins
58 | maven-jar-plugin
59 | 2.4
60 |
61 |
62 |
63 | test-jar
64 |
65 |
66 |
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/src/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bramdehart/java-calendar/fc4973e95ded13790010163849fc4319d2715fb9/src/.DS_Store
--------------------------------------------------------------------------------
/src/main/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bramdehart/java-calendar/fc4973e95ded13790010163849fc4319d2715fb9/src/main/.DS_Store
--------------------------------------------------------------------------------
/src/main/java/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bramdehart/java-calendar/fc4973e95ded13790010163849fc4319d2715fb9/src/main/java/.DS_Store
--------------------------------------------------------------------------------
/src/main/java/edu/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bramdehart/java-calendar/fc4973e95ded13790010163849fc4319d2715fb9/src/main/java/edu/.DS_Store
--------------------------------------------------------------------------------
/src/main/java/edu/avans/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bramdehart/java-calendar/fc4973e95ded13790010163849fc4319d2715fb9/src/main/java/edu/avans/.DS_Store
--------------------------------------------------------------------------------
/src/main/java/edu/avans/library/businesslogic/CalendarManager.java:
--------------------------------------------------------------------------------
1 | package edu.avans.library.businesslogic;
2 |
3 | import edu.avans.library.datastorage.*;
4 | import edu.avans.library.domain.Appointment;
5 | import java.sql.Time;
6 | import java.util.ArrayList;
7 | import java.util.Date;
8 |
9 | /**
10 | * The CalendarManager
manages communication between the different classes in the presentation, domain and data-storage level.
11 | * When CalendarManager
is initialized, a database connection is ensured.
12 | * @author Bram de Hart
13 | * @version 1.0
14 | */
15 | public class CalendarManager {
16 | private AppointmentDAO appointment = new AppointmentDAO();
17 |
18 | /**
19 | * Gets all appointments of a given date.
20 | * @param date the date the appointments needs to be retrieved from
21 | * @return arraylist of appointments
22 | */
23 | public ArrayList getAppointments(Date date) {
24 | return appointment.getAppointments(date);
25 | }
26 |
27 | /**
28 | * Inserts a new appointment in the database.
29 | * @param date the date the appointment needs to be added to
30 | * @param title the title of the appointment
31 | * @param location the location of the appointment
32 | * @param description a description of the appointment
33 | * @param startTime the starttime of the appointment
34 | * @param endTime the endtime of the appointment
35 | * @return boolean
36 | */
37 | public boolean addAppointment(Date date, String title, String description, String location, Time startTime, Time endTime) {
38 | return appointment.addAppointment(date, title, description, location, startTime, endTime);
39 | }
40 |
41 | /**
42 | * Deletes an appointment from the database.
43 | * @param appointmentId the id of the appointment that needs to be removed
44 | * @return boolean
45 | */
46 | public boolean deleteAppointment(Integer appointmentId) {
47 | return appointment.deleteAppointment(appointmentId);
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/edu/avans/library/datastorage/AppointmentDAO.java:
--------------------------------------------------------------------------------
1 | package edu.avans.library.datastorage;
2 |
3 | import java.sql.ResultSet;
4 | import java.sql.SQLException;
5 | import java.text.SimpleDateFormat;
6 | import java.util.ArrayList;
7 | import java.util.Date;
8 | import java.sql.Time;
9 | import java.util.List;
10 | import edu.avans.library.domain.Appointment;
11 |
12 | /**
13 | * AppointmentDAO
handles database requests that are related to Appointment
.
14 | * It is called by the CalendarManager
.
15 | * @author Bram de Hart
16 | * @version 1.0
17 | * @see edu.avans.library.domain.Appointment
18 | * @see edu.avans.library.businesslogic.CalendarManager
19 | */
20 | public class AppointmentDAO {
21 | private DatabaseConnection connection = new DatabaseConnection("jdbc:mysql://127.0.0.1/java_calendar", "root","root");
22 |
23 | /**
24 | * Gets all appointments of a given date.
25 | * @param date the date the appointments needs to be retrieved from
26 | */
27 | public ArrayList getAppointments(Date date) {
28 | ArrayList appointments = new ArrayList();
29 | String dateString = new SimpleDateFormat("yyyy-MM-dd").format(date);
30 |
31 | if(date != null) {
32 | // First open a database connnection
33 | if (connection.open()) {
34 | // If a connection was successfully setup, execute the SELECT statement.
35 | ResultSet resultset = connection.executeQuery(
36 | "SELECT * FROM appointment WHERE date = '" + dateString + "' ORDER BY startTime;");
37 |
38 | if (resultset != null) {
39 | try {
40 | while (resultset.next()) {
41 | // get fields
42 | Integer appointmentId = resultset.getInt("id");
43 | String title = resultset.getString("title");
44 | String description = resultset.getString("description");
45 | String location = resultset.getString("location");
46 | Time startTime = resultset.getTime("startTime");
47 | Time endTime = resultset.getTime("endTime");
48 |
49 | // add appointment to list
50 | Appointment appointment = new Appointment(appointmentId, title, description, location, date, startTime, endTime);
51 | appointments.add(appointment);
52 | }
53 | } catch (SQLException e) {
54 | System.out.println(e);
55 | }
56 | }
57 |
58 | // We had a database connection opened. Since we're finished,
59 | // we need to close it.
60 | connection.close();
61 | }
62 | }
63 |
64 | return appointments;
65 | }
66 |
67 | /**
68 | * Inserts a new appointment in the database.
69 | * @param date the date the appointment needs to be added to
70 | * @param title the title of the appointment
71 | * @param location the location of the appointment
72 | * @param description a description of the appointment
73 | * @param startTime the starttime of the appointment
74 | * @param endTime the endtime of the appointment
75 | */
76 | public Boolean addAppointment(Date date, String title, String description, String location, Time startTime, Time endTime) {
77 | List resultIds = new ArrayList();
78 |
79 | if (date != null && title != null && date != null && startTime != null && endTime != null) {
80 | // First open a database connnection
81 | if (connection.open()) {
82 | // If a connection was successfully setup, execute the statement.
83 | resultIds = connection.executePrepared("INSERT INTO appointment (title, description, location, date, startTime, endTime) VALUES(?,?,?,?,?,?);",
84 | title,description,location,date,startTime,endTime);
85 | }
86 |
87 | // We had a database connection opened. Since we're finished,
88 | // we need to close it.
89 | connection.close();
90 | }
91 | return resultIds.isEmpty();
92 | }
93 |
94 | /**
95 | * Deletes an appointment from the database.
96 | * @param appointmentId the id of the appointment.
97 | */
98 | public boolean deleteAppointment(Integer appointmentId) {
99 | boolean result = false;
100 | if (appointmentId != null) {
101 | // First open a database connnection
102 | if (connection.open()) {
103 | // If a connection was successfully setup, execute the statement.
104 | result = connection.execute("DELETE FROM appointment WHERE id = '"+appointmentId+"';");
105 | }
106 |
107 | // We had a database connection opened. Since we're finished,
108 | // we need to close it.
109 | connection.close();
110 | }
111 | return result;
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/src/main/java/edu/avans/library/datastorage/CategoryDAO.java:
--------------------------------------------------------------------------------
1 | package edu.avans.library.datastorage;
2 |
3 | /**
4 | * CategoryDAO
handles database requests that are related to Category
.
5 | * It is called by the CalendarManager
.
6 | * @author Bram de Hart
7 | * @version 1.0
8 | * @see edu.avans.library.domain.Category
9 | * @see edu.avans.library.businesslogic.CalendarManager
10 | */
11 | public class CategoryDAO {
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/edu/avans/library/datastorage/DatabaseConnection.java:
--------------------------------------------------------------------------------
1 | package edu.avans.library.datastorage;
2 |
3 | import java.sql.*;
4 | import java.util.ArrayList;
5 | import java.util.List;
6 | import java.util.logging.Level;
7 | import java.util.logging.Logger;
8 |
9 | /**
10 | * A wrapper class to make database actions easier to write.
11 | */
12 | public class DatabaseConnection {
13 | private static final Logger LOGGER;
14 |
15 | static {
16 | LOGGER = Logger.getLogger(DatabaseConnection.class.getName());
17 | }
18 |
19 | private final String connectionString;
20 | private final String user;
21 | private final String password;
22 | private Connection connection;
23 |
24 | /**
25 | * Initializes a new DatabaseConnection instance using the specified connection string.
26 | *
27 | * @param connectionString The connection string to create the connection with.
28 | * @param user The user to create the connection with.
29 | * @param password The password to create the connection with.
30 | */
31 | public DatabaseConnection(String connectionString, String user, String password) {
32 | this.connectionString = connectionString;
33 | this.user = user;
34 | this.password = password;
35 | }
36 |
37 | /**
38 | * Opens the connection.
39 | *
40 | * @return true if successful; otherwise, false.
41 | */
42 | public boolean open() {
43 | if (isOpen()) {
44 | return true;
45 | }
46 |
47 | try {
48 | connection = DriverManager.getConnection(connectionString, user, password);
49 | return true;
50 | } catch (Exception e) {
51 | LOGGER.log(Level.SEVERE, "An exception occurred while trying to open the database connection.", e);
52 | return false;
53 | }
54 | }
55 |
56 | /**
57 | * Checks if the connection is currently open.
58 | * @return true if open; otherwise, false.
59 | */
60 | public boolean isOpen() {
61 | if (connection == null) {
62 | return false;
63 | }
64 |
65 | try {
66 | return !connection.isClosed();
67 | } catch (Exception e) {
68 | LOGGER.log(Level.SEVERE, "An exception occurred while trying to check if the database connection is open.", e);
69 | return false;
70 | }
71 | }
72 |
73 | /**
74 | * Closes the connection.
75 | * @return true if successful; otherwise, false.
76 | */
77 | public boolean close() {
78 | if (!isOpen()) {
79 | return true;
80 | }
81 |
82 | try {
83 | connection.close();
84 | connection = null;
85 | return true;
86 | } catch (Exception e) {
87 | LOGGER.log(Level.SEVERE, "An exception occurred while trying to close the database connection.", e);
88 | return false;
89 | }
90 | }
91 |
92 | /**
93 | * Executes SQL that does not return any results.
94 | * @param sql The sql to be executed
95 | * @return true if successful; otherwise, false.
96 | */
97 | public boolean execute(String sql) {
98 | try {
99 | Statement statement = connection.createStatement();
100 | return statement.execute(sql);
101 | } catch (Exception e) {
102 | LOGGER.log(Level.SEVERE, "An exception occurred while trying to execute the statement.", e);
103 | return false;
104 | }
105 | }
106 |
107 | /**
108 | * Executes SQL that does not return any results.
109 | * @param sql The sql to be executed
110 | * @return true if successful; otherwise, false.
111 | */
112 | public int[] executeBatch(String... sql) {
113 | try {
114 | Statement statement = connection.createStatement();
115 | for (String sqlLine : sql) {
116 | statement.addBatch(sqlLine);
117 | }
118 | return statement.executeBatch();
119 | } catch (Exception e) {
120 | LOGGER.log(Level.SEVERE, "An exception occurred while trying to execute the statement.", e);
121 | return new int[0];
122 | }
123 | }
124 |
125 | /**
126 | * Executes an SQL query that returns results.
127 | * @param sql The sql to be executed
128 | * @return The result set returned by the query.
129 | */
130 | public ResultSet executeQuery(String sql) {
131 | try {
132 | Statement statement = connection.createStatement();
133 | return statement.executeQuery(sql);
134 | } catch (Exception e) {
135 | LOGGER.log(Level.SEVERE, "An exception occurred while trying to execute the query.", e);
136 | return null;
137 | }
138 | }
139 |
140 | /**
141 | * Creates and executes an SQL string by placing the specified items into the SQL format string and returns the generated keys.
142 | * @param sqlFormat The SQL string to place the items in.
143 | * @param items The items to place into the SQL format string.
144 | * @return The generated keys as an integer List.
145 | */
146 | public List executePrepared(String sqlFormat, Object... items) {
147 | try {
148 | PreparedStatement statement = connection.prepareStatement(sqlFormat, Statement.RETURN_GENERATED_KEYS);
149 |
150 | int itemCount = items.length;
151 |
152 | for (int i = 0; i < itemCount; i++) {
153 | statement.setObject(i + 1, items[i]);
154 | }
155 |
156 | statement.execute();
157 |
158 | List generatedIds = new ArrayList<>();
159 |
160 | ResultSet generatedKeys = statement.getGeneratedKeys();
161 | while (generatedKeys.next()) {
162 | generatedIds.add(generatedKeys.getInt(1));
163 | }
164 |
165 | return generatedIds;
166 | } catch (Exception e) {
167 | LOGGER.log(Level.SEVERE, "An exception occurred while trying to execute the prepared statement.", e);
168 | return new ArrayList<>();
169 | }
170 | }
171 |
172 | /**
173 | * Creates and executes an SQL string for each set of items in the specified batch by placing the specified items into the SQL format string and returns the number of affected rows.
174 | * @param sqlFormat The SQL string to place the items in.
175 | * @param batch The batch with item collections to place in the SQL string.
176 | * @return The number of affected rows as an integer List.
177 | */
178 | public int[] executePreparedBatch(String sqlFormat, List