Solved MySQL table doesn't exit

Discussion in 'Plugin Development' started by KAM202, Jul 2, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    KAM202

    Hi. I can't create a table in database...When i want reload server i've got table "hades.banlist" doesn't exit.
    After added "this.plugin.mysqldatabase.initialize(plugin) - error:
    Code:
     Error occurred while enabling Tools v1.0 (Is it up to date?)
    java.lang.NullPointerException
            at io.github.kam202.bans.MySQLDatabase.initialize(MySQLDatabase.java:53) ~[?:?]
            at io.github.kam202.Main.onEnable(Main.java:125) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
    
    My code initialize:
    Code:
    public boolean initialize(Main plugin) {
      this.plugin = plugin;
      conn = null;
      ps = null;
      rs = null;
     
      String table = this.plugin.configManager.mysqlTable;
      try {
        conn = getSQLConnection();
        DatabaseMetaData dbm = conn.getMetaData();
       
        if (!dbm.getTables(null, null, table, null).next()) {
          Bukkit.getLogger().log(Level.INFO, "[BkrBans:MySQL] Creating table " + table + ".");
          ps = conn.prepareStatement("CREATE TABLE `" + table + "` ( \n" + "  `name` varchar(32) NOT NULL, \n" +
              "  `reason` text NOT NULL, \n " + "  `admin` varchar(32) NOT NULL, \n" +
              "  `time` bigint(20) NOT NULL, \n " + "  `temptime` bigint(20) NOT NULL DEFAULT '0', \n" +
              "  `type` int(11) NOT NULL DEFAULT '0', \n" + "  `id` int(11) NOT NULL AUTO_INCREMENT, \n" +
              "  `ip` varchar(16) DEFAULT NULL, \n" + "  PRIMARY KEY (`id`) USING BTREE \n" +
              ") ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ROW_FORMAT=DYNAMIC;");
          ps.execute();
          if (!dbm.getTables(null, null, table, null).next()) {
            throw new SQLException("Table " + table + " not found; tired to create and failed");
          }
        }
        ps = conn.prepareStatement("DELETE FROM " + table +
            " WHERE (type = 0 OR type = 1) AND (temptime > 0) AND (temptime < ?)");
        ps.setLong(1, System.currentTimeMillis() / 1000L);
        ps.execute();
      } catch (SQLException ex) {
        plugin.logger.log(Level.SEVERE, "[BkrBans:MySQL 1] Couldn't execute MySQL statement: ", ex);
        return false;
      } finally {
        try {
          if (ps != null)
            ps.close();
          if (conn != null)
            conn.close();
          if (rs != null)
            rs.close();
        } catch (SQLException ex) {
          plugin.logger.log(Level.SEVERE, "[BkrBans:MySQL 2] Failed to close MySQL connection: ", ex);
        }
      }
    
     
      return true;
    }
    I understand that if it does not initiate creating a table, it will not find it and why does it not create it?
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    KAM202

    @timtower
    Code:
    String table = this.plugin.configManager.mysqlTable;
    why table == null? :///
     
  4. Offline

    timtower Administrator Administrator Moderator

    If that is line 53 then configManager or mysqlTable is null.
     
  5. Offline

    KAM202

    ConfigManager:
    Code:
    public String mysqlTable;
    ...
    this.mysqlTable = this.plugin.config.getString("config.mysql.table");
    so..?
    there may be a problem that I have something like this:
    Code:
    public MySQLDatabase(Main instance) { this.plugin = instance; }
    and
    Code:
    public boolean initialize(Main plugin) {
      this.plugin = plugin;
    hmm?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @KAM202 That all depends on code order that I can't see when I just get single lines.
     
  7. Offline

    KAM202

    Okay but in config i have password database login etc. and it connects to the database, but it does not create a database. and when I add the initiation in mainie it crashes the error. I sent you the code on dm
     
  8. Offline

    timtower Administrator Administrator Moderator

    Post the code here instead, won't help through DM.
    And that is why the thread exists, but I need to see the code to be able to tell what is wrong.
     
  9. Offline

    Kars

    configManager is null. You could assign mysqlTable to the variable table if mysqlTable was null.
     
Thread Status:
Not open for further replies.

Share This Page