Coder Social home page Coder Social logo

bukkit-async-sql's Introduction

Bukkit Asynchronous SQL

Library for using SQL in bukkit plugins.

TeamCity Development Builds - CI Server

Example usage / How to use

Here are some examples of how to use bukkit-async-sql.

Starting connection

This is typically something that you would do once in your plugin. This creates an AsyncSQL instance that you can then use to do stuff with the database.

String sqlHost = "localhost";
int sqlPort = 3306;
String sqlDatabase = "minecraft";
String sqlUsername = "root";
String sqlPassword = "qwerty";
SQLConnectionInfo connectionInfo = new SQLConnectionInfo(sqlHost, sqlPort, sqlDatabase, sqlUsername, sqlPassword);

// You can also set any properties here, e.g.:
// connectionInfo.setProperty("characterEncoding", "utf8")

AsyncSQL sql = new AsyncSQL(skywars, skywars.getLogger(), connectionInfo); // Typically this would set an instance variable.

Creating a table

This statement creates a table with the given table structure. This is basically just running the SQL statement:

CREATE TABLE IF NOT EXISTS `" + tableName + "` (`username` VARCHAR(32), `user_score` INT, PRIMARY KEY (`username`));

Your table structure will probably differ, so you will want to change this statement to whatever SQL you want to run.

You can do something similar to this whenever you want to run an update without registering any result.

Another note, there is no need to catch any SQLExceptions inside the run method, bukkit-async-sql will handle them. It will also reconnect on any timeout-related exceptions so that all of the queries/updates do get run if possible.

final String tableName = "myTable"; // This would be set earlier, just declaring it here for example
sql.run("create user table", new SQLRunnable() {
    @Override
    public void run(Connection connection) throws SQLException {
        PreparedStatement statement = connection.prepareStatement("CREATE TABLE IF NOT EXISTS `" + tableName + "` (`username` VARCHAR(32), `user_score` INT, PRIMARY KEY (`username`));");
        try {
            statement.execute();
        } finally {
            statement.close();
        }
    }
});

Setting data

This is very similar to creating a table, just with a different query statement. Again, your table structure will probably differ, so just replace the query statement string with what your own.

The only thing that is different about this, is that it has query parameters. Notice how I have the '?' query parameter. You will want to use that instead of just adding the username to the query string, as that allows for sql injection. You can instead use the statement.setString and statement.setInt methods to add user input to the string. On the MySQL server, the ? will be replaced with the int/string you set.

// These variables are probably going to want to be method parameters
final int diff = 5;
final String name = "daboross";
sql.run("add " + diff + " score to " + name, new SQLRunnable() {
    @Override
    public void run(final Connection connection) throws SQLException {
        PreparedStatement statement = connection.prepareStatement("INSERT INTO `" + tableName + "` (username, user_score) VALUES (?, ?) ON DUPLICATE KEY UPDATE `user_score` = `user_score` + ?;");
        statement.setString(1, name);
        statement.setInt(2, diff);
        statement.setInt(3, diff);
        try {
            statement.execute();
        } finally {
            statement.close();
        }
    }
});

Retrieving data

TODO

bukkit-async-sql's People

Contributors

daboross avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.