BoneAPI 1.0.0

A simple API for accessing BoneCP to establish a fast MySQL database connection.

  1. MinePeak
    Contributors:
    comniemeer, hiivin, BoneCP Team
    Please remember: This is just an API for developers, and if they told you to come here, because its needed for their projects, then download it and put it in your plugins folder on your Bukkit/Spigot/Bungee-Server.

    As a developer:

    Simply put this in your BuildPath and you can start using the MySQL Pool!
    We also recommend you to implement the following class:
    Code (Text):
    package de.minepeak.mysql;

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    import com.jolbox.bonecp.BoneCP;
    import com.jolbox.bonecp.BoneCPConfig;

    public class MySQL {

        private BoneCP boneCP = null;

        public MySQL() {
            String db = "MySQL.";
     
            String host = this.getConfig().getString(db + "Host");
            int port = this.getConfig().getInt(db + "Port");
            String user = this.getConfig().getString(db + "User");
            String password = this.getConfig().getString(db + "Password");
            String database = this.getConfig().getString(db + "Database");
            int minConnections = this.getConfig().getInt(db + "MinConnections");
            int maxConnections = this.getConfig().getInt(db + "MaxConnections");
     
            BoneCPConfig boneCPConfig = new BoneCPConfig();
     
            boneCPConfig.setJdbcUrl("jdbc:mysql://" + host + ":" + port + "/" + database);
            boneCPConfig.setUsername(user);
            boneCPConfig.setPassword(password);
            boneCPConfig.setMinConnectionsPerPartition(minConnections);
            boneCPConfig.setMaxConnectionsPerPartition(maxConnections);
     
            try {
                this.boneCP = new BoneCP(boneCPConfig);
            } catch (SQLException e) {
                System.out.println("Error whilst creating the BoneCP instance: " + e.getMessage());
            }
        }

        public Connection getConnection() {
            try {
                return boneCP.getConnection();
            } catch (SQLException e) {
                System.out.println("Error whilst getting the connection: " + e.getMessage());
                return null;
            }
        }

        /**
         * Sends a query to the database.
         * @param query The query to execute.
         * @deprecated Not recommended because SQL Injection could happen
         */
        @Deprecated
        public void queryUpdate(String query) {
            Connection connection = this.getConnection();
            PreparedStatement preparedStatement = null;
     
            try {
                preparedStatement = connection.prepareStatement(query);
         
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                System.out.println("Error whilst executing the query \"" + query + "\": " + e.getMessage());
            } finally {
                this.closeRessources(null, preparedStatement, connection);
            }
        }

        public void closeRessources(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    System.out.println("Error whilst closing the ResultSet: " + e.getMessage());
                }
            }
     
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    System.out.println("Error whilst closing the PreparedStatements: " + e.getMessage());
                }
            }
     
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    System.out.println("Error whilst closing the connection: " + e.getMessage());
                }
            }
        }
    }
    Be free to modify it for your needs.
    Hint:
    MinConnections and MaxConnections mean BoneCP opens minimum x and maximum y connections. Its recommended you use min 5 and max 10. It depends on how many queries you perform.
    After that, you can access your database with the most stable and fastest connection in the world!
    Note you have to put "depend: [BoneAPI]" into your plugin.yml!