-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventManager.java
83 lines (76 loc) · 3.17 KB
/
EventManager.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
import java.sql.*;
public class EventManager {
private Connection connection;
public EventManager() {
try {
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/your_database_name", "your_username","your_password");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public void addEvent(String name, String location, Date date) {
try {
String query = "INSERT INTO events (name, location, date) VALUES (?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, name);
statement.setString(2, location);
statement.setDate(3, date);
statement.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public void updateEvent(String name, String location, Date date) {
try {
String query = "UPDATE events SET location = ?, date = ? WHERE name = ?";
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, location);
statement.setDate(2, date);
statement.setString(3, name);
statement.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public void searchEventByDate(Date date) {
try {
String query = "SELECT * FROM events WHERE date = ?";
PreparedStatement statement = connection.prepareStatement(query);
statement.setDate(1, date);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
System.out.println("Name: " + resultSet.getString("name") +
", Location: " + resultSet.getString("location") +
", Date: " + resultSet.getDate("date"));
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public void searchEventByLocation(String location) {
try {
String query = "SELECT * FROM events WHERE location = ?";
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, location);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
System.out.println("Name: " + resultSet.getString("name") +
", Location: " + resultSet.getString("location") +
", Date: " + resultSet.getDate("date"));
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public void deleteEvent(String name) {
try {
String query = "DELETE FROM events WHERE name = ?";
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, name);
statement.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}