Add basic DatabaseConnection to API
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
package com.alttd.chat;
|
||||
|
||||
import com.alttd.chat.database.DatabaseConnection;
|
||||
import net.luckperms.api.LuckPerms;
|
||||
|
||||
public interface ChatAPI {
|
||||
|
||||
/*public static ChatAPI get() {
|
||||
return ChatImplementation.INSTANCE;
|
||||
}*/
|
||||
ChatAPI get();
|
||||
|
||||
LuckPerms getLuckPerms();
|
||||
|
||||
DatabaseConnection getDataBase();
|
||||
}
|
||||
|
||||
@@ -1,23 +1,42 @@
|
||||
package com.alttd.chat;
|
||||
|
||||
import com.alttd.chat.database.DatabaseConnection;
|
||||
import net.luckperms.api.LuckPerms;
|
||||
import net.luckperms.api.LuckPermsProvider;
|
||||
|
||||
public class ChatImplementation implements ChatAPI{
|
||||
|
||||
//public static final ChatAPI INSTANCE = new ChatImplementation();
|
||||
private ChatAPI instance;
|
||||
|
||||
private LuckPerms luckPerms;
|
||||
private DatabaseConnection databaseConnection;
|
||||
|
||||
ChatImplementation() {
|
||||
instance = this;
|
||||
// init database
|
||||
// init depends//or set them the first time they are called?
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatAPI get() {
|
||||
if(instance == null)
|
||||
instance = new ChatImplementation();
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LuckPerms getLuckPerms() {
|
||||
if(luckPerms == null)
|
||||
luckPerms = LuckPermsProvider.get();
|
||||
return luckPerms;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DatabaseConnection getDataBase() {
|
||||
if(databaseConnection == null)
|
||||
databaseConnection = new DatabaseConnection();
|
||||
return databaseConnection;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.alttd.chat.database;
|
||||
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DatabaseConnection {
|
||||
|
||||
private static DatabaseConnection instance;
|
||||
private Connection connection;
|
||||
private final String drivers, ip, port, database, username, password;
|
||||
|
||||
/**
|
||||
* Sets information for the database and opens the connection.
|
||||
*/
|
||||
public DatabaseConnection() {
|
||||
|
||||
/*this.drivers = Config.drivers;
|
||||
this.ip = Config.ip;
|
||||
this.port = Config.port;
|
||||
this.database = Config.database;
|
||||
this.username = Config.username;
|
||||
this.password = Config.password;*/
|
||||
// temp to make compile, remove when config is added
|
||||
this.drivers = this.ip = this.port = this.database = this.username = this.password = "";
|
||||
instance = this;
|
||||
|
||||
try {
|
||||
instance.openConnection();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the connection if it's not already open.
|
||||
* @throws SQLException If it can't create the connection.
|
||||
*/
|
||||
public void openConnection() throws SQLException {
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
synchronized (this) {
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
connection = DriverManager.getConnection(
|
||||
"jdbc:" + drivers + "://" + ip + ":" + port + "/" + database + "?autoReconnect=true", username,
|
||||
password);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the connection for the database
|
||||
* @return Returns the connection.
|
||||
*/
|
||||
public static Connection getConnection() {
|
||||
try {
|
||||
instance.openConnection();
|
||||
}
|
||||
catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return instance.connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the connection for this instance
|
||||
*/
|
||||
public boolean initialize() {
|
||||
instance = new DatabaseConnection();
|
||||
return connection != null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user