SQLibrary

80k Downloads

I would highly suggest using Bukkit's plugin databases API instead of this plugin.

SQLibrary

SQLibrary was taken over from alta189 by PatPeter. So far it has support for 13 databases, a factory, and one query builder.

Tutorial

First, add these elements to your pom.xml if you're using Maven:

<repository>
  <id>dakani</id>
  <name>Dakani Nexus Repo</name>
  <url>http://repo.dakanilabs.com/content/repositories/public</url>
</repository>

<dependency>
  <groupId>lib.PatPeter.SQLibrary</groupId>
  <artifactId>SQLibrary</artifactId>
  <version>7.1</version>
</dependency>

You should always save implementations to a superclass, unless that child class has more functionality. So, start off by creating your variable:

private Database sql;

Constructing your database object can be either hardcoded into the definition, or assigned in the method. The constructor does not automatically open the database connection so that it does not have to throw an SQLException (though this was considered for quite some time). Below are the constructors for the most popular databases: MySQL, SQLite, and H2.

sql = new MySQL(Logger.getLogger("Minecraft"), 
            "[MyPlugin] ", 
            "localhost", 
            3306, 
            "myplugin", 
            "minecraft", 
            "password1");
sql = new SQLite(Logger.getLogger("Minecraft"), 
             "[MyPlugin] ", 
             this.getDataFolder().getAbsolutePath(), 
             "MyPlugin", 
             ".sqlite");
sql = new H2(Logger.getLogger("Minecraft"), 
         "[MyPlugin] ", 
         this.getDataFolder().getAbsolutePath(), 
         "MyPlugin", 
         ".h2");

For SQLite and H2, the extension is optional but will default to ".db". For MySQL, the hostname and port number are optional, but will default to those values. Next, we need to open our database connection:

if (sql.open()) {
    // ...
}

If you know that your plugin will only use the database intermittently, you should check to see if it is open first:

if (!sql.isOpen()) {
    sql.open();
}

If any errors occur while opening the database connection, they will print to console.

Supported Databases

  1. Firebird
  2. FrontBase
  3. DB2
  4. H2
  5. Informix
  6. Ingres
  7. MaxDB
  8. MicrosoftSQL
  9. MySQL
  10. Mongo
  11. mSQL
  12. Oracle
  13. Ovrimos
  14. PostgreSQL
  15. SQLite

Installation

  1. Download the jar.
  2. In Eclipse, go to Project Properties ==> Java Build Path ==> Add External Jar and add SQLibrary.jar.
  3. Tell your users to install SQLibrary.jar along with your plugin

Do not install from source!

If you install SQLibrary directly into your plugin, the following bad things will occur:

  1. You cannot run your plugin alongside SQLibrary.jar plugins. Bukkit will (usually) find the SQLibrary classes in your plugin first, and block the SQLibrary.jar dependency.
  2. You cannot run your plugin alongside other plugins using SQLibrary directly from source. Same issue.
  3. Updating will definitely cause these problems and cause you much pain.

Download SQLibrary from your plugin

You can now place this code in your plugin to download and load SQLibrary from your plugin at runtime:

http://dev.bukkit.org/bukkit-plugins/sqlibrary/pages/soft-dependency-download/

Special Thanks

  • alta189, for starting this library.
  • Belphemur, for adding the first version of our factory package.
  • omwah, for adding Maven to SQLibrary.
  • moose51789, for adding Spout support.
  • Mitsugaru, for hosting our Maven repository, finding bugs, and making fixes.
  • max9403, for supplying the code to download SQLibrary at runtime.

Links