Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *pour avoir les coins d'un joueur pas une uuid
- */
- public static int getCoins(UUID uuid) {
- int Coins = 0;
- ResultSet rs = db.getAllDataFromRow(db_money, "uuid", uuid.toString()).getResultSet();
- try {
- while(rs.next()) {
- Coins = rs.getInt("coins");
- }
- } catch(SQLException e) {
- e.printStackTrace();
- }
- return Coins;
- }
- //=============================================================================================================================
- //pour se co à la bdd
- MySQL.db.connect();
- //=============================================================================================================================
- //MySQLManager
- public class MySQLManager
- {
- public static MySQLConnector getNewConnector(String ip, String port, String database, String username, String password)
- {
- return new MySQLConnector(ip, port, database, username, password);
- }
- }
- //=============================================================================================================================
- //MySQLResult
- import java.sql.ResultSet;
- public class MySQLResult
- {
- private boolean success;
- private ResultSet result;
- public MySQLResult(boolean success, ResultSet result)
- {
- this.success = success;
- this.result = result;
- }
- public Boolean isSuccessful()
- {
- return Boolean.valueOf(this.success);
- }
- public ResultSet getResultSet()
- {
- return this.result;
- }
- //=============================================================================================================================
- //MySQLconnector
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class MySQLConnector {
- private String ip;
- private String port;
- private String database;
- private String username;
- private String password;
- private Connection connection;
- private Statement statement;
- private boolean usable;
- public MySQLConnector(String ip, String port, String database, String username, String password) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- this.ip = ip;
- this.port = port;
- this.database = database;
- this.username = username;
- this.password = password;
- this.usable = true;
- } catch (Exception ex) {
- this.usable = false;
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- }
- public String getIP() {
- return this.ip;
- }
- public String getPORT() {
- return this.port;
- }
- public String getDatabaseName() {
- return this.database;
- }
- public String getUsername() {
- return this.username;
- }
- public String getPassword() {
- return this.password;
- }
- public void connect() {
- try {
- this.connection = DriverManager.getConnection("jdbc:mysql://" + this.ip + ":" + this.port + "/" + this.database, this.username, this.password);
- this.statement = this.connection.createStatement();
- } catch (SQLException ex) {
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- }
- public void disconnect() {
- try {
- this.connection.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- }
- public Boolean isConnected() {
- try {
- if (this.connection.isClosed()) {
- return Boolean.valueOf(false);
- }
- return Boolean.valueOf(true);
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- return Boolean.valueOf(false);
- }
- public Boolean isUsable() {
- return Boolean.valueOf(this.usable);
- }
- public MySQLResult getAllDataFromTable(String tableName) {
- if(!MySQL.db.isConnected()) {
- MySQL.db.connect();
- }
- try {
- if (!this.usable) {
- return new MySQLResult(false, null);
- }
- ResultSet result = null;
- result = this.statement.executeQuery("SELECT * FROM " + tableName);
- return new MySQLResult(true, result);
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- return new MySQLResult(false, null);
- }
- public MySQLResult getAllDataFromRow(String tableName, String field, String value) {
- if(!MySQL.db.isConnected()) {
- MySQL.db.connect();
- }
- try {
- if (!this.usable) {
- return new MySQLResult(false, null);
- }
- ResultSet result = null;
- result = this.statement.executeQuery("SELECT * FROM " + tableName + " WHERE " + field + "='" + value + "'");
- return new MySQLResult(true, result);
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- return new MySQLResult(false, null);
- }
- public void createTable(String tableName, boolean MyISAM) {
- if(!MySQL.db.isConnected()) {
- MySQL.db.connect();
- }
- try {
- if (!this.usable) {
- return;
- }
- if (!MyISAM)
- this.statement.execute("CREATE TABLE IF NOT EXISTS " + tableName);
- else {
- this.statement.execute("CREATE TABLE IF NOT EXISTS " + tableName + " ENGINE=MyISAM");
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- }
- public void removeTable(String tableName) {
- if(!MySQL.db.isConnected()) {
- MySQL.db.connect();
- }
- try {
- if (!this.usable) {
- return;
- }
- this.statement.execute("DROP TABLE IF EXISTS " + tableName);
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- }
- public void insertData(String tableName, String fields, String values) {
- if(!MySQL.db.isConnected()) {
- MySQL.db.connect();
- }
- try {
- if (!this.usable) {
- return;
- }
- this.statement.execute("INSERT INTO `" + tableName + "`(" + fields + ") VALUES (" + values + ")");
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- }
- public void updateAllData(String tableName, String field, String value) {
- if(!MySQL.db.isConnected()) {
- MySQL.db.connect();
- }
- try {
- if (!this.usable) {
- return;
- }
- this.statement.executeUpdate("UPDATE " + tableName + " SET " + field + "='" + value + "'");
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- }
- public void updateData(String tableName, String updateField, String updateValue, String field, String value) {
- if(!MySQL.db.isConnected()) {
- MySQL.db.connect();
- }
- try {
- if (!this.usable) {
- return;
- }
- this.statement.executeUpdate("UPDATE " + tableName + " SET " + updateField + "='" + updateValue + "' WHERE " + field + "='" + value + "'");
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- }
- public void deleteRow(String tableName, String field, String value) {
- if(!MySQL.db.isConnected()) {
- MySQL.db.connect();
- }
- try {
- if (!this.usable) {
- return;
- }
- this.statement.execute("DELETE FROM " + tableName + " WHERE " + field + "='" + value + "'");
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- }
- public void clearTable(String tableName) {
- if(!MySQL.db.isConnected()) {
- MySQL.db.connect();
- }
- try {
- if (!this.usable) {
- return;
- }
- this.statement.execute("DELETE FROM " + tableName);
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- }
- public MySQLResult executeRawQuery(String rawSQL) {
- if(!MySQL.db.isConnected()) {
- MySQL.db.connect();
- }
- try {
- if (!this.usable) {
- return new MySQLResult(false, null);
- }
- return new MySQLResult(true, this.statement.executeQuery(rawSQL));
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- return new MySQLResult(false, null);
- }
- public void executeRaw(String rawSQL) {
- if(!MySQL.db.isConnected()) {
- MySQL.db.connect();
- }
- try {
- if (!this.usable) {
- return;
- }
- this.statement.execute(rawSQL);
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println(ex.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement