SQLibrary
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
- Firebird
- FrontBase
- DB2
- H2
- Informix
- Ingres
- MaxDB
- MicrosoftSQL
- MySQL
- Mongo
- mSQL
- Oracle
- Ovrimos
- PostgreSQL
- SQLite
Installation
- Download the jar.
- In Eclipse, go to Project Properties ==> Java Build Path ==> Add External Jar and add SQLibrary.jar.
- 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:
- 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.
- You cannot run your plugin alongside other plugins using SQLibrary directly from source. Same issue.
- 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.
@awesomefacecreeper
nickbrickmaster needs to be in quotes, otherwise SQL perceives it as a column name:
Also, please post a thread here once you finish your plugin:
http://dev.bukkit.org/bukkit-plugins/sqlibrary/forum/implementing-plugins/
I'm getting an error when trying to insert a row.
My query is
INSERT INTO radiationLevels (player, level) VALUES (nickbrickmaster, 1.0)
player and level both exist in the radiationLevels table. It says SQLibrary v6.0, but I downloaded the 6.1 version.
Thank you for this. Very useful!
@Mitsugaru
Yep, sorry about that everyone. I wasn't standardized before, but according to the Maven naming conventions, I should be now.
Also, Mitsugaru told me how to subscribe to comments, so I shouldn't miss any important discussions like the one we had about a year ago over using a jar.
Maven devs, please note that the group id has been changed in the most recent release to:
lib.PatPeter.SQLibrary
And the version is 6.1 in Maven.
@PatPeter
Sorry for the late reply. I've sent you a PM regarding public Maven repository details.
For developers, version 4.2 is already available:
Future changes may be deployed automatically once the project's GitHub and POM are setup.
@PatPeter - I am getting some errors trying to use MYSQL with KarmicJail that appear to pertain to your plugin or I've got something setup very wrong.
Here's a pastebin of what I'm getting when Karmic load in:
http://pastebin.com/QWEjVU2Z
Any idea what would cause this?
Best, JAG100
IT Works on 1.6.2
Any news on when 1.6.2 will be available?
@Edguu
Will do.
@dotapurex1
BukkitDev had to ok it.
@jake0o0
Mitsugaru is going to help me set it up.
Is there a Maven repo?
@PatPeter
Can we please get the DL link?
@PatPeter
Could you recompile with 1.6.2 please?
Version 6.0 is released and compiled against 1.6.1 Bukkit.
I will be disabling comments to spur forum use, so that I only have to check one page daily.
I really wish BukkitDev notified you of comments. I've had the Help forum pinned in Firefox ever since I created it:
http://dev.bukkit.org/bukkit-plugins/sqlibrary/forum/help/
If any of you had issues, you should have posted them here.
Installation
Download the jar. In Eclipse, go to Project Properties ==> Java Build Path ==> Add External Jar and add SQLibrary.jar. 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:
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. You cannot run your plugin alongside other plugins using SQLibrary directly from source. Same issue. Updating will definitely cause these problems and cause you much pain.
The nerve to call this a plugin, it's like saying here's your new game, you just need to rewrite the first 5 lines of code to play it.
@ShinyQuagsire
Actually I had no issues with the plugin bookshelf until you had it forcibly changed, now I got much reworking to do, thanks PeterPiper.
@bloodless2010
This is literally just a database wrapper, meaning that if you have any issues with it, there's a good chance that it's an issue with your plugin, not this one. I've based my plugin on this API for quite some time, and I've never had any issues with it yet.
Is there anywhere I can get development help with this? I keep getting an error dealing with resolve type.
are there any conflicts? I'm using a plugin that uses this and for some reason I get weird problems (Someone can only register once and then I have to reload the server fully for another person to register)