Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. package br.com.kingdomcraft.utils.spigot.utils;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. import org.bukkit.event.player.PlayerMoveEvent;
  9.  
  10. public class Mysql {
  11.  
  12. private final String bancodedados;
  13. private final String host;
  14. private final String porta;
  15. private final String senha;
  16. private final String usuario;
  17.  
  18. public Mysql(String db, String host, String port, String pw, String user) {
  19. this.bancodedados = db;
  20. this.host = host;
  21. this.porta = port;
  22. this.senha = pw;
  23. this.usuario = user;
  24. }
  25.  
  26. public synchronized Connection conectar() {
  27.  
  28. try {
  29. return DriverManager.getConnection("jdbc:mysql://" + host + ":" + porta + "/" + bancodedados, usuario, senha);
  30. } catch (SQLException e) {
  31. e.printStackTrace();
  32. return null;
  33. }
  34.  
  35. }
  36.  
  37. public synchronized void execute(String query) {
  38. Connection con = conectar();
  39. try {
  40. con.prepareStatement(query).executeUpdate();
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. } finally {
  44. try {
  45. con.close();
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50.  
  51. }
  52.  
  53. public synchronized ResultSet getQueryResult(String query) {
  54. Connection con = conectar();
  55. try {
  56. return con.prepareStatement(query).executeQuery();
  57. } catch (SQLException e) {
  58. e.printStackTrace();
  59. return null;
  60. }
  61.  
  62. }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement