Пример #1
0
  public void connect() {
    Statement statement = null;

    databaseType = foxbot.getConfig().getDatabaseType();
    url =
        databaseType.equalsIgnoreCase("mysql")
            ? String.format(
                "jdbc:mysql://%s:%s/%s",
                foxbot.getConfig().getDatabaseHost(),
                foxbot.getConfig().getDatabasePort(),
                foxbot.getConfig().getDatabaseName())
            : "jdbc:sqlite:data/bot.db";

    try {
      if (databaseType.equalsIgnoreCase("sqlite")) {
        Class.forName("org.sqlite.JDBC");
      }

      if (databaseType.equalsIgnoreCase("mysql")) {
        String user = foxbot.getConfig().getDatabaseUser();
        String password = foxbot.getConfig().getDatabasePassword();
        connection = DriverManager.getConnection(url, user, password);
      } else {
        connection = DriverManager.getConnection(url);
      }

      statement = connection.createStatement();

      statement.setQueryTimeout(30);
      statement.executeUpdate(
          "CREATE TABLE IF NOT EXISTS tells (tell_time VARCHAR(32), sender VARCHAR(32), receiver VARCHAR(32), message VARCHAR(1024), used TINYINT)");
      statement.executeUpdate(
          "CREATE TABLE IF NOT EXISTS bans (channel VARCHAR(64), target VARCHAR(32), hostmask VARCHAR(64), reason VARCHAR(1024), banner VARCHAR(32), ban_time BIGINT)");
      statement.executeUpdate(
          "CREATE TABLE IF NOT EXISTS kicks (channel VARCHAR(64), target VARCHAR(32), hostmask VARCHAR(64), reason VARCHAR(1024), kicker VARCHAR(32), kick_time BIGINT)");
      statement.executeUpdate(
          "CREATE TABLE IF NOT EXISTS mutes (channel VARCHAR(64), target VARCHAR(32), hostmask VARCHAR(64), reason VARCHAR(1024), muter VARCHAR(32), mute_time BIGINT)");
    } catch (SQLException | ClassNotFoundException ex) {
      Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
      foxbot.disconnect();
    } finally {
      try {
        if (statement != null) {
          statement.close();
        }
        if (connection != null) {
          connection.close();
          connection = null;
        }
      } catch (SQLException ex) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
      }
    }
  }
Пример #2
0
  public void reconnect() {
    if (connection == null) {
      try {
        if (databaseType.equalsIgnoreCase("sqlite")) {
          Class.forName("org.sqlite.JDBC");
        }

        if (databaseType.equalsIgnoreCase("mysql")) {
          String user = foxbot.getConfig().getDatabaseUser();
          String password = foxbot.getConfig().getDatabasePassword();
          connection = DriverManager.getConnection(url, user, password);
        } else {
          connection = DriverManager.getConnection(url);
        }
      } catch (SQLException | ClassNotFoundException ex) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        foxbot.disconnect();
      }
    }
  }