Advertisement
Guest User

MySQL.java

a guest
May 29th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.88 KB | None | 0 0
  1. package com.skin_98.MySQL.DataBase;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. import org.bukkit.configuration.file.FileConfiguration;
  10. import org.bukkit.configuration.file.YamlConfiguration;
  11.  
  12. import com.mysql.jdbc.Connection;
  13. import com.mysql.jdbc.PreparedStatement;
  14.  
  15. /**
  16. * MySQL adatbázis util
  17. */
  18. public class MySQL {
  19.  
  20.         //Alap változók
  21.         public String HOST;
  22.         public String PORT;
  23.         public String DATABASE;
  24.         public String USERNAME;
  25.         private String PASSWORD; //<- Rejtett
  26.         public int TIMEOUT;
  27.    
  28.         //Kapcsolat
  29.         private Connection conn;
  30.    
  31.         //Instance
  32.         private static MySQL instance = null;
  33.    
  34.         /**
  35.          * Inicializálás
  36.          * @throws Exception
  37.          */
  38.         public void init() throws Exception {
  39.             if(instance != null) {
  40.                 throw new Exception("MySQL has already initialized!");
  41.             }
  42.             File MySQL_FILE = new File("plugins","MySQL.yml");
  43.             FileConfiguration sql_file = YamlConfiguration.loadConfiguration(MySQL_FILE);
  44.        
  45.             //Konfiguráció alapértelmezett értékei
  46.             String db = "MySQL.";
  47.             sql_file.addDefault(db + "host", "127.0.0.1");
  48.             sql_file.addDefault(db + "port", "3306");
  49.             sql_file.addDefault(db + "username", "user");
  50.             sql_file.addDefault(db + "password", "password");
  51.             sql_file.addDefault(db + "database", "database");
  52.             sql_file.addDefault(db + "timeout", new Integer(10000));
  53.        
  54.             sql_file.options().copyDefaults(true);
  55.             try {
  56.                 sql_file.save(MySQL_FILE);
  57.             } catch(IOException e) {
  58.                 e.printStackTrace();
  59.             }
  60.        
  61.             this.HOST = sql_file.getString(db + "host");
  62.             this.PORT = sql_file.getString(db + "port");
  63.             this.USERNAME = sql_file.getString(db + "username");
  64.             this.PASSWORD = sql_file.getString(db + "password");
  65.             this.DATABASE = sql_file.getString(db + "database");
  66.             this.TIMEOUT = sql_file.getInt(db + "password");
  67.        
  68.             this.openConnection();
  69.             instance = this;
  70.             System.out.println("[MySQL] This utility is created by Gerviba!");
  71.         }
  72.    
  73.         /**
  74.          * Kapcsolat megnyitása
  75.          */
  76.         public Connection openConnection() throws Exception{
  77.             //MySQL URI generálása config alapján
  78.             Class.forName("com.mysql.jdbc.Driver");
  79.             Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://" + this.HOST + ":" + this.PORT + "/" + this.DATABASE+"?autoReconnect=true", this.USERNAME, this.PASSWORD);
  80.             this.conn = conn;
  81.             this.conn.setAutoReconnect(true);
  82.             this.conn.setConnectTimeout(TIMEOUT);
  83.             return null;
  84.         }
  85.    
  86.         /**
  87.          * Kapcsolat getter
  88.          * @return Kapcsolat
  89.          */
  90.         public Connection getConnection() {
  91.             try {
  92.                 if(this.conn == null || !this.conn.isValid(TIMEOUT)) {
  93.                     System.out.println("[MySQL] Reconnecting!");
  94.                     this.openConnection();
  95.                 }
  96.             } catch (Exception e) {
  97.                 e.printStackTrace();
  98.             }
  99.             return this.conn;
  100.         }
  101.    
  102.         /**
  103.          * Kapcsolat checker
  104.          * @return true hogyha van kapcsolat
  105.          */
  106.         public boolean hasConnection() {
  107.             try {
  108.                 return this.conn != null || this.conn.isValid(1);
  109.             } catch(SQLException e) {
  110.                 return false;
  111.             }
  112.            
  113.         }
  114.    
  115.         /**
  116.          * Query kiadása
  117.          * @param query INSERT, DELETE, UPDATE és egyéb query
  118.          */
  119.         public void query(String query) {
  120.             PreparedStatement st = null;
  121.             try {
  122.                 st = (PreparedStatement) this.getConnection().prepareStatement(query);
  123.                 st.executeUpdate();
  124.             } catch(SQLException e) {
  125.                 System.err.println("MySQL Query error! {" + query + "}");
  126.                 e.printStackTrace();
  127.             } finally {
  128.                 this.closeRessources(null, st);
  129.             }
  130.         }
  131.    
  132.         /**
  133.          * Források bezárása
  134.          * @param rs ResultSet
  135.          * @param st PreparedStatement
  136.          */
  137.         public void closeRessources(ResultSet rs, PreparedStatement st) {
  138.             if (rs != null) {
  139.                 try {
  140.                     rs.close();
  141.                 } catch(SQLException e) {}
  142.             }
  143.             if (st != null) {
  144.                 try {
  145.                     st.close();
  146.                 } catch(SQLException e) {}
  147.             }
  148.         }
  149.    
  150.         /**
  151.          * Szétkapcsolás
  152.          */
  153.         public void closeConnection() {
  154.             try {
  155.                 this.conn.close();
  156.             } catch(SQLException e) {
  157.                 e.printStackTrace();
  158.             } finally {}
  159.             this.conn = null;
  160.         }
  161.    
  162.         /**
  163.          * Instance getter
  164.          * @return MySQL
  165.          */
  166.         public static MySQL getInstance() {
  167.             return instance;
  168.         }
  169.    
  170.         /**
  171.          * Tábla ellenőrző
  172.          * @param table Tábla neve
  173.          * @return true, hogyha létezik és false, hogyha nem
  174.          * @throws Exception
  175.          */
  176.         public boolean isTableExists(String table) throws Exception {
  177.             ResultSet rs = null;
  178.             PreparedStatement st = null;
  179.             try {
  180.                 st = (PreparedStatement) MySQL.getInstance().getConnection().prepareStatement("SHOW TABLES LIKE '"+table+"'");
  181.                 rs = st.executeQuery();
  182.                 rs.first();
  183.                 return rs.getRow() != 0;
  184.             } catch(Exception ex) {
  185.                 throw ex;
  186.             } finally {
  187.                 MySQL.getInstance().closeRessources(rs, st);
  188.             }
  189.         }
  190.    
  191.         /**
  192.          * Tábla ellenőrző
  193.          * @param table Tábla neve
  194.          * @return true, hogyha létezik és false, hogyha nem vagy ha hiba történik.
  195.          */
  196.         public boolean isTableExistsSafe(String table) {
  197.             ResultSet rs = null;
  198.             PreparedStatement st = null;
  199.             try {
  200.                 st = (PreparedStatement) MySQL.getInstance().getConnection().prepareStatement("SHOW TABLES LIKE '"+table+"'");
  201.                 rs = st.executeQuery();
  202.                 rs.first();
  203.                 return rs.getRow() != 0;
  204.             } catch(Exception ex) {
  205.                 ex.printStackTrace();
  206.             } finally {
  207.                 MySQL.getInstance().closeRessources(rs, st);
  208.             }
  209.             return false;
  210.         }
  211.    
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement