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.
@Leeman1234
Never mind. Maybe should at the dependency in the plugin.yml D:
Hey! Keep getting "org.bukkit.plugin.InvalidPluginException: java.lang.NoClassDefFoundError: lib/PatPeter/SQLibrary/Database" on startup. However the SQLibrary.jar is added as External Jar in the Buildpath. What am I doing wrong?
@Puschie
Actually I'd like this too. Atm, i have a wrapper for all my classes, which is messy >.>
@Schlumpf9
No. All MySQL databases require a password to create an account. Otherwise SQLibrary can't connect using any credentials.
Hello, is there a way to connect to a MySQL Server without setting a password? If I set it "null" or "" I get an error, that it can't be empty. Is there a way to do a connection without a password or is that not possible?
Hey, what do you thing about a result handling class, so that the sql throws fully handle by your plugin. I think it woud make the using very simple and safty.
Im trying to use mySQL on my server for hunger games but im confused on the setup
and btw can anyone help me with my server banner thats not working?
@PatPeter
Please look at my error on the help forums. I can't get it working.
@ftbastler
If you want to run asynchronously, create a new thread as Bukkit specifies. If you are running select statements in each thread, you should be safe, but if you are updating/inserting/deleting, you need to use the synchronized keyword:
@storm345
That's another reason we want everyone to switch over to the SQLibrary.jar, it keeps everyone updated and there aren't old versions of SQLibrary strewn throughout the internet.
@storm345
Don't worry. I fixed it. The api had changed and it was throwing random errors EVERYWHERE. Changed it to match new api and it now works!
Is it the same as my error?
Just changed to using the jar instead of source and I cannot get rid of NoClassDefFoundError! Help??
Bukkit's plugin tutorial shows it as: getLogger().info( "This is a log message!" ); instead of: Logger.getLogger("Minecraft")
Can you update the tutorial to reflect if we can use getLogger() instead of Logger.getLogger("Minecraft")?
I believe you have to do that in an async task
Does this run the mysql queries async?
4.2 has been released! Please download the jar when it is approved by BukkitDev.
Documentation for 4.2 is on Github: https://github.com/PatPeter/SQLibrary/
The version number in the plugin.yml file is wrong. It shows 4.0 in the 4.1 download.
Getting the same errors
Where is the documentation?
Please view my thread in the help section of the forums. Also, I added a thread for BookShelf in implementing plugins. :)