157 lines
6.3 KiB
Java
157 lines
6.3 KiB
Java
package net.t2code.commandguiv2.Spigot.database;
|
|
|
|
import com.zaxxer.hikari.HikariConfig;
|
|
import com.zaxxer.hikari.HikariDataSource;
|
|
import net.t2code.commandguiv2.Spigot.Main;
|
|
import net.t2code.commandguiv2.Spigot.config.config.SelectConfig;
|
|
import net.t2code.commandguiv2.Util;
|
|
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
|
|
|
|
import java.sql.*;
|
|
import java.time.ZoneId;
|
|
import java.util.ArrayList;
|
|
import java.util.Calendar;
|
|
import java.util.HashMap;
|
|
|
|
public class MySQL {
|
|
|
|
private static final HikariConfig config = new HikariConfig();
|
|
private static HikariDataSource ds;
|
|
|
|
public static void main() {
|
|
long long_ = System.currentTimeMillis();
|
|
Calendar now = Calendar.getInstance();
|
|
ZoneId timeZone = now.getTimeZone().toZoneId();
|
|
T2Csend.debug(Main.getPlugin(), "Server TimeZone is : " + timeZone);
|
|
try {
|
|
config.setJdbcUrl("jdbc:mysql://" + SelectConfig.getMysqlIp() + ":" + SelectConfig.getMysqlPort() + "/" + SelectConfig.getMysqlDatabase()
|
|
+ "?useJDBCCompliantTimezoneShift=true&allowMultiQueries=true&useLegacyDatetimeCode=false&autoReconnect=true&serverTimezone=" + timeZone
|
|
+ "&useSSL=" + SelectConfig.getMysqlSSL());
|
|
config.setUsername(SelectConfig.getMysqlUser());
|
|
config.setPassword(SelectConfig.getMysqlPassword());
|
|
config.addDataSourceProperty("cachePrepStmts", "true");
|
|
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
|
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
|
ds = new HikariDataSource(config);
|
|
T2Csend.console(Util.getPrefix() + " §2MYSQL successfully connected." + " §7- §e" + (System.currentTimeMillis() - long_) + "ms");
|
|
} catch (Exception ex) {
|
|
T2Csend.console(Util.getPrefix() + " §4MYSQL not connected." + " §7- §e" + (System.currentTimeMillis() - long_) + "ms");
|
|
T2Csend.error(Main.getPlugin(), ex.getMessage() + " --- " + (System.currentTimeMillis() - long_) + "ms");
|
|
}
|
|
|
|
if (SelectConfig.getDebug() || Main.version.toLowerCase().contains("dev") || Main.version.toLowerCase().contains("beta") || Main.version.toLowerCase().contains("snapshot")) {
|
|
try {
|
|
T2Csend.error(Main.getPlugin(), "MySQL DEBUG:");
|
|
Connection con = ds.getConnection();
|
|
DatabaseMetaData dbmd = con.getMetaData();
|
|
T2Csend.debugmsg(Main.getPlugin(), "§6Metadata of the database:");
|
|
T2Csend.debugmsg(Main.getPlugin(), "§6DB: §e" + dbmd.getDatabaseProductName());
|
|
T2Csend.debugmsg(Main.getPlugin(), "§6Version: §e" + dbmd.getDatabaseProductVersion());
|
|
T2Csend.debugmsg(Main.getPlugin(), "§6Driver: §e" + dbmd.getDriverName());
|
|
T2Csend.debugmsg(Main.getPlugin(), "§6Driver MajorVersion: §e" + dbmd.getDriverMajorVersion() + "." + dbmd.getDriverMinorVersion());
|
|
T2Csend.debugmsg(Main.getPlugin(), "§6Driver Version: §e" + dbmd.getDriverVersion());
|
|
con.close();
|
|
} catch (SQLException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
|
|
//Bukkit.getConsoleSender().sendMessage((System.currentTimeMillis() - long_) + "ms");
|
|
}
|
|
|
|
|
|
public static void query(String query) throws SQLException {
|
|
if (ds == null) {
|
|
return;
|
|
}
|
|
T2Csend.debug(Main.getPlugin(), query);
|
|
Connection con = ds.getConnection();
|
|
Statement stmt = con.createStatement();
|
|
stmt.execute(query);
|
|
stmt.close();
|
|
con.close();
|
|
}
|
|
|
|
|
|
public static HashMap<String, ArrayList<String>> selectAll(String query) {
|
|
T2Csend.debug(Main.getPlugin(), query);
|
|
HashMap<String, ArrayList<String>> Result = new HashMap<>();
|
|
try (Connection con = ds.getConnection()) {
|
|
Statement stmt = con.createStatement();
|
|
ResultSet rs = stmt.executeQuery(query);
|
|
int columns = rs.getMetaData().getColumnCount();
|
|
while (rs.next()) {
|
|
ArrayList<String> columnList = new ArrayList<>();
|
|
for (int i = 1; i <= columns; i++) {
|
|
columnList.add(rs.getString(i));
|
|
}
|
|
Result.put(rs.getString(1), columnList);
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
} catch (SQLException ex) {
|
|
System.err.println(ex.getMessage());
|
|
}
|
|
return Result;
|
|
}
|
|
|
|
public static String select(String query) {
|
|
T2Csend.debug(Main.getPlugin(), query);
|
|
String Ausgabe = "";
|
|
try (Connection con = ds.getConnection()) {
|
|
Statement stmt = con.createStatement();
|
|
ResultSet rs = stmt.executeQuery(query);
|
|
while (rs.next()) {
|
|
Ausgabe = String.valueOf(rs.getString(1));
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
} catch (SQLException ex) {
|
|
System.err.println(ex.getMessage());
|
|
}
|
|
return Ausgabe;
|
|
}
|
|
|
|
public static int count(String query) {
|
|
T2Csend.debug(Main.getPlugin(), query);
|
|
int count = 0;
|
|
try (Connection con = ds.getConnection()) {
|
|
Statement stmt = con.createStatement();
|
|
ResultSet rs = stmt.executeQuery(query);
|
|
while (rs.next()) {
|
|
count++;
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
} catch (SQLException ex) {
|
|
System.err.println(ex.getMessage());
|
|
}
|
|
return count;
|
|
}
|
|
|
|
public static ArrayList<String> selectRow(String query) {
|
|
T2Csend.debug(Main.getPlugin(), query);
|
|
ArrayList<String> Result = new ArrayList<>();
|
|
try (Connection con = ds.getConnection()) {
|
|
Statement stmt = con.createStatement();
|
|
ResultSet rs = stmt.executeQuery(query);
|
|
int columns = rs.getMetaData().getColumnCount();
|
|
while (rs.next()) {
|
|
for (int i = 1; i <= columns; i++) {
|
|
Result.add(rs.getString(i));
|
|
}
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
} catch (SQLException ex) {
|
|
System.err.println(ex.getMessage());
|
|
}
|
|
return Result;
|
|
}
|
|
|
|
public static void close() {
|
|
if (ds != null) ds.close();
|
|
}
|
|
}
|