Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. package fr.lifecraft.sql;
  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.  
  10. import fr.lifecraft.HungerGames;
  11.  
  12. public class MySQL {
  13.  
  14. private Database db;
  15. private Connection connection;
  16. private boolean readOnly;
  17.  
  18. public MySQL(Database db) throws SQLException, NullPointerException {
  19. this.db = db;
  20. try {
  21. this.connection = DriverManager.getConnection("jdbc:mysql://" + db.getHost() + ":" + db.getPort() + "/"
  22. + db.getDatabase() + "?user=" + db.getUser() + "&password=" + db.getPassword() + "&autoReconnect="
  23. + db.isAutoReconnect());
  24. } catch (SQLException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28.  
  29. public Database getDatabase() {
  30. return db;
  31. }
  32.  
  33. public void setDatabase(Database db) {
  34. this.db = db;
  35. }
  36.  
  37. public Connection getConnection() {
  38. return connection;
  39. }
  40.  
  41. public void setConnection(Connection connection) {
  42. this.connection = connection;
  43. }
  44.  
  45. public ResultSet query(String query) {
  46. ResultSet rs = null;
  47.  
  48. try {
  49. Statement state = this.connection.createStatement();
  50. rs = state.executeQuery(query);
  51. } catch (SQLException e) {
  52. e.printStackTrace();
  53. }
  54.  
  55. return rs;
  56. }
  57.  
  58. public void update(String query) {
  59. if (this.readOnly) {
  60. throw new RuntimeException("Failed lol (Read Only)");
  61. }
  62.  
  63. try {
  64. Statement state = this.connection.createStatement();
  65. state.executeQuery(query);
  66. } catch (SQLException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70.  
  71. public void updatePrepared(String query) {
  72. if (this.readOnly) {
  73. throw new RuntimeException("Failed lol (Read Only)");
  74. }
  75.  
  76. try {
  77. PreparedStatement state = this.connection.prepareStatement(query);
  78. state.executeQuery();
  79. } catch (SQLException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83.  
  84. public void clearWarnings() {
  85. try {
  86. this.connection.clearWarnings();
  87. } catch (SQLException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91.  
  92. public void deleteDatabase(Database db) {
  93. this.db = null;
  94. this.connection = null;
  95. }
  96.  
  97. public void setReadOnly() {
  98. try {
  99. this.connection.setReadOnly(true);
  100. } catch (SQLException e) {
  101. e.printStackTrace();
  102. }
  103. }
  104.  
  105. public void close() {
  106. try {
  107. this.connection.close();
  108. } catch (SQLException e) {
  109. e.printStackTrace();
  110. }
  111. }
  112.  
  113. public void createTable() {
  114. try {
  115. final PreparedStatement sql = HungerGames.getInstance().getSQL().getConnection()
  116. .prepareStatement("CREATE TABLE IF NOT EXISTS "
  117. + HungerGames.getInstance().getConfig().getString("mysql.table")
  118. + " (id INT(11) NOT NULL AUTO_INCREMENT,"
  119. + "name VARCHAR(255) NOT NULL,"
  120. + "kills INT(11) NOT NULL,"
  121. + "games INT(11) NOT NULL,"
  122. + "wins INT(11) NOT NULL,"
  123. + "PRIMARY KEY (id));");
  124. sql.executeUpdate();
  125. sql.close();
  126. } catch (Exception e) {
  127. e.printStackTrace();
  128. }
  129. }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement