Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. package br.alkazuz.kitpvp.mysql.connection;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. import org.bukkit.Bukkit;
  10.  
  11. import br.alkazuz.kitpvp.config.Config;
  12.  
  13. public class kMySQL
  14. {
  15. public static String ip = Config.getConfig().getString("MySQL.host");
  16. public static String porta = Config.getConfig().getString("MySQL.Port");
  17. public static String database = Config.getConfig().getString("MySQL.Database");
  18. public static String usuario = Config.getConfig().getString("MySQL.User");
  19. public static String senha = Config.getConfig().getString("MySQL.Password");
  20. public static Connection con;
  21.  
  22. public static boolean jáConectado()
  23. {
  24. return con != null;
  25. }
  26.  
  27. public static void conectar()
  28. {
  29. if (!jáConectado()) {
  30. try
  31. {
  32. con = DriverManager.getConnection("jdbc:mysql://" + ip + ":" + porta + "/" + database + "?autoReconnect=true", usuario, senha);
  33. Bukkit.getConsoleSender().sendMessage("§7A conexão com o §dMySQL§7 foi aceita.");
  34. Bukkit.getConsoleSender().sendMessage("§7O Servidor está conectado ao mysql.");
  35. }
  36. catch (SQLException e)
  37. {
  38. Bukkit.getConsoleSender().sendMessage("§7A conexão com o §dMySQL§7 foi negada.");
  39. }
  40. }
  41. }
  42.  
  43. public static void Desconectar()
  44. {
  45. try
  46. {
  47. con.close();
  48. System.out.print("§cDesligar conexão com o MySQL: §c§lSucesso!");
  49. }
  50. catch (SQLException e)
  51. {
  52. System.out.print("§cDesligar conexão com o MySQL: §c§lProblema!");
  53. }
  54. }
  55.  
  56. public static PreparedStatement getStatement(String sql)
  57. {
  58. if (jáConectado()) {
  59. try
  60. {
  61. return con.prepareStatement(sql);
  62. }
  63. catch (SQLException e)
  64. {
  65. e.printStackTrace();
  66. }
  67. }
  68. return null;
  69. }
  70.  
  71. public static ResultSet getResult(String sql)
  72. {
  73. if (jáConectado()) {
  74. try
  75. {
  76. PreparedStatement ps = getStatement(sql);
  77. return ps.executeQuery();
  78. }
  79. catch (SQLException e)
  80. {
  81. e.printStackTrace();
  82. }
  83. }
  84. return null;
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement