Advertisement
MCMarter

Untitled

Oct 9th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. package CustomTokensMySQL
  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. import org.bukkit.Bukkit;
  9. import org.bukkit.command.ConsoleCommandSender;
  10. import org.bukkit.configuration.file.FileConfiguration;
  11.  
  12. public class MySQL
  13. {
  14. static Main plugin;
  15.  
  16. public MySQL(Main main)
  17. {
  18. plugin = main;
  19. }
  20.  
  21. public String host = "";
  22. public String port = "";
  23. public String database = "";
  24. public String username = "";
  25. public String password = "";
  26. public Connection con;
  27.  
  28. public void connect()
  29. {
  30. this.host = plugin.getConfig().getString("CustomTokens.MySQL.Host");
  31. this.port = plugin.getConfig().getString("CustomTokens.MySQL.Port");
  32. this.database = plugin.getConfig().getString("CustomTokens.MySQL.Database");
  33. this.username = plugin.getConfig().getString("CustomTokens.MySQL.Username");
  34. this.password = plugin.getConfig().getString("CustomTokens.MySQL.Password");
  35. if (!connected()) {
  36. try
  37. {
  38. this.con = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password);
  39. Bukkit.getConsoleSender().sendMessage("[CustomTokens] MySQL - Connection constructed!");
  40. }
  41. catch (SQLException e)
  42. {
  43. e.printStackTrace();
  44. Bukkit.getConsoleSender().sendMessage("[CustomTokens] MySQL - Connection failed!");
  45. }
  46. }
  47. }
  48.  
  49. public void disconnect()
  50. {
  51. if (connected()) {
  52. try
  53. {
  54. this.con.close();
  55. Bukkit.getConsoleSender().sendMessage("[CustomTokens] MySQL - Connection closed!");
  56. }
  57. catch (SQLException e)
  58. {
  59. e.printStackTrace();
  60. }
  61. }
  62. }
  63.  
  64. public boolean connected()
  65. {
  66. if (this.con == null) {
  67. return false;
  68. }
  69. return true;
  70. }
  71.  
  72. public void executeupdate(String qry)
  73. {
  74. try
  75. {
  76. PreparedStatement ps = this.con.prepareStatement(qry);
  77. ps.executeUpdate();
  78. }
  79. catch (SQLException e)
  80. {
  81. e.printStackTrace();
  82. }
  83. }
  84.  
  85. public ResultSet gerResult(String qry)
  86. {
  87. try
  88. {
  89. PreparedStatement ps = this.con.prepareStatement(qry);
  90. return ps.executeQuery();
  91. }
  92. catch (SQLException e)
  93. {
  94. e.printStackTrace();
  95. }
  96. return null;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement