Guest User

Untitled

a guest
Apr 28th, 2016
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.07 KB | None | 0 0
  1. package en.datapi.database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class MySQLConnector {
  10.  
  11.     private String ip;
  12.     private String port;
  13.     private String database;
  14.     private String username;
  15.     private String password;
  16.     private Connection connection;
  17.     private Statement statement;
  18.     private boolean usable;
  19.  
  20.     public MySQLConnector(String ip, String port, String database, String username, String password) {
  21.         try {
  22.             Class.forName("com.mysql.jdbc.Driver");
  23.             //Class.forName("com.mariadb.jdbc.Driver");
  24.             this.ip = ip;
  25.             this.port = port;
  26.             this.database = database;
  27.             this.username = username;
  28.             this.password = password;
  29.             this.usable = true;
  30.         } catch (Exception ex) {
  31.             this.usable = false;
  32.             ex.printStackTrace();
  33.             System.out.println(ex.getMessage());
  34.         }
  35.     }
  36.  
  37.     public String getIP() {
  38.         return this.ip;
  39.     }
  40.  
  41.     public String getPORT() {
  42.         return this.port;
  43.     }
  44.  
  45.     public String getDatabaseName() {
  46.         return this.database;
  47.     }
  48.  
  49.     public String getUsername() {
  50.         return this.username;
  51.     }
  52.  
  53.     public String getPassword() {
  54.         return this.password;
  55.     }
  56.  
  57.     public void connect() {
  58.         try {
  59.             this.connection = DriverManager.getConnection("jdbc:mysql://" + this.ip + ":" + this.port + "/" + this.database, this.username, this.password);
  60.             this.statement = this.connection.createStatement();
  61.  
  62.         } catch (SQLException ex) {
  63.             ex.printStackTrace();
  64.             System.out.println(ex.getMessage());
  65.         }
  66.     }
  67.  
  68.     public void disconnect() {
  69.         try {
  70.             this.connection.close();
  71.         } catch (SQLException ex) {
  72.             ex.printStackTrace();
  73.             System.out.println(ex.getMessage());
  74.         }
  75.     }
  76.  
  77.     public Boolean isConnected() {
  78.         try {
  79.             if (this.connection.isClosed()) {
  80.                 return Boolean.valueOf(false);
  81.             }
  82.             return Boolean.valueOf(true);
  83.         } catch (Exception ex) {
  84.             ex.printStackTrace();
  85.             System.out.println(ex.getMessage());
  86.         }
  87.         return Boolean.valueOf(false);
  88.     }
  89.  
  90.     public Boolean isUsable() {
  91.         return Boolean.valueOf(this.usable);
  92.     }
  93.  
  94.     public MySQLResult getAllDataFromTable(String tableName) {
  95.  
  96.         if(!MySQL.db.isConnected()) {
  97.             MySQL.db.connect();
  98.         }
  99.  
  100.         try {
  101.             if (!this.usable) {
  102.                 return new MySQLResult(false, null);
  103.             }
  104.             ResultSet result = null;
  105.             result = this.statement.executeQuery("SELECT * FROM " + tableName);
  106.             return new MySQLResult(true, result);
  107.         } catch (Exception ex) {
  108.             ex.printStackTrace();
  109.             System.out.println(ex.getMessage());
  110.         }
  111.         return new MySQLResult(false, null);
  112.     }
  113.  
  114.  
  115.  
  116.     public MySQLResult executeRawMySQL(String raw) {
  117.         if(!MySQL.db.isConnected()) {
  118.             MySQL.db.connect();
  119.         }
  120.         try {
  121.             if (!this.usable) {
  122.                 return new MySQLResult(false, null);
  123.             }
  124.             ResultSet result = null;
  125.             result = this.statement.executeQuery(raw);
  126.             return new MySQLResult(true, result);
  127.         } catch (Exception ex) {
  128.             ex.printStackTrace();
  129.             System.out.println(ex.getMessage());
  130.         }
  131.         return new MySQLResult(false, null);
  132.     }
  133.  
  134.  
  135.     public MySQLResult getAllDataFromRow(String tableName, String field, String value) {
  136.         if(!MySQL.db.isConnected()) {
  137.             MySQL.db.connect();
  138.         }
  139.         try {
  140.             if (!this.usable) {
  141.                 return new MySQLResult(false, null);
  142.             }
  143.             ResultSet result = null;
  144.             result = this.statement.executeQuery("SELECT * FROM " + tableName + " WHERE " + field + "='" + value + "'");
  145.             return new MySQLResult(true, result);
  146.         } catch (Exception ex) {
  147.             ex.printStackTrace();
  148.             System.out.println(ex.getMessage());
  149.         }
  150.         return new MySQLResult(false, null);
  151.     }
  152.  
  153.     public void createTable(String tableName, boolean MyISAM, String args) {
  154.         if(!MySQL.db.isConnected()) {
  155.             MySQL.db.connect();
  156.         }
  157.         try {
  158.             if (!this.usable) {
  159.                 return;
  160.             }
  161.             //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)
  162.             if (!MyISAM)
  163.                 this.statement.execute("CREATE TABLE IF NOT EXISTS " + tableName + " (" + args + ")" + " ENGINE=MyISAM");
  164.             else {
  165.                 this.statement.execute("CREATE TABLE IF NOT EXISTS " + tableName + " (" + args + ")" + " ENGINE=MyISAM");
  166.             }
  167.         } catch (Exception ex) {
  168.             ex.printStackTrace();
  169.             System.out.println(ex.getMessage());
  170.         }
  171.     }
  172.  
  173.     public void removeTable(String tableName) {
  174.         if(!MySQL.db.isConnected()) {
  175.             MySQL.db.connect();
  176.         }
  177.         try {
  178.             if (!this.usable) {
  179.                 return;
  180.             }
  181.             this.statement.execute("DROP TABLE IF EXISTS " + tableName);
  182.         } catch (Exception ex) {
  183.             ex.printStackTrace();
  184.             System.out.println(ex.getMessage());
  185.         }
  186.     }
  187.  
  188.     public void insertData(String tableName, String fields, String values) {
  189.         if(!MySQL.db.isConnected()) {
  190.             MySQL.db.connect();
  191.         }
  192.         try {
  193.             if (!this.usable) {
  194.                 return;
  195.             }
  196.             this.statement.execute("INSERT INTO `" + tableName + "`(" + fields + ") VALUES (" + values + ")");
  197.         } catch (Exception ex) {
  198.             ex.printStackTrace();
  199.             System.out.println(ex.getMessage());
  200.         }
  201.     }
  202.  
  203.     public void updateAllData(String tableName, String field, String value) {
  204.         if(!MySQL.db.isConnected()) {
  205.             MySQL.db.connect();
  206.         }
  207.         try {
  208.             if (!this.usable) {
  209.                 return;
  210.             }
  211.             this.statement.executeUpdate("UPDATE " + tableName + " SET " + field + "='" + value + "'");
  212.         } catch (Exception ex) {
  213.             ex.printStackTrace();
  214.             System.out.println(ex.getMessage());
  215.         }
  216.     }
  217.  
  218.     public void updateData(String tableName, String updateField, String updateValue, String field, String value) {
  219.         if(!MySQL.db.isConnected()) {
  220.             MySQL.db.connect();
  221.         }
  222.         try {
  223.             if (!this.usable) {
  224.                 return;
  225.             }
  226.             this.statement.executeUpdate("UPDATE " + tableName + " SET " + updateField + "='" + updateValue + "' WHERE " + field + "='" + value + "'");
  227.         } catch (Exception ex) {
  228.             ex.printStackTrace();
  229.             System.out.println(ex.getMessage());
  230.         }
  231.     }
  232.  
  233.     public void deleteRow(String tableName, String field, String value) {
  234.         if(!MySQL.db.isConnected()) {
  235.             MySQL.db.connect();
  236.         }
  237.         try {
  238.             if (!this.usable) {
  239.                 return;
  240.             }
  241.             this.statement.execute("DELETE FROM " + tableName + " WHERE " + field + "='" + value + "'");
  242.         } catch (Exception ex) {
  243.             ex.printStackTrace();
  244.             System.out.println(ex.getMessage());
  245.         }
  246.  
  247.     }
  248.  
  249.     public void clearTable(String tableName) {
  250.         if(!MySQL.db.isConnected()) {
  251.             MySQL.db.connect();
  252.         }
  253.         try {
  254.             if (!this.usable) {
  255.                 return;
  256.             }
  257.             this.statement.execute("DELETE FROM " + tableName);
  258.         } catch (Exception ex) {
  259.             ex.printStackTrace();
  260.             System.out.println(ex.getMessage());
  261.         }
  262.     }
  263.  
  264. }
Add Comment
Please, Sign In to add comment