Code Monkey home page Code Monkey logo

easysql's Introduction

Description

A lib based on mysql-connector to simplify the usage of databases, tables & columns. This lib automatically creates the databases & tables if they do not already exist ; so you don't have to create them manually.

If you have any problem, DM me on discord (692109214104944650)

Developers

You can reuse EasySQL but make sure you comply with the LICENSE.

Maven

<dependencies>
    <dependency>
        <groupId>com.easysql</groupId>
        <artifactId>EasySQL</artifactId>
        <version>1.0.3</version>
    </dependency>
</dependencies>

Usage

โš ๏ธ THERE ARE NOT ALL THE METHODS HERE. THESE ARE ONLY EXAMPLES.

Simple configure connection & database

final EasySQL easySQL = new EasySQL();
easySQL.setHost("127.0.0.1");
easySQL.setPort(3306);
easySQL.setUser("root");
easySQL.setPassword("password");
easySQL.setDatabase("demo");

Add specific tables

// First table
final Table playerTable = new Table("players");
playerTable.setPrimaryKey("uuid");
playerTable.addColumn("uuid", "VARCHAR(36)");
playerTable.addColumn("kills", "INTEGER");
playerTable.addColumn("deaths", "INTEGER");

// Second table
final Table demoTable = new Table("demo2");
demoTable.setPrimaryKey("uuid");
demoTable.addColumn("uuid", "VARCHAR(36)");
demoTable.addColumn("totalConnections", "INTEGER");

// Create the tables if they do not exist
easySQL.createDefaultTables(playerTable, demoTable); //Here you can enter the number of tables you want 

As you can see above, you can use "int" instead of "INTEGER", "string" instead of "VARCHAR(255)", "double" instead of "DOUBLE" and more. But you can also insert the default types like "INTEGER", "VARCHAR(36)"...

Etablish connection

easySQL.connect();

Close connection

easySQL.close();

Delete database

easySQL.delete();

Delete table

playerTable.delete();

Insert default VALUES

final Column column = playerTable.getColumns();
column.insertDefault("cdb4810e-b975-4fbd-97be-3aa838a017aa", 0, 0); //uuid, kills, deaths --> see above to understand the order of values

Edit values (replacement)

column.editValue("uuid", "cdb4810e-b975-4fbd-97be-3aa838a017aa", "kills", 2)

Check if value exists in specific column

if(column.isExists("uuid", "cdb4810e-b975-4fbd-97be-3aa838a017aa")) {
  System.out.println("Yes !");
} else {
  System.out.println("No !");
}

Getting value

System.out.println("Kills : " + column.getValue("uuid", "cdb4810e-b975-4fbd-97be-3aa838a017aa", "kills"));

Returning Kills : 2

Get table name

System.out.println("Table name : " + column.getTableName());

Returning Table name : players

Remove row in column

column.delete("uuid", "cdb4810e-b975-4fbd-97be-3aa838a017aa");

Simple example for bukkit

In this example, if the player who connects does not have a profile in the database, plugin will create one for him.

Then, for each broken block, add 1 to "blocks" in database and print that value.

import com.nz1337.easysql.*;
import com.nz1337.easysql.manager.*;
import org.bukkit.event.*;
import org.bukkit.event.block.*;
import org.bukkit.event.player.*;
import org.bukkit.plugin.java.*;

public class Test extends JavaPlugin implements Listener {

    private Column column;

    public void onEnable() {
        this.getServer().getPluginManager().registerEvents(this, this);
        this.createDatabase();
    }

    private void createDatabase() {
        final Table table = new Table("breaked").setPrimaryKey("uuid").addColumn("uuid", "VARCHAR(36)").addColumn("blocks", "INTEGER");
        final EasySQL easySQL = new EasySQL();
        easySQL.setHost("127.0.0.1");
        easySQL.setPort(3306);
        easySQL.setUser("root");
        easySQL.setPassword("password");
        easySQL.setDatabase("server");
        easySQL.createDefaultTables(table);
        easySQL.connect();
        
        this.column = table.getColumns();
    }

    @EventHandler
    public void onConnect(final PlayerJoinEvent event) {
        final String uuid = event.getPlayer().getUniqueId().toString();
        this.column.insertDefault(uuid, 0);
    }

    @EventHandler
    public void onBreak(final BlockBreakEvent event) {
        final String uuid = event.getPlayer().getUniqueId().toString();
        final int oldBreakedBlocks = (int) this.column.getValue("uuid", uuid, "blocks");
        System.out.println("Old value for " + uuid + " : " + oldBreakedBlocks);
        
        this.column.editValue("uuid", uuid, "blocks", oldBreakedBlocks + 1);
        
        final int newbreakedBlocks = (int) this.column.getValue("uuid", uuid, "blocks");
        System.out.println("New value for " + uuid + " : " + newbreakedBlocks);
    }
}

After use :

easysql's People

Contributors

thisisnzed avatar

Watchers

 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.