A framework based on mysql-connector to simplify the usage of databases, tables & columns. This framework automatically creates the databases & tables if they do not already exist ; so you don't have to create them manually.
You can reuse EasySQL but make sure you comply with the LICENSE.
<dependencies>
<dependency>
<groupId>com.easysql</groupId>
<artifactId>EasySQL</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>finalEasySQLeasySQL = newEasySQL();
easySQL.setHost("127.0.0.1");
easySQL.setPort(3306);
easySQL.setUser("root");
easySQL.setPassword("password");
easySQL.setDatabase("demo");// First tablefinalTableplayerTable = newTable("players");
playerTable.setPrimaryKey("uuid");
playerTable.addColumn("uuid", "VARCHAR(36)");
playerTable.addColumn("kills", "INTEGER");
playerTable.addColumn("deaths", "INTEGER");
// Second tablefinalTabledemoTable = newTable("demo2");
demoTable.setPrimaryKey("uuid");
demoTable.addColumn("uuid", "VARCHAR(36)");
demoTable.addColumn("totalConnections", "INTEGER");
// Create the tables if they do not existeasySQL.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)"...
easySQL.connect();easySQL.close();easySQL.delete();playerTable.delete();finalColumncolumn = playerTable.getColumns();
column.insertDefault("cdb4810e-b975-4fbd-97be-3aa838a017aa", 0, 0); //uuid, kills, deaths --> see above to understand the order of valuescolumn.editValue("uuid", "cdb4810e-b975-4fbd-97be-3aa838a017aa", "kills", 2)if(column.isExists("uuid", "cdb4810e-b975-4fbd-97be-3aa838a017aa")) {
System.out.println("Yes !");
} else {
System.out.println("No !");
}System.out.println("Kills : " + column.getValue("uuid", "cdb4810e-b975-4fbd-97be-3aa838a017aa", "kills"));Returning Kills : 2
System.out.println("Table name : " + column.getTableName());Returning Table name : players
column.delete("uuid", "cdb4810e-b975-4fbd-97be-3aa838a017aa");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.
importcom.nz1337.easysql.*;
importcom.nz1337.easysql.objects.*;
importorg.bukkit.event.*;
importorg.bukkit.event.block.*;
importorg.bukkit.event.player.*;
importorg.bukkit.plugin.java.*;
publicclassTestextendsJavaPluginimplementsListener {
privateColumncolumn;
publicvoidonEnable() {
this.getServer().getPluginManager().registerEvents(this, this);
this.createDatabase();
}
privatevoidcreateDatabase() {
finalTabletable = newTable("breaked").setPrimaryKey("uuid").addColumn("uuid", "VARCHAR(36)").addColumn("blocks", "INTEGER");
finalEasySQLeasySQL = newEasySQL();
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();
}
@EventHandlerpublicvoidonConnect(finalPlayerJoinEventevent) {
finalStringuuid = event.getPlayer().getUniqueId().toString();
this.column.insertDefault(uuid, 0);
}
@EventHandlerpublicvoidonBreak(finalBlockBreakEventevent) {
finalStringuuid = event.getPlayer().getUniqueId().toString();
finalintoldBreakedBlocks = (int) this.column.getValue("uuid", uuid, "blocks");
System.out.println("Old value for " + uuid + " : " + oldBreakedBlocks);
this.column.editValue("uuid", uuid, "blocks", oldBreakedBlocks + 1);
finalintnewbreakedBlocks = (int) this.column.getValue("uuid", uuid, "blocks");
System.out.println("New value for " + uuid + " : " + newbreakedBlocks);
}
}After use :

