EasyDB
EasyDB is a developer tool that makes working with MySQL easier. I wrote this so all of my plugins could share the same MySQL database easily.
Installation
- Put the plugin in your plugins folder and run the plugin.
- Modify
config.yml
. - Type
/dbreload
.
Now you are configured to use EasyDB! You can stop reading now.
Why EasyDB?
- It uses prepared statements for everything, allowing your code to be safer.
- It prevents you from writing a lot of try/catch statements
- It has a Maven repository for you to use
- It uses Apache DBUtils, a professional database library built for big and small applications
- It enables you to write a lot less code to perform queries.
Maven Repository
To use this plugin as a dependency in your project, add the following to your pom.xml:
<repository>
<id>nl-repo</id>
<url>http://nexus.new-liberty.net/content/groups/public/</url>
</repository>
<dependency>
<groupId>com.simplyian</groupId>
<artifactId>easydb</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
API
If you have any questions, feel free to add me on Skype - simplyianm
.
The database object can be accessed with EasyDB.getDb()
. This is the only object you need to start dealing with the database. Read the Javadocs for Database
for in-depth information.
There are only 4 methods you need to know to do everything you would ever need.
boolean isValid()
Checks if the database connection is valid.
Example
if (db.isValid()) {
getLogger().log(Level.INFO, "Database info is valid!");
}
void update(String query, Object... params)
Runs a database update.
Example
db.update("DELETE FROM bans WHERE player_name = ?", "albireox");
Since we are using prepared statements, you need to put ?
marks where you would normally put the data in your queries then set the parameter after the query.
<T> T query(String query, ResultSetHandler<T> handler, Object... params)
Queries the database and returns the result.
Example
String name = db.query("SELECT firstname, lastname FROM people WHERE address = ?", new ResultSetHandler<String>{
@Override
public String handle(ResultSet rs) {
rs.next();
return rs.getString("firstname") + " " + rs.getString("lastname");
}
});
Object get(String query, Object def, Object... params)
Gets one value from the database.
Example
int money = ((Integer) db.get("SELECT amount FROM money WHERE player = ?", 0, "albireox")).intValue();
:) waiting
Good Job