0

Implemented better MySQL connection handling (NEEDS TESTING!)

This commit is contained in:
David Panić
2019-04-29 23:50:49 +02:00
parent 7d3e2a04b2
commit 855ce84f7a
2 changed files with 27 additions and 13 deletions

View File

@@ -55,10 +55,10 @@ public class MysqlHandler {
}
public MysqlDatabase getDatabase(String databaseName) {
return new MysqlDatabase(getConnection(databaseName));
return new MysqlDatabase(this, databaseName);
}
private Connection getConnection(String databaseName) throws IllegalStateException {
public Connection getConnection(String databaseName) throws IllegalStateException {
Connection connection = null;
try {
connection = DriverManager.getConnection(url + databaseName, username, password);
@@ -76,7 +76,7 @@ public class MysqlHandler {
ResultSet queryResults = metadata.getCatalogs();
while (queryResults.next()) {
String databaseName = queryResults.getString("TABLE_CAT");
databases.add(new MysqlDatabase(getConnection(databaseName)));
databases.add(new MysqlDatabase(this, databaseName));
}
connection.close();
return databases;

View File

@@ -1,5 +1,6 @@
package com.redstoner.misc.mysql.elements;
import com.redstoner.misc.mysql.MysqlHandler;
import com.redstoner.misc.mysql.MysqlQueryHandler;
import java.sql.Connection;
@@ -10,10 +11,14 @@ import java.util.ArrayList;
import java.util.List;
public class MysqlDatabase {
private Connection connection;
private Connection connection = null;
public MysqlDatabase(Connection connection) {
this.connection = connection;
private final MysqlHandler handler;
private final String databaseName;
public MysqlDatabase(MysqlHandler handler, String databaseName) {
this.handler = handler;
this.databaseName = databaseName;
}
public MysqlTable getTable(String name) {
@@ -21,7 +26,7 @@ public class MysqlDatabase {
}
public boolean createTable(String name, MysqlField... description) {
return MysqlQueryHandler.queryNoResult(connection, "CREATE TABLE `" + name + "` " + getDescription(description) + ";");
return MysqlQueryHandler.queryNoResult(getConnection(), "CREATE TABLE `" + name + "` " + getDescription(description) + ";");
}
private String getDescription(MysqlField... description) {
@@ -47,20 +52,20 @@ public class MysqlDatabase {
}
public boolean createTableIfNotExists(String name, MysqlField... description) {
return MysqlQueryHandler.queryNoResult(connection, "CREATE TABLE IF NOT EXISTS `" + name + "` " + getDescription(description) + ";");
return MysqlQueryHandler.queryNoResult(getConnection(), "CREATE TABLE IF NOT EXISTS `" + name + "` " + getDescription(description) + ";");
}
public boolean dropTable(String name) {
return MysqlQueryHandler.queryNoResult(connection, "DROP TABLE `" + name + "`;");
return MysqlQueryHandler.queryNoResult(getConnection(), "DROP TABLE `" + name + "`;");
}
public boolean drop() {
return MysqlQueryHandler.queryNoResult(connection, "DROP DATABASE `" + getName() + "`;");
return MysqlQueryHandler.queryNoResult(getConnection(), "DROP DATABASE `" + getName() + "`;");
}
public String getName() {
try {
return connection.getCatalog();
return getConnection().getCatalog();
} catch (SQLException e) {
e.printStackTrace();
return null;
@@ -70,7 +75,7 @@ public class MysqlDatabase {
public List<MysqlTable> getTables() {
try {
List<MysqlTable> tables = new ArrayList<>();
DatabaseMetaData metadata = connection.getMetaData();
DatabaseMetaData metadata = getConnection().getMetaData();
ResultSet queryResults = metadata.getTables(null, null, "%", null);
while (queryResults.next()) {
@@ -84,7 +89,16 @@ public class MysqlDatabase {
}
}
protected Connection getConnection() {
Connection getConnection() {
try {
if (connection == null || connection.isClosed()) {
connection = handler.getConnection(databaseName);
}
} catch (SQLException e) {
e.printStackTrace();
connection = handler.getConnection(databaseName);
}
return connection;
}
}