66 lines
1.8 KiB
Java
66 lines
1.8 KiB
Java
package nl.iobyte.themepark.database;
|
|
|
|
import com.zaxxer.hikari.HikariConfig;
|
|
import com.zaxxer.hikari.HikariDataSource;
|
|
import nl.iobyte.themepark.ThemeParkPlugin;
|
|
import java.sql.Connection;
|
|
import java.sql.SQLException;
|
|
import java.util.Properties;
|
|
|
|
public class MySQL extends DB {
|
|
|
|
private String dbName;
|
|
private HikariDataSource source;
|
|
private Connection con;
|
|
|
|
/**
|
|
* Initialize a new connection
|
|
*
|
|
* @param dbName The database name
|
|
*/
|
|
public MySQL(String host, int port, String dbName, String username, String password) {
|
|
this.dbName = dbName;
|
|
HikariConfig config = new HikariConfig();
|
|
config.setPoolName(ThemeParkPlugin.getInstance().getName());
|
|
config.setUsername(username);
|
|
config.setPassword(password);
|
|
config.setDriverClassName("com.mysql.jdbc.Driver");
|
|
config.setConnectionTestQuery("SELECT 1");
|
|
config.setMaximumPoolSize(1);
|
|
|
|
Properties prop = new Properties();
|
|
prop.setProperty("date_string_format", "yyyy-MM-dd HH:mm:ss");
|
|
config.setJdbcUrl("jdbc:mysql://"+host+":"+port+"/"+dbName);
|
|
config.setDataSourceProperties(prop);
|
|
source = new HikariDataSource(config);
|
|
|
|
try {
|
|
con = source.getConnection();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get connection
|
|
* @return Connection Instance
|
|
*/
|
|
public Connection getConnection() {
|
|
return con;
|
|
}
|
|
|
|
/**
|
|
* Close connection
|
|
*/
|
|
public void closeConnection() {
|
|
System.out.println("Closing the database connection for " + dbName + ".db!");
|
|
try {
|
|
con.close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
source.close();
|
|
}
|
|
|
|
}
|