Interface Database

All Superinterfaces:
AutoCloseable

public interface Database extends AutoCloseable
Database interface which represents a connection to an SQLite or MySQL database.
Example: Full example of how you may use an SQLite database
1public class MyPlugin extends Plugin implements Listener {
2
3 //Cache the db connection
4 public Database database;
5
6 @Override
7 public void onEnable() {
8 //We only create a db connection once and reuse it
9 database = getSQLiteConnection(getPath() + "/database.db");
10
11 //Create new table if it does not yet exist
12 database.execute("CREATE TABLE IF NOT EXISTS `MyTable` (`playeruid` VARCHAR(255) PRIMARY KEY NOT NULL, `timestamp` BIGINT);");
13
14 //Register event listener
15 registerEventListener(this);
16 }
17
18 @Override
19 public void onDisable() {
20 //When our plugin gets disabled, close the database connection
21 if(database != null) database.close();
22 }
23
24 @EventMethod
25 public void onPlayerConnect(PlayerConnectEvent evt) {
26 Player player = evt.getPlayer();
27
28 //If player is a new player (connect for the first time), we insert a new data set
29 if(evt.isNewPlayer()) {
30 //Insert data through our cached db connection
31 database.executeUpdate("INSERT INTO `MyTable` (playeruid, timestamp) VALUES ('" + player.getUID() + ", " + System.currentTimeMillis() + ");");
32 }
33 }
34
35 @EventMethod
36 public void onPlayerCommand(PlayerCommandEvent evt) {
37 Player player = evt.getPlayer();
38
39 if(evt.getCommand().startsWith("/testcommand")) {
40 //This "try-with-resources" block closes the ResultSet automatically
41 try(ResultSet result = database.executeQuery("SELECT * FROM `MyTable` WHERE `playeruid` = '" + player.getUID() + "';")){
42 while(result.next()) {
43 //Do something with the data
44 long timestamp = result.getLong("timestamp");
45 player.sendTextMessage("Stored timestamp: " + timestamp);
46 }
47 }
48 catch(Exception e) {
49 e.printStackTrace();
50 }
51 }
52 }
53
54}
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes this connection.
    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 (or use a try-with-resources statement).
    void
    Executes the given SQL statement, which may be an INSERT, UPDATE or DELETE statement.
    Gets the underlying Connection object, which represents the connection to the specific database.
    Gets the database type.
  • Method Details

    • getType

      DatabaseType getType()
      Gets the database type.
      Returns:
      the database type, SQLite or MySQL
    • getConnection

      Connection getConnection()
      Gets the underlying Connection object, which represents the connection to the specific database. This provides full access to the database. Be careful when changing any settings of this connection;
      Returns:
      a Connection object.
    • execute

      void execute(String sql)
      Executes the given SQL statement, for example a CREATE or DELETE statement.
      Parameters:
      sql - the SQL statement.
    • executeUpdate

      void executeUpdate(String sql)
      Executes the given SQL statement, which may be an INSERT, UPDATE or DELETE statement.
      Parameters:
      sql - the SQL statement, INSERT, UPDATE or DELETE.
    • 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 (or use a try-with-resources statement).
      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.
      See Also:
    • close

      void close()
      Closes this connection. Call this method if the connection isn't needed anymore.
      Specified by:
      close in interface AutoCloseable