-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.java
122 lines (101 loc) · 3.34 KB
/
Database.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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class Database
{
private String url = "jdbc:mysql://localhost/Calendar";
private String user = "root";
private String pass = "";
// private String user = "user";
// private String pass = "#1#2#3%1%2%3";
private Statement statement;
public Database()
{
try
{
Connection connection = DriverManager.getConnection(url, user, pass);
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public ArrayList<Event> getEvents(String date)
{
ArrayList<Event> events = new ArrayList<>();
String select = "SELECT * FROM `calendar` WHERE `Date` = '"+date+"';";
try
{
ResultSet rs = statement.executeQuery(select);
while (rs.next())
{
Event e = new Event();
e.setID(rs.getInt("ID"));
e.setTitle(rs.getString("Title"));
e.setDescription(rs.getString("Description"));
e.setDateTimeFromString(rs.getString("Date")+" | "+rs.getString("Time"));
events.add(e);
}
}
catch(SQLException exception)
{
exception.printStackTrace();
}
return events;
}
public boolean hasEvent(String date)
{
boolean hasEvent = false;
String select = "SELECT * From `calendar` WHERE `Date` = '"+ date+ "';";
try
{
ResultSet rs = statement.executeQuery(select);
hasEvent = rs.next();
}
catch (SQLException exception)
{
exception.printStackTrace();
}
return hasEvent;
}
public void createEvent(Event e)
{
String insert = "INSERT INTO `calendar` (`Title`, `Description`, `Date`, `Time`)" + "VALUES ('"+e.getTitle()+"', '"+e.getDescription()+"', '"+e.getDateToString() + "', '" + e.getTimeToString()+"');";
try
{
statement.execute(insert);
}
catch (SQLException exception)
{
exception.printStackTrace();
}
}
public void updateEvent(Event e)
{
String update = "UPDATE `calendar` SET `Title` = '"+e.getTitle()+"', `Description` = '"+e.getDescription()+"', `Date` = '"+e.getDateToString()+"', `Time` = '"+e.getTimeToString()+"' WHERE `ID` = "+e.getID()+" ;";
try
{
statement.execute(update);
}
catch (SQLException exception)
{
exception.printStackTrace();
}
}
public void deleteEvent(int ID)
{
String delete = "DELETE FROM `calendar` WHERE `ID` = "+ID+" ;";
try
{
statement.execute(delete);
}
catch (SQLException exception)
{
exception.printStackTrace();
}
}
}