83 lines
No EOL
1.7 KiB
Java
83 lines
No EOL
1.7 KiB
Java
package nl.iobyte.themepark.database;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.util.HashMap;
|
|
|
|
public class DBManager {
|
|
|
|
private static HashMap<String, DB> databases = new HashMap<>();
|
|
|
|
/**
|
|
* Add DB to manager
|
|
* @param id identifier
|
|
* @param db DB Instance
|
|
*/
|
|
public static void addDatabase(String id, DB db) {
|
|
if(id == null || id.isEmpty() || db == null)
|
|
return;
|
|
|
|
if(databases.containsKey(id))
|
|
return;
|
|
|
|
databases.put(id, db);
|
|
}
|
|
|
|
/**
|
|
* Get Database
|
|
* @param id identifier
|
|
* @return DB Instance
|
|
*/
|
|
public static DB getDatabase(String id) {
|
|
if(id == null || id.isEmpty())
|
|
return null;
|
|
|
|
return databases.get(id);
|
|
}
|
|
|
|
/**
|
|
* Database exists
|
|
* @param id identifier
|
|
* @return boolean
|
|
*/
|
|
public static boolean hasDatabase(String id) {
|
|
if(id == null || id.isEmpty())
|
|
return false;
|
|
|
|
return databases.containsKey(id);
|
|
}
|
|
|
|
/**
|
|
* Remove Database
|
|
* @param id identifier
|
|
*/
|
|
public static void removeDatabase(String id) {
|
|
DB db = getDatabase(id);
|
|
if(db == null)
|
|
return;
|
|
|
|
db.closeConnection();
|
|
databases.remove(id);
|
|
}
|
|
|
|
/**
|
|
* Close all connections
|
|
*/
|
|
public static void shutdown() {
|
|
for(DB db : databases.values())
|
|
db.closeConnection();
|
|
}
|
|
|
|
/**
|
|
* Create tabke
|
|
* @param db DB Instance
|
|
* @param query Query
|
|
*/
|
|
public static void createTable(DB db, String query) {
|
|
if(db == null || query == null || query.isEmpty())
|
|
return;
|
|
|
|
db.execute(query, null);
|
|
}
|
|
|
|
} |