Advertisement
Cynfos

Spigot 1.9+ MySQL API

Apr 16th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. package de.cynfos.mysqlapi;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10.  
  11. import org.bukkit.configuration.file.FileConfiguration;
  12.  
  13. public class MySQL {
  14.    
  15.     private int port;
  16.     private String host, user, password, database;
  17.    
  18.     private Connection conn;
  19.    
  20.     public MySQL() {
  21.         File file = new File("plugins/MySQL-API/", "config.yml");
  22.         FileConfiguration cfg = this.plugin.getConfig();
  23.        
  24.         String db = "database.";
  25.        
  26.         cfg.addDefault(db + "host", "localhost");
  27.         cfg.addDefault(db + "port", 3306);
  28.         cfg.addDefault(db + "user", "root");
  29.         cfg.addDefault(db + "password", "password");
  30.         cfg.addDefault(db + "database", "database");
  31.        
  32.         cfg.options().copyDefaults(true);
  33.        
  34.         try {
  35.             cfg.save(file);
  36.         } catch (IOException e) {
  37.             e.printStackTrace();
  38.         }
  39.        
  40.         this.host = cfg.getString(db + "host");
  41.         this.port = cfg.getInt(db + "port");
  42.         this.user = cfg.getString(db + "user");
  43.         this.password = cfg.getString(db + "password");
  44.         this.database = cfg.getString(db + "database");
  45.        
  46.         this.openConnection();
  47.     }
  48.    
  49.     private Connection openConnection() {
  50.         try {
  51.             Class.forName("com.mysql.jdbc.Driver");
  52.             Connection conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.user, this.password);
  53.             this.conn = conn;
  54.            
  55.             return conn;
  56.         } catch (SQLException | ClassNotFoundException e) {
  57.             e.printStackTrace();
  58.         }
  59.        
  60.         return null;
  61.     }
  62.    
  63.     public Connection getConnection() {
  64.         return this.conn;
  65.     }
  66.    
  67.     public boolean hasConnection() {
  68.         try {
  69.             return this.conn != null || this.conn.isValid(1);
  70.         } catch (SQLException e) {
  71.             return false;
  72.         }
  73.     }
  74.    
  75.     public void queryUpdate(String query) {
  76.         Connection conn = this.conn;
  77.         PreparedStatement st = null;
  78.        
  79.         try {
  80.             st = conn.prepareStatement(query);
  81.             st.executeUpdate();
  82.         } catch (SQLException e) {
  83.             System.err.println("[MySQL] Failed to send update '" + query + "' (" + e.getMessage() + ")");
  84.         } finally {
  85.             this.closeRessources(null, st);
  86.         }
  87.     }
  88.    
  89.     public void closeRessources(ResultSet rs, PreparedStatement st) {
  90.         if (rs != null) {
  91.             try {
  92.                 rs.close();
  93.             } catch (SQLException e) {
  94.             }
  95.         }
  96.        
  97.         if (st != null) {
  98.             try {
  99.                 st.close();
  100.             } catch (SQLException e) { 
  101.             }
  102.            
  103.         }
  104.     }
  105.    
  106.     public void closeConnection() {
  107.         try {
  108.             this.conn.close();
  109.         } catch (SQLException e) {
  110.             e.printStackTrace();
  111.         } finally {
  112.             this.conn = null;
  113.         }
  114.     }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement