MySQL Database Connect

1.3k Downloads

What is MySQL Database Connect

This plugin is designed for developers to have access to a simple plugin that can connect to a MySQL database.

General

This is designed for Developers of plugins and you only need to download it if you are a developer or another plugin requires this plugin to work.

This plugin is designed to provide a simple interface to access a MySQL Database. There is no database setup for preset storage configurations. This is a connector only.

How to install

You need to go and install the Java MySQL Connector for this plugin to work correctly. Do this before running the plugin. Download for Java MySQL Connector

Copy the Jar into the plugins folder of the Bukkit / Spigot Server. On first run of the plugin the config file will be automatically generated. From this config file you will need to edit the config to suit your needs (Connection Details).

If you have downloaded another plugin that requires this plugin read here.

When you start up your server for the next time, in your console it should print out a message that says 'Connected to Database'. If it prints out an error means you have probably got your config wrong or the MySQL database isn't started.

If you're a developer read here

It is only recommended to develop with plugin if you have knowledge of SQL and how library static instancing works, but you could get away with only knowing the SQL part.

After you have downloaded the plugin you will need to link it to your project. To do this in Eclipse:

  1. 1 Right Click your project and select properties
  2. 2 Go to Java Build Path, Libraries
  3. 3 Click 'Add External Jars' and find the Database Connection Jar which you have downloaded. Click Open, then click ok.
  4. 4 Now as the Jar has been added to your project

There are 2 methods/functions that you need to worry about. These are:

update(String sql);
Resultset data = get(String sql);

Both of these do throw an SQLException if something goes wrong.

The update function is to be used with SQL functions such as 'INSERT INTO' or 'UPDATE'. The get function is to be used with SQL functions such as 'SELECT'.

To access these functions you will need to access the Plugin instance. This is done by a static variable which is of the type of the JavaPlugin class of the Database plugin.

There are 2 ways of accessing.

The easyist way to access the database plugin is by the code below. In rare cases Due to Bukkit Class Loaders and init patterns, this will return a null.

Connect dbInstance = Connect.getInstance();

A way to avoid this is to do a post connection after onEnable of all plugins. This is done by using a Runnable on the Bukkit Scheduler. It also does a null check to make sure that the plugin initialized properly.

Connect dbInstance = null;

private void pendConnection() {
	Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
		public void run() {
			if(Connect.getInstance() != null)
				dbInstance  = Connect.getInstance();
			else
				pendConnection();
		}
	});
}

After you have done this you should be storing a object variable inside your project and not constantly accessing the getInstance() method. All data from the database is returned in the form of a ResultSet which is part of the java.sql library.

Known Issues

  • If the server is left with no one on it, the MySQL connector will disconnect and will require a server restart (Not Reload) to reconnect.

Change Log

  • 1.0.0 - First Release of Plugin

Bungee Cord

If you want to run the bungee cord version, on the download page for it, there is a list of requirements. The plugin is experimental and can rarely cause some odds things on the Bungee Cord Server.