SQLibrary

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</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.

You must login to post a comment. Don't have an account? Register to get one!

  • Avatar of broluigi broluigi Nov 16, 2014 at 01:09 UTC - 0 likes

    @SableDnah: Go

    using spigot 1.7.10 with the latest bps patch, and using carbon for 1.8 items it wont load, but when i use it on a server using purely 1649 it works perfectly

    16.11 16:05:55 [Server] INFO ... 6 more
    16.11 16:05:55 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO at java.lang.Class.newInstance(Class.java:374) ~[?:1.7.0_51]
    16.11 16:05:55 [Server] INFO at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[?:1.7.0_51]
    16.11 16:05:55 [Server] INFO at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.7.0_51]
    16.11 16:05:55 [Server] INFO at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[?:1.7.0_51]
    16.11 16:05:55 [Server] INFO at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.7.0_51]
    16.11 16:05:55 [Server] INFO at com.addemod.itsatacoshop.motd.PersonalMOTD.<init>(PersonalMOTD.java:25) ~[?:?]
    16.11 16:05:55 [Server] INFO at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO Caused by: java.lang.IllegalStateException: Initial initialization
    16.11 16:05:55 [Server] INFO ... 6 more
    16.11 16:05:55 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO at java.lang.Class.newInstance(Class.java:374) ~[?:1.7.0_51]
    16.11 16:05:55 [Server] INFO at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[?:1.7.0_51]
    16.11 16:05:55 [Server] INFO at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.7.0_51]
    16.11 16:05:55 [Server] INFO at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[?:1.7.0_51]
    16.11 16:05:55 [Server] INFO at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.7.0_51]
    16.11 16:05:55 [Server] INFO at lib.PatPeter.SQLibrary.SQLibrary.<init>(SQLibrary.java:12) ~[?:?]
    16.11 16:05:55 [Server] INFO at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
    16.11 16:05:55 [Server] INFO at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:458) [SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO at net.minecraft.server.v1_7_R4.DedicatedServer.init(DedicatedServer.java:152) [SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugins(CraftServer.java:369) [SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:258) [SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:336) ~[SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:133) ~[SpigotServer.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    16.11 16:05:55 [Server] INFO org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: Plugin already initialized!
    16.11 16:05:55 [Server] INFO [16:05:55 ERROR]: Could not load 'plugins/SQLibrary.jar' in folder 'plugins'
    16.11 16:05:55 [Server] INFO [16:05:55 INFO]: Set PluginClassLoader as parallel capabl
    
    Last edited Nov 16, 2014 by broluigi

    TownyMc

    Website: lorecase.org

  • Avatar of SableDnah SableDnah Nov 16, 2014 at 00:50 UTC - 0 likes

    @broluigi: Go Java 1.8 or MC 1.8?

    This works find in the spigot 1649+ builds for me.

    "All this technology and it still takes forever to get anywhere" [ Donate ] [ Twitter ]

    Mob HealthLegendQuest
    Mob Health: Simple damage reporting.LegendQuest: Complete RPG plugin.
  • Avatar of broluigi broluigi Nov 16, 2014 at 00:31 UTC - 0 likes

    @Mitsugaru: Go

    Can you send me a version of sqlibrary that works on 1.8! We need your plugin for legendquest to work on our servers! I would gladly donate if possible

  • Avatar of ThomasRaud ThomasRaud Sep 26, 2014 at 04:24 UTC - 0 likes

    Problem for using java8u20, reccomended version java is 7u22 or first 8.0(same as 7u22 but 8.0 readdy)

    CubeHex Thomasraud is here to help! If you looking help for WebSend and SMS Service, go to: WebSendHelp

    We try to build our HomePage, but its take a time! If you like to see our webseite development.. go ahead: HomePageDev

  • Avatar of dori99xd dori99xd Aug 24, 2014 at 12:05 UTC - 0 likes

    I' am getting an error:

    [13:59:23 INFO]: [SQLibrary] Loading SQLibrary v7.1 [13:59:23 INFO]: [Tials-Core] Loading Tials-Core v3.7 [13:59:23 INFO]: [SQLibrary] Enabling SQLibrary v7.1 [13:59:23 INFO]: SQLibrary loaded. [13:59:23 INFO]: [Tials-Core] Enabling Tials-Core v3.7 [13:59:23 ERROR]: [MyPlugin][H2] H2 driver class missing: org.h2.D river.

    And my code:

    public static Database database;

    public void onEnable() { database = new lib.PatPeter.SQLibrary.H2(Logger.getLogger("Minecraft"), "[MyPlugin]", this.getDataFolder().getAbsolutePath(), "MyPlugin", ".h2"); database.open(); }

    SqlLibary is in the Build Path as Libary and is in the plugin Folder....

  • Avatar of selfservice0 selfservice0 Jul 22, 2014 at 17:49 UTC - 0 likes

    @Mitsugaru: Go

    I tried bukkit 1.7.10 builds same thing happens. It only happens when I add SQLibrary.

    Send me your paypal and a link to the 1.7.10 build in pm please :D. I'll test it out and send you a reward if it solves the problem.

    PS: Basically what is happening is Skript uses sqlibrary for sql database connections. Skript itself runs fine in 1.7.10 and can store in alternative databases on 1.7.10 with no issue. But as soon as I add SQLibrary and have a moderate size mysql database (I lowered it from over 300,000 rows to 22,000 rows. still won't work) the server freezes as it tries to slowly load the sql database. In bukkit builds prior to 1.7.10 the database loads within a few seconds. After 1.7.10 it seems to load a few rows every few seconds, which is extremely slow.

    Last edited Jul 22, 2014 by selfservice0
  • Avatar of Mitsugaru Mitsugaru Jul 22, 2014 at 03:20 UTC - 0 likes

    @wiedzmin137: Go

    Have you done a null check on the player or sql objects?

    @selfservice0: Go

    My question is: what steps have you taken to determine that the library is at fault and not the specific implementation, Cauldron? Have you tested other plugins that utilize the library to have similar slow downs?

    @selfservice0: Go

    Appears to build fine for 1.7.10 without any code changes.

    Jenkins | Trello Dev board | Website
    Visit the Trello board to see/vote/comment on my current todo list for all projects in real time.
    Mine-RP Operator BFAK:52950,9c92ea09892ae7395af1f98741fb8dd73aed386e0b40f81435281ce5eca68c7f

  • Avatar of selfservice0 selfservice0 Jul 21, 2014 at 23:10 UTC - 0 likes

    If someone is willing/able to update this to work with 1.7.10 please message me I am more than willing to compensate for your hard.

    The github repositories is here: https://github.com/PatPeter/SQLibrary

  • Avatar of selfservice0 selfservice0 Jul 21, 2014 at 12:21 UTC - 0 likes

    this does not work properly with any of the cauldron 1.7.10 builds. Pulls sql data very slowly http://files.minecraftforge.net/Cauldron/

  • Avatar of wiedzmin137 wiedzmin137 Jul 07, 2014 at 14:57 UTC - 0 likes

    Hello. I have problem with NullPointerException at this line:

    ResultSet rs = sql.query("SELECT skill, level FROM skills WHERE name='" + player.getName().toLowerCase() + "';");
    

    Do you know what is reason of that error?

    ShadowKingdom - Revolution has just begun...

    Classes & SkillTrees | Feudal land management | Community engagement systems

Facts

Date created
Aug 25, 2012
Categories
Last update
Jan 14, 2014
Development stage
Release
Language
  • enUS
License
Creative Commons — Attribution-NonCommercial-NoDerivs 3.0 Unported (CC BY-NC-ND 3.0)
Curse link
SQLibrary
Downloads
36,425
Recent files

Authors