Guest User

Untitled

a guest
Jul 14th, 2016
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. package pl.theminecraft.core.storage;
  2.  
  3. import java.io.PrintStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. public class MySQLDatabase {
  11. private String hostname = "localhost";
  12. private String username = "minecraft";
  13. private String portnum = "3306";
  14. private String dbname = "minecraft";
  15. private String password = "";
  16. private Connection conn;
  17.  
  18. private MySQLDatabase(String host, String user, String port, String password, String db) {
  19. this.hostname = host;
  20. this.username = user;
  21. this.portnum = port;
  22. this.dbname = db;
  23. this.password = password;
  24. }
  25.  
  26. public static final MySQLDatabase connect(String host, String user, String port, String password, String db) {
  27. return new MySQLDatabase(host, user, port, password, db);
  28. }
  29.  
  30. boolean initialize() {
  31. try {
  32. Class.forName("com.mysql.jdbc.Driver");
  33. return true;
  34. }
  35. catch (ClassNotFoundException e) {
  36. System.out.println("Could not load JDBC driver " + e.getMessage());
  37. return false;
  38. }
  39. }
  40.  
  41. public Connection open() {
  42. if (this.initialize()) {
  43. String url = "";
  44. try {
  45. url = "jdbc:mysql://" + this.hostname + ":" + this.portnum + "/" + this.dbname;
  46. this.conn = DriverManager.getConnection(url, this.username, this.password);
  47. }
  48. catch (SQLException e) {
  49. System.out.println("Could not connect to database" + e.getMessage());
  50. }
  51. }
  52. return null;
  53. }
  54.  
  55. public void close() {
  56. try {
  57. if (this.conn != null) {
  58. this.conn.close();
  59. }
  60. }
  61. catch (Exception e) {
  62. System.out.println("Could not close connection to database" + e.getMessage());
  63. }
  64. }
  65.  
  66. public Connection getConnection() {
  67. if (this.conn == null) {
  68. return this.open();
  69. }
  70. return this.conn;
  71. }
  72.  
  73. public boolean checkConnection() {
  74. return this.conn != null;
  75. }
  76.  
  77. public ResultSet query(String query) {
  78. Statement statement = null;
  79. ResultSet result = null;
  80. try {
  81. if (!this.checkConnection()) {
  82. this.open();
  83. }
  84. statement = this.conn.createStatement();
  85. result = statement.executeQuery("SELECT CURTIME()");
  86. if (query.contains("SELECT")) {
  87. result = statement.executeQuery(query);
  88. } else {
  89. statement.executeUpdate(query);
  90. }
  91. return result;
  92. }
  93. catch (SQLException e) {
  94. System.out.println("Error in SQL query: " + e.getMessage());
  95. return result;
  96. }
  97. }
  98.  
  99. public boolean createTable(String query) {
  100. Statement statement = null;
  101. try {
  102. if (query.equals("") || query == null) {
  103. System.out.println("SQL query empty: createTable(" + query + ")");
  104. return false;
  105. }
  106. statement = this.conn.createStatement();
  107. statement.execute(query);
  108. return true;
  109. }
  110. catch (SQLException e) {
  111. System.out.println(e.getMessage());
  112. return false;
  113. }
  114. catch (Exception e) {
  115. System.out.println(e.getMessage());
  116. return false;
  117. }
  118. }
  119.  
  120. public boolean checkTable(String table) {
  121. try {
  122. Statement statement = this.conn.createStatement();
  123. ResultSet result = statement.executeQuery("SELECT * FROM " + table);
  124. if (result == null) {
  125. return false;
  126. }
  127. if (result != null) {
  128. return true;
  129. }
  130. }
  131. catch (SQLException e) {
  132. if (e.getMessage().contains("exist")) {
  133. return false;
  134. }
  135. System.out.println("Error in SQL query: " + e.getMessage());
  136. }
  137. return this.query("SELECT * FROM " + table) == null;
  138. }
  139. }
Add Comment
Please, Sign In to add comment