Database connecting wiki

Discussion in 'Spigot Help' started by craftinc747, Oct 11, 2021.

  1. What class is this database object from? I cannot find it anywhere. It states clearly where the dataSource object is from.

    The following code snippet is from a spigot tutorial on db connecting.

    Code (Java):
    Database database = config.getDatabase();
    // we set our credentials
    dataSource.setServerName(database.getHost());
    dataSource.setPortNumber(database.getPort());
    dataSource.setDatabaseName(database.getDatabase());
    dataSource.setUser(database.getUser());
    dataSource.setPassword(database.getPassword());
    From:
    https://www.spigotmc.org/wiki/connecting-to-databases-mysql/?noRedirect=1
     
  2. You just have to fill that methods. Its not important how to fill.
     
    • Agree Agree x 1
  3. Choco

    Moderator

    Clarifying @kingOf0's answer, the YAML snippet above that snippet shows a structure that would be similar to that Database object. It's titled database with fields for the host, port, username, password, and database. The object referenced is just a POJO probably no different than this:
    Code (Java):
    public class Database {

        private final String host;
        private final int port;
        private final String database, username, password;

        public Database(String host, int port, String database, String username, String password) {
            this.host = host;
            this.port = port;
            this.database = database;
            this.username = username;
            this.password = password;
        }

        // etc. the getHost(), getPort(), getDatabase(), getUsername(), and getPassword() methods

    }
    You can accomplish the same just fetching those values from your config.yml directly and skipping the Database object entirely. It's a middle-man object that you really don't need.
    Code (Java):
    String host = config.getString("database.host");
    int port = config.getInt("database.port");
    String database = config.getString("database.database");
    String username = config.getString("database.username");
    String password = config.getString("database.password");
     
    • Like Like x 1

  4. okay cool. that makes sense. now i just have to figure out how to set the path to the appropriate config file.