123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package me.km.databank;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.HashMap;
- import me.km.module.Module;
- import me.km.scheduler.SnuviScheduler;
- import net.minecraft.util.text.TextFormatting;
- public class DataBank extends Module
- {
- private Connection c = null;
- private final String user;
- private final String password;
-
- private final HashMap<String, PreparedStatement> statements = new HashMap<>();
-
- public DataBank(String prefix, TextFormatting color, String user, String password)
- {
- super(prefix, color);
- this.user = user;
- this.password = password;
- }
-
- public boolean openDataBankConnection()
- {
- try
- {
- // The newInstance() call is a work around for some broken Java implementations
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- c = DriverManager.getConnection("jdbc:mysql://localhost/minecraft?useSSL=false", user, password);
- this.sendToConsole("Connection to datebank etablished.");
- return true;
- }
- catch(ClassNotFoundException | IllegalAccessException | InstantiationException | SQLException ex)
- {
- this.sendWarningToConsole(ex.toString());
- return false;
- }
- }
-
- public void closeDataBankConnection()
- {
- try
- {
- if(c != null)
- {
- c.close();
- statements.values().forEach(stmt ->
- {
- try
- {
- stmt.close();
- }
- catch(SQLException ex)
- {
- }
- });
- }
- this.sendToConsole("The connection was closed.");
- }
- catch (SQLException ex)
- {
- this.sendWarningToConsole(ex.getMessage());
- }
- }
-
- public boolean isDummyDatabank()
- {
- return c == null;
- }
-
- public PreparedStatement prepareStatement(String query)
- {
- try
- {
- PreparedStatement ps = c.prepareStatement(query);
- PreparedStatement old = statements.put(query, ps);
- if(old != null)
- {
- old.close();
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- return null;
- }
-
- public PreparedStatement prepareUnsafeStatement(String query) throws SQLException
- {
- return c.prepareStatement(query);
- }
-
- public boolean execute(String query)
- {
- if(c == null)
- {
- return false;
- }
- try(java.sql.Statement stmt = c.createStatement())
- {
- stmt.executeUpdate(query);
- return true;
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- return false;
- }
- }
-
- public void startReconnecting(SnuviScheduler scheduler)
- {
- if(c == null)
- {
- return;
- }
- scheduler.scheduleRepeatingTask(() ->
- {
- try(java.sql.Statement stmt = c.createStatement())
- {
- // pointless query just for reconnecting
- stmt.executeUpdate("SHOW TABLES IN minecraft");
- }
- catch(SQLException ex)
- {
- sendToConsole("reconnect was done");
- }
- }, 100, 12000); // doing this every 10 minutes
- }
- }
|