Hello, I had a question about how to handle MySQL Queries. So this plugin I'm working on records kills and deaths from my main plugin. I have two HashMaps with the player's name and an integer. The two are kills and deaths. My question was how should I go about uploading the stats of the player and what time is recommended so it is not intensive. Another question I had is should I just onDisable() if one of the HashMaps have information in it then save to a config then onEnable I would just put it back into the HashMap. Here is my KDManager Class: Code: package com.mcforger.kitpvpkd.utils; import java.util.HashMap; import java.util.Map; import com.mcforger.kitpvpkd.KitPVPKD; import com.mcforger.kitpvpkd.database.Row; public class KDManager { private final KitPVPKD _plugin; private final Map<String, Integer> kills = new HashMap<String, Integer>(); private final Map<String, Integer> deaths = new HashMap<String, Integer>(); public enum DBTypes { kills, deaths; } public KDManager(KitPVPKD main) { _plugin = main; } public int getType(DBTypes type, String playerName) { try { if (type.equals(DBTypes.kills)) { return getIntFromRow( _plugin.db.getRow("SELECT `kills` FROM `kitpvp_players` WHERE `player`='" + playerName + "'"), 1); } else if (type.equals(DBTypes.deaths)) { return getIntFromRow( _plugin.db.getRow("SELECT `deaths` FROM `kitpvp_players` WHERE `player`='" + playerName + "'"), 1); } return 0; } catch (Exception e) { return 0; } } public boolean addKill(int amount) { return false; } public boolean addDeath(int amount) { return false; } public void setKills(String playerName, int amount) { this.kills.put(playerName, amount); } public void setDeaths(String playerName, int amount) { this.deaths.put(playerName, amount); } public int getIntFromRow(Row row, int column) { try { return row.getInt(column); } catch (Exception e) { return 0; } } public int getIntFromRow(Row row, String column) { try { return row.getInt(column); } catch (Exception e) { return 0; } } public double getDoubleFromRow(Row row, int column) { try { return row.getDouble(column); } catch (Exception e) { return 0; } } public double getDoubleFromRow(Row row, String column) { try { return row.getDouble(column); } catch (Exception e) { return 0; } } } The MySQL Table I make is this: Code: String query = "CREATE TABLE IF NOT EXISTS `kitpvp_players`(`player` VARCHAR(255) NOT NULL, `kills` number(4) NOT NULL, `deaths` number(4) NOT NULL , PRIMARY KEY (`player`))"; Any help is appreciated!
idk if this helpes u but... Code: package your.package.here; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class MySQLDatabase { private final String host; private final String port; private final String username; private final String password; private final String database; private final String url; private Connection connection; public final String getHost() { return host; } public final String getPort() { return port; } public final String getUsername() { return username; } public final String getPassword() { return password; } public final String getDatabase() { return database; } public final String getUrl() { return url; } public final Connection getConnection() { return connection; } public MySQLDatabase(final String host, final String port, final String username, final String password, final String database) { this.host = host; this.port = port; this.username = username; this.password = password; this.database = database; url = "jdbc:mysql://" + host + ":" + port + "/" + database; } public Connection open() throws Exception { try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(url, username, password); return connection; } catch (final Exception e) { throw e; } } public void close() throws Exception { try { if (connection != null) connection.close(); } catch (final Exception e) { throw e; } } public ResultSet query(final String sql) throws Exception { Statement statement; ResultSet result; try { statement = connection.createStatement(); result = statement.executeQuery(sql); return result; } catch (final Exception e) { throw e; } } public int update(final String sql) throws Exception { Statement statement; int result; try { statement = connection.createStatement(); result = statement.executeUpdate(sql); return result; } catch (final Exception e) { throw e; } } } example: Code: private String host = "localhost"; private String port = "3306"; private String username = "root"; private String password = "password"; private String database = "minecraft"; MySQLDatabase mySQLDatabase; @Override public void onEnable() { try { mySQLDatabase = new MySQLDatabase(host, port, username, password, database); } catch (final Exception e) { //handle exceptions } } @Override public void onDisable() { try { mySQLDatabase.close(); } } catch (final Exception e) { //handle exceptions } } ResultSet result = mySQLDatabase.query("SELECT `player` FROM `players`;"); mySQLDatabase.update("INSERT INTO `players`(`player`) VALUES ('" + player.getName() + "');");
I already have all this code. I'm talking about querying the information in a thread and how long should it go for and how should I update the database. Also trying to see what to do with information that didn't get to get queried in time. Thank you though