-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySqlConnection.java
More file actions
103 lines (79 loc) · 3.8 KB
/
Copy pathMySqlConnection.java
File metadata and controls
103 lines (79 loc) · 3.8 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
import java.sql.*;
import java.time.Duration;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class MySqlConnection {
private static final String URL = "jdbc:mysql://localhost:3306/mysql2?user=root&password=Hoangson09112004@";
private static final String USERNAME = "root";
private static final String PASSWORD = "Hoangson09112004@";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
public static List<Task> getAllTasks() throws SQLException {
List<Task> tasks = new ArrayList<>();
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String query = "SELECT * FROM tasks";
try (PreparedStatement statement = connection.prepareStatement(query)) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String description = resultSet.getString("description");
Status status = Status.valueOf(resultSet.getString("status"));
Priority priority = Priority.valueOf(resultSet.getString("priority"));
LocalDate dueDate = resultSet.getDate("due_date").toLocalDate();
Duration duration = Duration.ofMinutes(resultSet.getLong("duration"));
String notes = resultSet.getString("notes");
Task task = new Task(id, name, description, status, priority, dueDate, duration, notes);
tasks.add(task);
}
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}
connection.close();
return tasks;
}
public static void addTask(Task task) throws SQLException {
Connection connection = getConnection();
String query = "INSERT INTO tasks (name, description, status, priority, due_date) " +
"VALUES (?, ?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, task.getName());
statement.setString(2, task.getDescription());
statement.setString(3, task.getStatus().toString());
statement.setString(4, task.getPriority().toString());
statement.setDate(5, Date.valueOf(task.getDueDate()));
statement.executeUpdate();
closeConnection(connection, statement);
}
public static void updateTask(Task task) throws SQLException {
Connection connection = getConnection();
String query = "UPDATE tasks SET name = ?, description = ?, status = ?, priority = ?, due_date = ? " +
"WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, task.getName());
statement.setString(2, task.getDescription());
statement.setString(3, task.getStatus().toString());
statement.setString(4, task.getPriority().toString());
statement.setDate(5, Date.valueOf(task.getDueDate()));
statement.setInt(6, task.getId());
statement.executeUpdate();
closeConnection(connection, statement);
}
public static void deleteTask(int taskId) throws SQLException {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("DELETE FROM tasks WHERE id=?")) {
stmt.setInt(1, taskId);
stmt.executeUpdate();
}
}
private static void closeConnection(Connection connection, PreparedStatement statement) throws SQLException {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
}