Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package en.datapi.database;
- 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");
- //Class.forName("com.mariadb.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 executeRawMySQL(String raw) {
- if(!MySQL.db.isConnected()) {
- MySQL.db.connect();
- }
- try {
- if (!this.usable) {
- return new MySQLResult(false, null);
- }
- ResultSet result = null;
- result = this.statement.executeQuery(raw);
- 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, String args) {
- if(!MySQL.db.isConnected()) {
- MySQL.db.connect();
- }
- try {
- if (!this.usable) {
- return;
- }
- //ex: (uuid TINYTEXT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'UUID des joueurs', PlayerName TINYTEXT NOT NULL, Connect TINYTEXT NOT NULL, Disconnect TINYTEXT NOT NULL, PlayerIP TINYTEXT NOT NULL)
- if (!MyISAM)
- this.statement.execute("CREATE TABLE IF NOT EXISTS " + tableName + " (" + args + ")" + " ENGINE=MyISAM");
- else {
- this.statement.execute("CREATE TABLE IF NOT EXISTS " + tableName + " (" + args + ")" + " 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());
- }
- }
- }
Add Comment
Please, Sign In to add comment