161 lines
6.2 KiB
Java
161 lines
6.2 KiB
Java
package de.jatitv.commandguiv2.Spigot.system.database;
|
|
|
|
import com.zaxxer.hikari.HikariConfig;
|
|
import com.zaxxer.hikari.HikariDataSource;
|
|
import de.jatitv.commandguiv2.Spigot.Main;
|
|
import de.jatitv.commandguiv2.Spigot.config.config.SelectConfig;
|
|
import net.t2code.lib.Spigot.Lib.messages.send;
|
|
import org.bukkit.Bukkit;
|
|
|
|
import java.sql.*;
|
|
import java.time.ZoneId;
|
|
import java.util.ArrayList;
|
|
import java.util.Calendar;
|
|
import java.util.HashMap;
|
|
|
|
public class MySQL {
|
|
public static String ip = "localhost";
|
|
public static Integer port = 3306;
|
|
public static String database;
|
|
public static String user = "root";
|
|
public static String password = "";
|
|
protected static String url;
|
|
public static Boolean SSL;
|
|
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();
|
|
send.debug(Main.plugin, "Server TimeZone is : " + timeZone);
|
|
try {
|
|
config.setJdbcUrl("jdbc:mysql://" + ip + ":" + port + "/" + database + "?useJDBCCompliantTimezoneShift=true&allowMultiQueries=true&useLegacyDatetimeCode=false&autoReconnect=true&serverTimezone=" + timeZone + "&useSSL=" + SSL);
|
|
config.setUsername(user);
|
|
config.setPassword(password);
|
|
config.addDataSourceProperty("cachePrepStmts", "true");
|
|
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
|
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
|
ds = new HikariDataSource(config);
|
|
send.console(Main.prefix + " §2MYSQL successfully connected." + " §7- §e" + (System.currentTimeMillis() - long_) + "ms");
|
|
} catch (Exception ex) {
|
|
send.console(Main.prefix + " §4MYSQL not connected." + " §7- §e" + (System.currentTimeMillis() - long_) + "ms");
|
|
send.error(Main.plugin, ex.getMessage() + " --- " + (System.currentTimeMillis() - long_) + "ms");
|
|
}
|
|
|
|
if (SelectConfig.Debug || Main.version.toLowerCase().contains("dev") || Main.version.toLowerCase().contains("beta") || Main.version.toLowerCase().contains("snapshot")) {
|
|
try {
|
|
send.error(Main.plugin, "MySQL DEBUG:");
|
|
Connection con = ds.getConnection();
|
|
DatabaseMetaData dbmd = con.getMetaData();
|
|
send.debugmsg(Main.plugin, "§6Metadata of the database:");
|
|
send.debugmsg(Main.plugin, "§6DB: §e" + dbmd.getDatabaseProductName());
|
|
send.debugmsg(Main.plugin, "§6Version: §e" + dbmd.getDatabaseProductVersion());
|
|
send.debugmsg(Main.plugin, "§6Driver: §e" + dbmd.getDriverName());
|
|
send.debugmsg(Main.plugin, "§6Driver MajorVersion: §e" + dbmd.getDriverMajorVersion() + "." + dbmd.getDriverMinorVersion());
|
|
send.debugmsg(Main.plugin, "§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;
|
|
}
|
|
send.debug(Main.plugin, 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) {
|
|
send.debug(Main.plugin, 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) {
|
|
send.debug(Main.plugin, 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) {
|
|
send.debug(Main.plugin, 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) {
|
|
send.debug(Main.plugin, 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();
|
|
}
|
|
}
|