Implemented better MySQL connection handling (NEEDS TESTING!)
This commit is contained in:
@@ -55,10 +55,10 @@ public class MysqlHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public MysqlDatabase getDatabase(String databaseName) {
|
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;
|
Connection connection = null;
|
||||||
try {
|
try {
|
||||||
connection = DriverManager.getConnection(url + databaseName, username, password);
|
connection = DriverManager.getConnection(url + databaseName, username, password);
|
||||||
@@ -76,7 +76,7 @@ public class MysqlHandler {
|
|||||||
ResultSet queryResults = metadata.getCatalogs();
|
ResultSet queryResults = metadata.getCatalogs();
|
||||||
while (queryResults.next()) {
|
while (queryResults.next()) {
|
||||||
String databaseName = queryResults.getString("TABLE_CAT");
|
String databaseName = queryResults.getString("TABLE_CAT");
|
||||||
databases.add(new MysqlDatabase(getConnection(databaseName)));
|
databases.add(new MysqlDatabase(this, databaseName));
|
||||||
}
|
}
|
||||||
connection.close();
|
connection.close();
|
||||||
return databases;
|
return databases;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.redstoner.misc.mysql.elements;
|
package com.redstoner.misc.mysql.elements;
|
||||||
|
|
||||||
|
import com.redstoner.misc.mysql.MysqlHandler;
|
||||||
import com.redstoner.misc.mysql.MysqlQueryHandler;
|
import com.redstoner.misc.mysql.MysqlQueryHandler;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
@@ -10,10 +11,14 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class MysqlDatabase {
|
public class MysqlDatabase {
|
||||||
private Connection connection;
|
private Connection connection = null;
|
||||||
|
|
||||||
public MysqlDatabase(Connection connection) {
|
private final MysqlHandler handler;
|
||||||
this.connection = connection;
|
private final String databaseName;
|
||||||
|
|
||||||
|
public MysqlDatabase(MysqlHandler handler, String databaseName) {
|
||||||
|
this.handler = handler;
|
||||||
|
this.databaseName = databaseName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MysqlTable getTable(String name) {
|
public MysqlTable getTable(String name) {
|
||||||
@@ -21,7 +26,7 @@ public class MysqlDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean createTable(String name, MysqlField... description) {
|
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) {
|
private String getDescription(MysqlField... description) {
|
||||||
@@ -47,20 +52,20 @@ public class MysqlDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean createTableIfNotExists(String name, MysqlField... description) {
|
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) {
|
public boolean dropTable(String name) {
|
||||||
return MysqlQueryHandler.queryNoResult(connection, "DROP TABLE `" + name + "`;");
|
return MysqlQueryHandler.queryNoResult(getConnection(), "DROP TABLE `" + name + "`;");
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean drop() {
|
public boolean drop() {
|
||||||
return MysqlQueryHandler.queryNoResult(connection, "DROP DATABASE `" + getName() + "`;");
|
return MysqlQueryHandler.queryNoResult(getConnection(), "DROP DATABASE `" + getName() + "`;");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
try {
|
try {
|
||||||
return connection.getCatalog();
|
return getConnection().getCatalog();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
@@ -70,7 +75,7 @@ public class MysqlDatabase {
|
|||||||
public List<MysqlTable> getTables() {
|
public List<MysqlTable> getTables() {
|
||||||
try {
|
try {
|
||||||
List<MysqlTable> tables = new ArrayList<>();
|
List<MysqlTable> tables = new ArrayList<>();
|
||||||
DatabaseMetaData metadata = connection.getMetaData();
|
DatabaseMetaData metadata = getConnection().getMetaData();
|
||||||
ResultSet queryResults = metadata.getTables(null, null, "%", null);
|
ResultSet queryResults = metadata.getTables(null, null, "%", null);
|
||||||
|
|
||||||
while (queryResults.next()) {
|
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;
|
return connection;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user