Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. package de.VealtBeast.main;
  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 java.sql.Statement;
  9. import java.util.UUID;
  10.  
  11. public class MySQL {
  12.  
  13. private static String host = "localhost";
  14. private static String database = "SoundSystem";
  15. private static String user = "SoundSystem";
  16. private static String password = "Gxz2m_19IGyptwsh";
  17. private static String port = "3306";
  18. private static Connection connection;
  19.  
  20. public static void connect(){
  21. try {
  22. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", user, password);
  23. System.out.println("[MySQL] Verbindung wurde erfolgreich aufgebaut");
  24. } catch (Exception e) {
  25. System.out.println("[MySQL] Deine MySQL Verbindung konnte NICHT hergestellt werden");
  26. }
  27. }
  28.  
  29. public static void disconnect() {
  30. if(connection != null) {
  31. try {
  32. connection.close();
  33. } catch (SQLException e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39.  
  40. public static void Update(String query){
  41. try{
  42. Statement statement = connection.createStatement();
  43. statement.executeUpdate(query);
  44. statement.close();
  45. }catch(SQLException e){
  46. e.printStackTrace();
  47. disconnect();
  48. connect();
  49. }
  50. }
  51.  
  52. public static void createUser(String UUID) {
  53. try {
  54. PreparedStatement statement = connection.prepareStatement("INSERT INTO SoundSystem values (?, true)");
  55. statement.setString(1, UUID);
  56.  
  57. statement.execute();
  58. statement.close();
  59. } catch (SQLException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. public static void createTable() {
  65. if(connection != null)
  66. Update("CREATE TABLE IF NOT EXISTS SoundSystem(uuid varchar(100), soundon boolean)");
  67. }
  68.  
  69. public static boolean containsSoundSystem(String UUID) {
  70. try {
  71. PreparedStatement statement = connection.prepareStatement("SELECT * FROM SoundSystem WHERE uuid=?");
  72. statement.setString(1, UUID);
  73.  
  74. ResultSet resultSet = statement.executeQuery();
  75. boolean con = resultSet.next();
  76.  
  77. statement.close();
  78. resultSet.close();
  79.  
  80. return con;
  81. } catch (SQLException e) {
  82. e.printStackTrace();
  83. }
  84. return true;
  85. }
  86.  
  87. public static boolean isSoundOn(String UUID, String type) {
  88.  
  89. if(type.equalsIgnoreCase("INVENTORY")) {
  90. try {
  91. PreparedStatement statement = connection.prepareStatement("SELECT * FROM SoundSystem WHERE uuid=?");
  92. statement.setString(1, UUID);
  93.  
  94. ResultSet resultSet = statement.executeQuery();
  95. resultSet.next();
  96.  
  97. boolean isSoundOn = resultSet.getBoolean("soundon");
  98.  
  99. resultSet.close();
  100. statement.close();
  101.  
  102. return isSoundOn;
  103. } catch (SQLException e) {
  104. e.printStackTrace();
  105. }
  106. return true;
  107. }else {
  108. try {
  109. PreparedStatement statement = connection.prepareStatement("SELECT * FROM SoundSystem WHERE uuid=?");
  110. statement.setString(1, UUID);
  111.  
  112. ResultSet resultSet = statement.executeQuery();
  113. resultSet.next();
  114.  
  115. boolean isSoundOn = resultSet.getBoolean("musicon");
  116.  
  117. resultSet.close();
  118. statement.close();
  119.  
  120. return isSoundOn;
  121. } catch (SQLException e) {
  122. e.printStackTrace();
  123. }
  124. return true;
  125. }
  126. }
  127.  
  128. public static void setSound(String UUID, int i, String type) {
  129.  
  130. if(type.equalsIgnoreCase("INVENTORY")) {
  131. Update("UPDATE SoundSystem SET soundon='" + i + "' WHERE uuid='"+ UUID +"'");
  132. }else if(type.equalsIgnoreCase("MUSIC")) {
  133. Update("UPDATE SoundSystem SET musicon='" + i + "' WHERE uuid='"+ UUID +"'");
  134. }
  135.  
  136. }
  137.  
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement