API Databases API

Discussion in 'Resources' started by gpotter2, May 16, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    gpotter2

    Databases API
    [​IMG] [​IMG]
    Hey ! :D

    Here's a API to use MySQL and SQLite functions in java to use with bukkit/craftbukkit servers.

    The API is now safe against SQL Injections !

    Installation:

    Just add the 2 classes (DatabasesHandler.java & DatabasesUtil.java) to your plugin.

    Usage:

    Setting up the Handler:
    • First, create a new instance of the DatabasesHandler class. This will be different if you are using MySQL or SQLite:
    MySQL
    Code:
    DatabasesHandler handler = new DatabasesHandler(DatabaseType.MYSQL, "my_table_name");

    SQLite

    Code:
    String path = getDataFolder().getAbsolutePath() +  File.separator + "database.db";
    DatabasesHandler handler = new DatabasesHandler(DatabaseType.SQLITE, "my_table_name", path);
    • Then init the handler with init():

    MySQL
    Code:
    handler.init("host", "user", "password", "database_name", "port (default:3306)", "primary_key_name");
    SQLite
    Code:
    handler.init(null, null, null, "database_name", null, "primary_key_name");
    Add (if not exist) columns:
    Then you can add the columns (just add them, if they already exist, the handler will ignore it). The function works with the ObjectType enum. It correspond to the type of the data that will be contained in the column. You can see them in the docs. Here, VARCHAR is a String.
    Code:
    handler.addColumn("name", ObjectType.VARCHAR);
    handler.addColumn("value", ObjectType.VARCHAR);
    • You can now start to use the databases ! A dataObject will be insert in the line that correspond to all conditions (it may create the line). The "Condition" means "WHERE key=value" so (WHERE 'name'="my_object") would be new Condition("name", "my_object);
    Available Functions:

    Insert (or update if exist) a value:
    Code:
    DataObject[] da = new DataObject[2];
    da[0] = new DataObject("name", "my_object");
    da[1] = new DataObject("value", "Hey !");
    handler.InsertOrUpdateValue(da, new Condition("name", "my_object");
    Force Insert a value even if it already exist (deprecated):
    Code:
    DataObject[] da = new DataObject[2];
    da[0] = new DataObject("name", "my_object");
    da[1] = new DataObject("value", "Hey !");
    handler.InsertValueForce(da);
    Get all values that correspond to a Condition:
    Code:
    List<Object> values = handler.getValues("value", new Condition("name", "my_object"));
    Here, values contains only one object: a String: "Hey !"

    Detect if a value exist with Conditions:
    Code:
    boolean exist = handler.existInTable(new Condition("name", "my_object"));
    Get the number of objects existing with Conditions:
    Code:
    int number_exist = handler.numberObjectsInTable(new Condition("name", "my_object"));
    Delete a value with Conditions:
    Code:
    handler.deleteObject(new Condition("name", "my_object"));
    Clear a table:
    Code:
    handler.clearTable();
    Download:

    GitHub

    I hope this API will be useful for some one :D
     
    Last edited: Jul 29, 2015
    RunsWithShovels, Goblom and Totom3 like this.
  2. Offline

    nverdier

    That won't work.
     
  3. Offline

    gpotter2

    You're right :) Just updated :D
     
  4. Offline

    DJSkepter

    @gpotter2 This looks awesome. I might use this in future :)

    @gpotter2 What does 'existInTable' do and is there a way to delete a record from a database?
     
    Last edited: May 23, 2015
  5. Offline

    gpotter2

    I'm going to add it...

    The existInTable is just not finished.... Sorry for this small issue :)
     
  6. Offline

    Cirno

    I highly suggest you try to escape your SQL; SQL injections are a pain.
     
  7. Offline

    gpotter2

    I know that, but as this must be used by a plugin...... I was thinking it was not required..... anyway, you're right... I'll add it.
    I'm now doing a new version with escaping SQL, finishing the existintable function and adding a way to delete things :)
     
    Last edited: Jul 25, 2015
  8. Offline

    nverdier

    *Use prepared statements.
     
  9. Offline

    gpotter2

    A new version is out... Next one is for the prepared statements....
     
  10. Offline

    gpotter2

    New version !
    Now using prepared statements, and corrected bugs..... Every one should update their versions :)
     
Thread Status:
Not open for further replies.

Share This Page