Interface WorldDatabase


public interface WorldDatabase
Provides access to the current world database.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static enum 
    Represents a particular world database.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Executes the given SQL statement, for example a CREATE or DELETE statement.
    Executes the given SQL statement and returns a ResultSet object.
    Remember to close the ResultSet once you're ready.
    void
    Executes the given SQL statement, which may be an INSERT, UPDATE or DELETE statement.
    Gets the database name.
     
    Gets the path to the database file.
  • Method Details

    • getDatabaseType

      DatabaseType getDatabaseType()
    • getPath

      String getPath()
      Gets the path to the database file.
      Returns:
      the full path to the database file.
    • getDatabaseName

      String getDatabaseName()
      Gets the database name.
      Returns:
      the database name.
    • execute

      void execute(String sql)
      Executes the given SQL statement, for example a CREATE or DELETE statement.

      Please note: In order to synchronize any commits to the database, this statement may be executed with a minor delay (usually just a few ticks)
      Parameters:
      sql - the SQL statement.
      Example: Create a new table in world database
      1WorldDatabase db = getWorldDatabase(WorldDatabase.Target.World);
      2
      3//Creates a new table "MyTable" with columns "ID" (primary, auto-incr.), "Name" (String, 64 chars) and "Value" (int)
      4db.execute("CREATE TABLE IF NOT EXISTS `MyTable` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `Name` VARCHAR(64), `Value` INTEGER)");
    • executeUpdate

      void executeUpdate(String sql)
      Executes the given SQL statement, which may be an INSERT, UPDATE or DELETE statement.

      Please note: In order to synchronize any commits to the database, this statement may be executed with a minor delay (usually just a few ticks)
      Parameters:
      sql - the SQL statement, INSERT, UPDATE or DELETE.
      Example: Delete a player from player database
      1WorldDatabase db = getWorldDatabase(WorldDatabase.Target.Players);
      2db.executeUpdate("DELETE FROM `player` WHERE `uid` = '76561197960265728'");
    • executeQuery

      ResultSet executeQuery(String sql) throws SQLException
      Executes the given SQL statement and returns a ResultSet object.
      Remember to close the ResultSet once you're ready.
      Parameters:
      sql - the SQL statement, typically a SELECT statement.
      Returns:
      a ResultSet object containing the data produced by the given SQL statement.
      Throws:
      SQLException - If a database access error occurs.
      Example: Read player data from database
      1WorldDatabase db = getWorldDatabase(WorldDatabase.Target.Players);
      2try(ResultSet result = db.executeQuery("SELECT * FROM `player` WHERE `name` = 'Testplayer'")){
      3 if(result.next()){
      4 String uid = result.getString("uid");
      5 int health = result.getInt("health");
      6 int hunger = result.getInt("hunger");
      7
      8 //Position is stored as 3 floats
      9 Vector3f position = new Vector3f();
      10 position.x = result.getFloat("posx");
      11 position.y = result.getFloat("posy");
      12 position.z = result.getFloat("posz");
      13
      14 //...
      15 }
      16}
      17catch(SQLException e){
      18 //SQL Exception occurred
      19}