Advertisement
Guest User

Untitled

a guest
Apr 10th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. package ch.batthomas.ttt.database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. import org.bukkit.entity.Player;
  10.  
  11. public class MySQL {
  12.  
  13. public static String username = "";
  14. public static String password = "";
  15. public static String database = "";
  16. public static String host = "";
  17. public static int port = 3306;
  18. public static Connection con;
  19.  
  20. public static void connect() {
  21. if (!MySQL.isConnected()) {
  22. try {
  23. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  24. } catch (SQLException ex) {
  25. Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  26. }
  27.  
  28. }
  29. }
  30.  
  31. public static void close() {
  32. if (MySQL.isConnected()) {
  33. try {
  34. con.close();
  35. } catch (SQLException ex) {
  36. Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  37. }
  38. }
  39. }
  40.  
  41. public static boolean isConnected() {
  42. return con != null;
  43. }
  44.  
  45. public static void createTable() {
  46. if (MySQL.isConnected()) {
  47. try {
  48. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS Stats (PlayerUUID TEXT,Karma TEXT,Kills TEXT,Deaths TEXT,Pass TEXT,Tested TEXT,WinnedGames TEXT,PlayedGames TEXT,RID int(11) NOT NULL auto_increment,primary KEY (RID));");
  49. } catch (SQLException ex) {
  50. Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  51. }
  52. }
  53. }
  54.  
  55. public static void update(String qry) {
  56. try {
  57. con.createStatement().executeUpdate(qry);
  58. } catch (SQLException ex) {
  59. Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  60. }
  61. }
  62.  
  63. public static ResultSet getResult(String qry) {
  64. if (MySQL.isConnected()) {
  65. try {
  66. return con.createStatement().executeQuery(qry);
  67. } catch (SQLException ex) {
  68. Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  69. }
  70. }
  71. return null;
  72. }
  73.  
  74. public static boolean isInDatabase(Player player) {
  75. try {
  76. ResultSet rs = MySQL.getResult("SELECT * FROM Stats WHERE PlayerUUID = '" + player.getUniqueId() + "'");
  77. return rs.next();
  78. } catch (Exception ex) {
  79. Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  80. return false;
  81. }
  82. }
  83.  
  84. public static void addPlayerToDatabase(Player player) {
  85. try {
  86. MySQL.update("INSERT INTO Stats (PlayerUUID,Karma,Kills,Deaths,Pass,Tested,WinnedGames,PlayedGames) VALUES ('" + player.getUniqueId().toString() + "', '0', '0', '0', '0', '0', '0', '0')");
  87. } catch (Exception ex) {
  88. Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  89. }
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement