123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package me.km.databank;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import me.km.api.Module;
- import me.km.scheduler.SnuviScheduler;
- import net.minecraft.util.text.TextFormatting;
- public class DataBank extends Module
- {
- private final static DummyStatement DUMMY_STATEMENT = new DummyStatement();
-
- private Connection c;
- private final String user;
- private final String password;
-
- public DataBank(String mname, String prefix, TextFormatting color, String user, String password)
- {
- super(mname, prefix, color);
- this.user = user;
- this.password = password;
- }
-
- public boolean openDataBankConnection()
- {
- c = null;
- 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?autoReconnect=true", user, password);
- this.sendToConsole("Die Verbindung wurde erfolgreich aufgebaut.");
- return true;
- }
- catch(ClassNotFoundException | IllegalAccessException | InstantiationException | SQLException ex)
- {
- this.sendWarningToConsole(ex.toString());
- return false;
- }
- }
-
- public void closeDataBankConnection()
- {
- try
- {
- c.close();
- this.sendToConsole("Die Verbindung wurde erfolgreich unterbrochen.");
- }
- catch (SQLException ex)
- {
- this.sendWarningToConsole(ex.getMessage());
- }
- catch (NullPointerException ex)
- {
- }
- }
-
- public Statement createStatement() throws SQLException
- {
- if(c == null)
- {
- return DUMMY_STATEMENT;
- }
- return c.createStatement();
- }
-
- public void startReconnecting(SnuviScheduler scheduler)
- {
- scheduler.scheduleRepeatingTask(() ->
- {
- try(Statement stmt = 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
- }
- }
|