T2C-CommandGUI/CommandGUI V2/src/main/java/net/t2code/commandguiv2/Spigot/database/MySQL.java

157 lines
6.3 KiB
Java
Raw Normal View History

2022-11-01 12:20:53 +00:00
package net.t2code.commandguiv2.Spigot.database;
2021-12-21 04:57:29 +00:00
2022-01-16 02:03:04 +00:00
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
2022-11-01 12:20:53 +00:00
import net.t2code.commandguiv2.Spigot.Main;
import net.t2code.commandguiv2.Spigot.config.config.SelectConfig;
import net.t2code.commandguiv2.Util;
2022-08-10 13:53:01 +00:00
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
2021-12-21 04:57:29 +00:00
import java.sql.*;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
2022-01-16 02:03:04 +00:00
import java.util.HashMap;
2021-12-21 04:57:29 +00:00
public class MySQL {
2022-01-16 02:03:04 +00:00
private static final HikariConfig config = new HikariConfig();
private static HikariDataSource ds;
2021-12-21 04:57:29 +00:00
public static void main() {
2022-01-16 02:03:04 +00:00
long long_ = System.currentTimeMillis();
2021-12-21 04:57:29 +00:00
Calendar now = Calendar.getInstance();
ZoneId timeZone = now.getTimeZone().toZoneId();
2022-08-10 13:53:01 +00:00
T2Csend.debug(Main.getPlugin(), "Server TimeZone is : " + timeZone);
2022-01-16 02:03:04 +00:00
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());
2022-01-16 02:03:04 +00:00
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
ds = new HikariDataSource(config);
2022-08-10 13:53:01 +00:00
T2Csend.console(Util.getPrefix() + " §2MYSQL successfully connected." + " §7- §e" + (System.currentTimeMillis() - long_) + "ms");
2022-01-16 02:03:04 +00:00
} catch (Exception ex) {
2022-08-10 13:53:01 +00:00
T2Csend.console(Util.getPrefix() + " §4MYSQL not connected." + " §7- §e" + (System.currentTimeMillis() - long_) + "ms");
T2Csend.error(Main.getPlugin(), ex.getMessage() + " --- " + (System.currentTimeMillis() - long_) + "ms");
2021-12-21 04:57:29 +00:00
}
if (SelectConfig.getDebug() || Main.version.toLowerCase().contains("dev") || Main.version.toLowerCase().contains("beta") || Main.version.toLowerCase().contains("snapshot")) {
try {
2022-08-10 13:53:01 +00:00
T2Csend.error(Main.getPlugin(), "MySQL DEBUG:");
Connection con = ds.getConnection();
DatabaseMetaData dbmd = con.getMetaData();
2022-08-10 13:53:01 +00:00
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());
2022-06-01 11:23:53 +00:00
con.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
2022-01-16 02:03:04 +00:00
//Bukkit.getConsoleSender().sendMessage((System.currentTimeMillis() - long_) + "ms");
2021-12-21 04:57:29 +00:00
}
2022-01-16 02:03:04 +00:00
public static void query(String query) throws SQLException {
if (ds == null) {
2022-01-16 02:03:04 +00:00
return;
}
2022-08-10 13:53:01 +00:00
T2Csend.debug(Main.getPlugin(), query);
Connection con = ds.getConnection();
Statement stmt = con.createStatement();
stmt.execute(query);
stmt.close();
2022-06-01 11:23:53 +00:00
con.close();
2022-01-16 02:03:04 +00:00
}
2021-12-21 04:57:29 +00:00
2022-01-16 02:03:04 +00:00
public static HashMap<String, ArrayList<String>> selectAll(String query) {
2022-08-10 13:53:01 +00:00
T2Csend.debug(Main.getPlugin(), query);
2022-01-16 02:03:04 +00:00
HashMap<String, ArrayList<String>> Result = new HashMap<>();
try (Connection con = ds.getConnection()) {
2021-12-21 04:57:29 +00:00
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
int columns = rs.getMetaData().getColumnCount();
while (rs.next()) {
2022-01-16 02:03:04 +00:00
ArrayList<String> columnList = new ArrayList<>();
for (int i = 1; i <= columns; i++) {
columnList.add(rs.getString(i));
}
Result.put(rs.getString(1), columnList);
2021-12-21 04:57:29 +00:00
}
rs.close();
stmt.close();
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
return Result;
}
public static String select(String query) {
2022-08-10 13:53:01 +00:00
T2Csend.debug(Main.getPlugin(), query);
2021-12-21 04:57:29 +00:00
String Ausgabe = "";
2022-01-16 02:03:04 +00:00
try (Connection con = ds.getConnection()) {
2021-12-21 04:57:29 +00:00
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) {
2022-08-10 13:53:01 +00:00
T2Csend.debug(Main.getPlugin(), query);
2022-01-16 02:03:04 +00:00
int count = 0;
try (Connection con = ds.getConnection()) {
2021-12-21 04:57:29 +00:00
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;
}
2022-01-16 02:03:04 +00:00
public static ArrayList<String> selectRow(String query) {
2022-08-10 13:53:01 +00:00
T2Csend.debug(Main.getPlugin(), query);
2022-01-16 02:03:04 +00:00
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() {
2022-04-16 12:32:22 +00:00
if (ds != null) ds.close();
2022-01-16 02:03:04 +00:00
}
2021-12-21 04:57:29 +00:00
}