promotional bannermobile promotional banner

c3p0 Extension

Abandoned
A simplified multi-type database manager for plugins.

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

The c3p0 Extension Team

profile avatar
  • 5
    Projects
  • 25.4K
    Downloads

More from _ForgeUser9414361View all

  • SafeCity project image

    SafeCity

    • 9.3K
    • Bukkit Plugins

    Buy, rent and sell land to become the biggest city on the map!

    • 9.3K
    • October 1, 2013
    • Bukkit Plugins
    • +1
  • WebOp project image

    WebOp

    • 6.4K
    • Bukkit Plugins

    Server health, log search and console access via browser!

    • 6.4K
    • September 13, 2013
    • Bukkit Plugins
  • BlockShop project image

    BlockShop

    • 4.4K
    • Bukkit Plugins

    An effective shop plugin without being overly-complicated

    • 4.4K
    • September 13, 2013
    • Bukkit Plugins
  • Jayfella's SimpleJail project image

    Jayfella's SimpleJail

    • 3.3K
    • Bukkit Plugins

    A simple jail plugin with criminal history records.

    • 3.3K
    • May 6, 2013
    • Bukkit Plugins
  • SafeCity project image

    SafeCity

    • 9.3K
    • Bukkit Plugins

    Buy, rent and sell land to become the biggest city on the map!

    • 9.3K
    • October 1, 2013
    • Bukkit Plugins
    • +1
  • WebOp project image

    WebOp

    • 6.4K
    • Bukkit Plugins

    Server health, log search and console access via browser!

    • 6.4K
    • September 13, 2013
    • Bukkit Plugins
  • BlockShop project image

    BlockShop

    • 4.4K
    • Bukkit Plugins

    An effective shop plugin without being overly-complicated

    • 4.4K
    • September 13, 2013
    • Bukkit Plugins
  • Jayfella's SimpleJail project image

    Jayfella's SimpleJail

    • 3.3K
    • Bukkit Plugins

    A simple jail plugin with criminal history records.

    • 3.3K
    • May 6, 2013
    • Bukkit Plugins