Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.skin_98.MySQL.DataBase;
- import java.io.File;
- import java.io.IOException;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.PreparedStatement;
- /**
- * MySQL adatbázis util
- */
- public class MySQL {
- //Alap változók
- public String HOST;
- public String PORT;
- public String DATABASE;
- public String USERNAME;
- private String PASSWORD; //<- Rejtett
- public int TIMEOUT;
- //Kapcsolat
- private Connection conn;
- //Instance
- private static MySQL instance = null;
- /**
- * Inicializálás
- * @throws Exception
- */
- public void init() throws Exception {
- if(instance != null) {
- throw new Exception("MySQL has already initialized!");
- }
- File MySQL_FILE = new File("plugins","MySQL.yml");
- FileConfiguration sql_file = YamlConfiguration.loadConfiguration(MySQL_FILE);
- //Konfiguráció alapértelmezett értékei
- String db = "MySQL.";
- sql_file.addDefault(db + "host", "127.0.0.1");
- sql_file.addDefault(db + "port", "3306");
- sql_file.addDefault(db + "username", "user");
- sql_file.addDefault(db + "password", "password");
- sql_file.addDefault(db + "database", "database");
- sql_file.addDefault(db + "timeout", new Integer(10000));
- sql_file.options().copyDefaults(true);
- try {
- sql_file.save(MySQL_FILE);
- } catch(IOException e) {
- e.printStackTrace();
- }
- this.HOST = sql_file.getString(db + "host");
- this.PORT = sql_file.getString(db + "port");
- this.USERNAME = sql_file.getString(db + "username");
- this.PASSWORD = sql_file.getString(db + "password");
- this.DATABASE = sql_file.getString(db + "database");
- this.TIMEOUT = sql_file.getInt(db + "password");
- this.openConnection();
- instance = this;
- System.out.println("[MySQL] This utility is created by Gerviba!");
- }
- /**
- * Kapcsolat megnyitása
- */
- public Connection openConnection() throws Exception{
- //MySQL URI generálása config alapján
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://" + this.HOST + ":" + this.PORT + "/" + this.DATABASE+"?autoReconnect=true", this.USERNAME, this.PASSWORD);
- this.conn = conn;
- this.conn.setAutoReconnect(true);
- this.conn.setConnectTimeout(TIMEOUT);
- return null;
- }
- /**
- * Kapcsolat getter
- * @return Kapcsolat
- */
- public Connection getConnection() {
- try {
- if(this.conn == null || !this.conn.isValid(TIMEOUT)) {
- System.out.println("[MySQL] Reconnecting!");
- this.openConnection();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return this.conn;
- }
- /**
- * Kapcsolat checker
- * @return true hogyha van kapcsolat
- */
- public boolean hasConnection() {
- try {
- return this.conn != null || this.conn.isValid(1);
- } catch(SQLException e) {
- return false;
- }
- }
- /**
- * Query kiadása
- * @param query INSERT, DELETE, UPDATE és egyéb query
- */
- public void query(String query) {
- PreparedStatement st = null;
- try {
- st = (PreparedStatement) this.getConnection().prepareStatement(query);
- st.executeUpdate();
- } catch(SQLException e) {
- System.err.println("MySQL Query error! {" + query + "}");
- e.printStackTrace();
- } finally {
- this.closeRessources(null, st);
- }
- }
- /**
- * Források bezárása
- * @param rs ResultSet
- * @param st PreparedStatement
- */
- public void closeRessources(ResultSet rs, PreparedStatement st) {
- if (rs != null) {
- try {
- rs.close();
- } catch(SQLException e) {}
- }
- if (st != null) {
- try {
- st.close();
- } catch(SQLException e) {}
- }
- }
- /**
- * Szétkapcsolás
- */
- public void closeConnection() {
- try {
- this.conn.close();
- } catch(SQLException e) {
- e.printStackTrace();
- } finally {}
- this.conn = null;
- }
- /**
- * Instance getter
- * @return MySQL
- */
- public static MySQL getInstance() {
- return instance;
- }
- /**
- * Tábla ellenőrző
- * @param table Tábla neve
- * @return true, hogyha létezik és false, hogyha nem
- * @throws Exception
- */
- public boolean isTableExists(String table) throws Exception {
- ResultSet rs = null;
- PreparedStatement st = null;
- try {
- st = (PreparedStatement) MySQL.getInstance().getConnection().prepareStatement("SHOW TABLES LIKE '"+table+"'");
- rs = st.executeQuery();
- rs.first();
- return rs.getRow() != 0;
- } catch(Exception ex) {
- throw ex;
- } finally {
- MySQL.getInstance().closeRessources(rs, st);
- }
- }
- /**
- * Tábla ellenőrző
- * @param table Tábla neve
- * @return true, hogyha létezik és false, hogyha nem vagy ha hiba történik.
- */
- public boolean isTableExistsSafe(String table) {
- ResultSet rs = null;
- PreparedStatement st = null;
- try {
- st = (PreparedStatement) MySQL.getInstance().getConnection().prepareStatement("SHOW TABLES LIKE '"+table+"'");
- rs = st.executeQuery();
- rs.first();
- return rs.getRow() != 0;
- } catch(Exception ex) {
- ex.printStackTrace();
- } finally {
- MySQL.getInstance().closeRessources(rs, st);
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement