Fix bugs lose connection with DB
This commit is contained in:
@@ -9,7 +9,8 @@ import java.util.*;
|
|||||||
|
|
||||||
public class DataBase {
|
public class DataBase {
|
||||||
private static DataBase instance;
|
private static DataBase instance;
|
||||||
private final Connection connection;
|
private Connection connection;
|
||||||
|
private final String login, password;
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
try {
|
try {
|
||||||
@@ -20,15 +21,21 @@ public class DataBase {
|
|||||||
instance = null;
|
instance = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DataBase(String login, String pass) {
|
private void OpenConnection() {
|
||||||
try {
|
try {
|
||||||
connection = DriverManager.getConnection(API_KEYS.DB_URL, login, pass);
|
connection = DriverManager.getConnection(API_KEYS.DB_URL, login, password);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.out.println("[!] DB: " + e.getMessage());
|
System.out.println("[!] DB: " + e.getMessage());
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private DataBase(String login, String password) {
|
||||||
|
this.login = login;
|
||||||
|
this.password = password;
|
||||||
|
OpenConnection();
|
||||||
|
}
|
||||||
|
|
||||||
public static DataBase getInstance() {
|
public static DataBase getInstance() {
|
||||||
if(instance == null) {
|
if(instance == null) {
|
||||||
instance = new DataBase(API_KEYS.DB_LOGIN, API_KEYS.DB_PASSWORD);
|
instance = new DataBase(API_KEYS.DB_LOGIN, API_KEYS.DB_PASSWORD);
|
||||||
@@ -37,6 +44,9 @@ public class DataBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public User SaveUser(String login, String password) throws SQLException {
|
public User SaveUser(String login, String password) throws SQLException {
|
||||||
|
if(!connection.isValid(100))
|
||||||
|
OpenConnection();
|
||||||
|
|
||||||
PreparedStatement saveUser_stmt = connection.prepareStatement("INSERT INTO users(login, password) VALUE (?, ?)");
|
PreparedStatement saveUser_stmt = connection.prepareStatement("INSERT INTO users(login, password) VALUE (?, ?)");
|
||||||
saveUser_stmt.setString(1, login);
|
saveUser_stmt.setString(1, login);
|
||||||
saveUser_stmt.setString(2, password);
|
saveUser_stmt.setString(2, password);
|
||||||
@@ -51,6 +61,9 @@ public class DataBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void AddTask(Task task, User user) throws SQLException {
|
public void AddTask(Task task, User user) throws SQLException {
|
||||||
|
if(!connection.isValid(100))
|
||||||
|
OpenConnection();
|
||||||
|
|
||||||
PreparedStatement addTask = connection.prepareStatement("INSERT INTO tasks(ID," +
|
PreparedStatement addTask = connection.prepareStatement("INSERT INTO tasks(ID," +
|
||||||
" TaskName," +
|
" TaskName," +
|
||||||
" TaskDescription," +
|
" TaskDescription," +
|
||||||
@@ -69,6 +82,9 @@ public class DataBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateTask(Task task, User user) throws SQLException {
|
public void UpdateTask(Task task, User user) throws SQLException {
|
||||||
|
if(!connection.isValid(100))
|
||||||
|
OpenConnection();
|
||||||
|
|
||||||
PreparedStatement addTask = connection.prepareStatement("UPDATE tasks SET TaskName = ?," +
|
PreparedStatement addTask = connection.prepareStatement("UPDATE tasks SET TaskName = ?," +
|
||||||
" TaskDescription = ?," +
|
" TaskDescription = ?," +
|
||||||
" TaskDate = ?," +
|
" TaskDate = ?," +
|
||||||
@@ -85,6 +101,9 @@ public class DataBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveTask(long taskID, User user) throws SQLException {
|
public void RemoveTask(long taskID, User user) throws SQLException {
|
||||||
|
if(!connection.isValid(100))
|
||||||
|
OpenConnection();
|
||||||
|
|
||||||
PreparedStatement remove = connection.prepareStatement("DELETE FROM tasks WHERE ID = ? AND UserOwner = ?");
|
PreparedStatement remove = connection.prepareStatement("DELETE FROM tasks WHERE ID = ? AND UserOwner = ?");
|
||||||
remove.setLong(1, taskID);
|
remove.setLong(1, taskID);
|
||||||
remove.setLong(2, user.getID());
|
remove.setLong(2, user.getID());
|
||||||
@@ -92,6 +111,9 @@ public class DataBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void UpdatePassword(String password, int userID) throws SQLException {
|
public void UpdatePassword(String password, int userID) throws SQLException {
|
||||||
|
if(!connection.isValid(100))
|
||||||
|
OpenConnection();
|
||||||
|
|
||||||
PreparedStatement update = connection.prepareStatement("UPDATE users SET password = ? WHERE id = ?");
|
PreparedStatement update = connection.prepareStatement("UPDATE users SET password = ? WHERE id = ?");
|
||||||
update.setString(1, password);
|
update.setString(1, password);
|
||||||
update.setInt(2, userID);
|
update.setInt(2, userID);
|
||||||
@@ -99,6 +121,9 @@ public class DataBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<Task> getTaskList(User user) throws SQLException {
|
public List<Task> getTaskList(User user) throws SQLException {
|
||||||
|
if(!connection.isValid(100))
|
||||||
|
OpenConnection();
|
||||||
|
|
||||||
PreparedStatement findTasks = connection.prepareStatement("SELECT * FROM tasks WHERE UserOwner = ?");
|
PreparedStatement findTasks = connection.prepareStatement("SELECT * FROM tasks WHERE UserOwner = ?");
|
||||||
findTasks.setInt(1, user.getID());
|
findTasks.setInt(1, user.getID());
|
||||||
ResultSet result = findTasks.executeQuery();
|
ResultSet result = findTasks.executeQuery();
|
||||||
@@ -117,6 +142,9 @@ public class DataBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public User FindUser(String login) throws SQLException {
|
public User FindUser(String login) throws SQLException {
|
||||||
|
if(!connection.isValid(100))
|
||||||
|
OpenConnection();
|
||||||
|
|
||||||
PreparedStatement getUser_stmt = connection.prepareStatement("SELECT * FROM users WHERE login = ?");
|
PreparedStatement getUser_stmt = connection.prepareStatement("SELECT * FROM users WHERE login = ?");
|
||||||
getUser_stmt.setString(1, login);
|
getUser_stmt.setString(1, login);
|
||||||
ResultSet resultSet = getUser_stmt.executeQuery();
|
ResultSet resultSet = getUser_stmt.executeQuery();
|
||||||
|
|||||||
Reference in New Issue
Block a user