This commit is contained in:
@@ -13,9 +13,14 @@ public class TaskManager {
|
||||
private Map<Integer, Task> taskMap;
|
||||
private MySql mysql;
|
||||
|
||||
public TaskManager(String user, String password, String port, String host) {
|
||||
public TaskManager(String user, String password, String db, String host, String port) {
|
||||
|
||||
if(user == null || password == null || db == null || host == null || port == null) {
|
||||
throw new NullPointerException("A argument is null");
|
||||
}
|
||||
|
||||
taskMap = new HashMap<>();
|
||||
this.mysql = new MySql(user, password, port, host);
|
||||
this.mysql = new MySql(user, password, db, host, port);
|
||||
this.mysql.connect();
|
||||
}
|
||||
/**
|
||||
|
||||
@@ -16,17 +16,19 @@ public class MySql {
|
||||
private String password;
|
||||
private String db;
|
||||
private String host;
|
||||
private String port;
|
||||
private Connection connection;
|
||||
|
||||
public MySql(String user, String password, String db, String host) {
|
||||
public MySql(String user, String password, String db, String host, String port) {
|
||||
this.user = user;
|
||||
this.password = password;
|
||||
this.db = db;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void connect() {
|
||||
String url = "jdbc:mysql://" + host + ":3306/" + db + "?autoReconnect=true&useSSL=false";
|
||||
String url = "jdbc:mysql://" + host + ":" + port + "/" + db + "?autoReconnect=true&useSSL=false";
|
||||
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
@@ -39,6 +41,9 @@ public class MySql {
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
if(connection == null) {
|
||||
throw new NullPointerException("You must first connect to the Database.");
|
||||
}
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("TRUNCATE task");
|
||||
stmt.executeUpdate();
|
||||
@@ -48,6 +53,9 @@ public class MySql {
|
||||
}
|
||||
|
||||
public int createTask(String name, String description) {
|
||||
if(connection == null) {
|
||||
throw new NullPointerException("You must first connect to the Database.");
|
||||
}
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("INSERT INTO task ( taskName, taskDescription) VALUES ( ? , ? )", Statement.RETURN_GENERATED_KEYS);
|
||||
stmt.setString(1, name);
|
||||
@@ -64,17 +72,11 @@ public class MySql {
|
||||
}
|
||||
}
|
||||
|
||||
// public void deleteTask(int id) {
|
||||
// try {
|
||||
// PreparedStatement stmt = connection.prepareStatement("DELETE task WHERE taskID=?");
|
||||
// stmt.setString(1, ""+id);
|
||||
// stmt.executeUpdate();
|
||||
// } catch (SQLException e) {
|
||||
// throw new SQLStatmentException("Cant delete data in database");
|
||||
// }
|
||||
// }
|
||||
|
||||
public boolean existTask(String name) {
|
||||
if(connection == null) {
|
||||
throw new NullPointerException("You must first connect to the Database.");
|
||||
}
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("Select * FROM task WHERE taskName=?");
|
||||
stmt.setString(1, name);
|
||||
@@ -86,6 +88,9 @@ public class MySql {
|
||||
}
|
||||
|
||||
public boolean existTask(int id) {
|
||||
if(connection == null) {
|
||||
throw new NullPointerException("You must first connect to the Database.");
|
||||
}
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("Select * FROM task WHERE taskID=?");
|
||||
stmt.setString(1, ""+id);
|
||||
@@ -97,6 +102,9 @@ public class MySql {
|
||||
}
|
||||
|
||||
public void deleteTask(String name) {
|
||||
if(connection == null) {
|
||||
throw new NullPointerException("You must first connect to the Database.");
|
||||
}
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("DELETE FROM task WHERE taskName=?");
|
||||
stmt.setString(1, name);
|
||||
@@ -107,6 +115,9 @@ public class MySql {
|
||||
}
|
||||
|
||||
public void updateDescription(int id, String description) {
|
||||
if(connection == null) {
|
||||
throw new NullPointerException("You must first connect to the Database.");
|
||||
}
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("UPDATE task SET taskDescription=? WHERE taskID=?");
|
||||
stmt.setString(1, description);
|
||||
@@ -117,18 +128,11 @@ public class MySql {
|
||||
throw new SQLStatmentException("Cant update data in database");
|
||||
}
|
||||
}
|
||||
// public void updateDescription(String name, String description) {
|
||||
// try {
|
||||
// PreparedStatement stmt = connection.prepareStatement("UPDATE task SET taskDescription=? WHERE taskName=?");
|
||||
// stmt.setString(1, description);
|
||||
// stmt.setString(2, name);
|
||||
// stmt.executeUpdate();
|
||||
//
|
||||
// } catch (SQLException e) {
|
||||
// throw new SQLStatmentException("Cant update data in database");
|
||||
// }
|
||||
// }
|
||||
|
||||
public void updateStatus(int id, TaskStatus status) {
|
||||
if(connection == null) {
|
||||
throw new NullPointerException("You must first connect to the Database.");
|
||||
}
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("UPDATE task SET taskStatus=? WHERE taskID=?");
|
||||
stmt.setString(1, status.name());
|
||||
@@ -139,19 +143,12 @@ public class MySql {
|
||||
throw new SQLStatmentException("Cant update data in database");
|
||||
}
|
||||
}
|
||||
// public void updateStatus(String name, TaskStatus status) {
|
||||
// try {
|
||||
// PreparedStatement stmt = connection.prepareStatement("UPDATE task SET taskStatus=? WHERE taskName=?");
|
||||
// stmt.setString(1, status.name());
|
||||
// stmt.setString(2, name);
|
||||
// stmt.executeUpdate();
|
||||
//
|
||||
// } catch (SQLException e) {
|
||||
// throw new SQLStatmentException("Cant update data in database");
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
public void updateWorker(int id, String worker) {
|
||||
if(connection == null) {
|
||||
throw new NullPointerException("You must first connect to the Database.");
|
||||
}
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("UPDATE task SET taskWorker=? WHERE taskID=?");
|
||||
stmt.setString(1, worker);
|
||||
@@ -163,19 +160,12 @@ public class MySql {
|
||||
}
|
||||
}
|
||||
|
||||
// public void updateWorker(String name, String worker) {
|
||||
// try {
|
||||
// PreparedStatement stmt = connection.prepareStatement("UPDATE task SET taskWorker=? WHERE taskName=?");
|
||||
// stmt.setString(1, worker);
|
||||
// stmt.setString(2, name);
|
||||
// stmt.executeUpdate();
|
||||
//
|
||||
// } catch (SQLException e) {
|
||||
// throw new SQLStatmentException("Cant update data in database");
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
public Task getTask(int id) {
|
||||
if(connection == null) {
|
||||
throw new NullPointerException("You must first connect to the Database.");
|
||||
}
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("SELECT * FROM task WHERE taskID=?");
|
||||
stmt.setString(1, ""+id);
|
||||
@@ -190,6 +180,9 @@ public class MySql {
|
||||
}
|
||||
}
|
||||
public Task getTask(String name) {
|
||||
if(connection == null) {
|
||||
throw new NullPointerException("You must first connect to the Database.");
|
||||
}
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("SELECT * FROM task WHERE taskName=?");
|
||||
stmt.setString(1, name);
|
||||
@@ -206,6 +199,9 @@ public class MySql {
|
||||
}
|
||||
|
||||
public List<Task> getTaskList() {
|
||||
if(connection == null) {
|
||||
throw new NullPointerException("You must first connect to the Database.");
|
||||
}
|
||||
try {
|
||||
PreparedStatement stmt = connection.prepareStatement("SELECT * FROM task");
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
Reference in New Issue
Block a user