c3p0 Extension

This project is abandoned and its default file will likely not work with the most recent version of Minecraft. Whether this project is out of date or its author has marked it as abandoned, this project is no longer maintained.

Storing data for your plugin using a database can be problematic and difficult, and even more so when trying to include support for the various types of database around. The c3p0 extension plugin takes advantage of the functionality of c3p0 and allows you to support all database types supported by c3p0, as well as not having to worry about open, idle or dead connections, or any of the other problematic situations that can arise. Since only one instance of the plugin is ever created, memory is saved from having to use multiple drivers and implementations, and provides a simple, efficient, managed connection system.

Adding the Dependency

On itself, the plugin does nothing. To use the plugin, you must add it into your "/plugins/" folder, and add it as a dependency to your existing plugin, including adding it as a dependency in the plugins config.yml:

name: SafeCity
main: me.jayfella.SafeCity.SafeCityPlugin
version: 1.0.0
author: jayfella
depend: [ Vault, c3p0Extension ]
Useage

Two objects are required to start using your database: A reference to the plugin itself, and a DatabaseConnection object.

public final class DatabaseExample
{
    private final C3p0ExtensionPlugin c3p0;
    private final DatabaseConnection databaseConnection;

    public DatabaseExample(Plugin plugin)
    {

        c3p0 = (C3p0ExtensionPlugin)plugin.getServer().getPluginManager().getPlugin("c3p0Extension");

        databaseConnection = new DatabaseConnection(
                DatabaseType.valueOf("MYSQL"),
                "127.0.0.1",
                3306,
                "nameOfDatabase",
                "username",
                "password");

        if (!c3p0.createDataSource(databaseConnection))
        {
            Bukkit.getPluginManager().disablePlugin(plugin);
            return;
        }
    }

    public Connection getConnection() throws SQLException
    {
        return c3p0.getConnection(databaseConnection);
    }

    private void doSomething()
    {
        Connection connection = null;
        PreparedStatement ps = null;
                
        try
        {
            StringBuilder statement = new StringBuilder()
                    .append("UPDATE nameOfDatabase SET ")
                    .append("owner = ? ")
                    .append("WHERE id = ?");
                    
            connection = this.getConnection();
            ps = connection.prepareStatement(statement.toString());
                    
            ps.setString(1, "jayfella");
            ps.setInt(2, 1337);
                    
            ps.executeUpdate();
        } 
        catch (SQLException ex)
        {
            Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
            return;
        }
        finally
        {
            try
            {
                if (ps != null) { ps.close(); }
                if (connection != null) { connection.close(); }
            }
            catch (SQLException ex)
            {
                context.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
                return;
            }
        }
    }

}

The above example creates a new database connection and creates a datasource. If the connection fails, it disables the plugin. The doSomething() method shows an example how how to obtain a connection and execute a prepared statement.

An example showing three plugins using the c3p0 extension, and the connections being made in the console on startup: Connection example


Comments

Posts Quoted:
Reply
Clear All Quotes

About This Project

Categories

Members

Recent Files