Advertisement
Guest User

Untitled

a guest
Aug 14th, 2016
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. package de.LobbySystem.Utils;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6.  
  7. import org.bukkit.Bukkit;
  8.  
  9. import com.mysql.jdbc.Connection;
  10. import com.mysql.jdbc.PreparedStatement;
  11.  
  12. import de.LobbySystem.Main.Main;
  13.  
  14.  
  15. public class MySQL {
  16.  
  17. public static String host = "";
  18. public static String port = "3306";
  19. public static String database = "";
  20. public static String username = "";
  21. public static String password = "";
  22.  
  23. public static Connection con;
  24.  
  25. public static boolean isConnected(){
  26. return con != null;
  27. }
  28.  
  29. public static void connect(){
  30. if(!isConnected()){
  31. try {
  32. con = (Connection) DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
  33. Bukkit.getConsoleSender().sendMessage(Main.Coinsprefix + "§aEs konnte erfolgreich mit der Datenbank verbunden werden");
  34. } catch (SQLException e) {
  35. Bukkit.getConsoleSender().sendMessage(Main.Coinsprefix + "§cEs konnte nicht mit der Datenbank verbunden werden");
  36. }
  37. }
  38. }
  39.  
  40. public static void disconnect(){
  41. try {
  42. con.close();
  43. Bukkit.getConsoleSender().sendMessage(Main.Coinsprefix + "§aDie Verbindung zur Datenbank konnte erfolgreich geschlossen werden");
  44. } catch (SQLException e) {
  45. Bukkit.getConsoleSender().sendMessage(Main.Coinsprefix + "§cDie Verbindung zur Datenbank konnte nicht geschlossen werden");
  46. }
  47. }
  48.  
  49. public static PreparedStatement getStatement(String sql){
  50. if(isConnected()){
  51. PreparedStatement ps;
  52. try {
  53. ps = (PreparedStatement) con.prepareStatement(sql);
  54. return ps;
  55. } catch (SQLException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. return null;
  60. }
  61.  
  62. public static ResultSet getResult(String sql){
  63. if(isConnected()){
  64. PreparedStatement ps;
  65. ResultSet rs;
  66. try {
  67. ps = getStatement(sql);
  68. rs = ps.executeQuery();
  69. return rs;
  70. } catch (SQLException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. return null;
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement