Advertisement
Guest User

MySQL

a guest
Nov 11th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. package me.schwitzer.stats;
  2.  
  3. import me.schwitzer.utils.Data;
  4. import org.bukkit.Bukkit;
  5.  
  6. import java.sql.*;
  7.  
  8. public class MySQL {
  9.  
  10.     private String host = "";
  11.     private String username = "";
  12.     private String password = "";
  13.     private String database = "";
  14.     private int port;
  15.     private static Connection connection;
  16.  
  17.  
  18.     public MySQL(String host, String username, String password, String database, int port) {
  19.         this.host = host;
  20.         this.username = username;
  21.         this.password = password;
  22.         this.database = database;
  23.         this.port = port;
  24.         this.connect();
  25.     }
  26.  
  27.     public void connect() {
  28.         if(!this.isConnected()) {
  29.             try {
  30.                 connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true", this.username, this.password);
  31.                 Bukkit.getConsoleSender().sendMessage(Data.pr + "§aVerbindung erfolgreich aufgebaut!");
  32.             } catch (SQLException var2) {
  33.                 Bukkit.getConsoleSender().sendMessage(Data.pr + "§cVerbindung erfolgreich geschlossen! Reason: NO CONNECTION!");
  34.             }
  35.         }
  36.  
  37.     }
  38.  
  39.     public void disconnect() {
  40.         if(this.isConnected()) {
  41.             try {
  42.                 connection.close();
  43.                 Bukkit.getConsoleSender().sendMessage(Data.pr + "§cVerbindung erfolgreich geschlossen!");
  44.             } catch (SQLException var2) {
  45.                 ;
  46.             }
  47.         }
  48.  
  49.     }
  50.  
  51.     public boolean isConnected() {
  52.         return connection != null;
  53.     }
  54.  
  55.     public Connection getConnection() {
  56.         return connection;
  57.     }
  58.  
  59.     public void update(String qry) {
  60.         try {
  61.             PreparedStatement ps = connection.prepareStatement(qry);
  62.             ps.executeUpdate();
  63.             ps.close();
  64.         } catch (SQLException var3) {
  65.             ;
  66.         }
  67.  
  68.     }
  69.  
  70.     public ResultSet getResult(String qry) {
  71.         try {
  72.             PreparedStatement var3 = connection.prepareStatement(qry);
  73.             return var3.executeQuery();
  74.         } catch (SQLException var31) {
  75.             return null;
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement