Advertisement
Guest User

url line 17

a guest
Jul 23rd, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. package titancoreapi.API.MySQL;
  2.  
  3. import titancoreapi.Core.LogLevel;
  4. import titancoreapi.TitanCoreAPI;
  5.  
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10.  
  11. public class SQLManager extends SQLOperations implements Runnable {
  12.  
  13. private TitanCoreAPI main = TitanCoreAPI.getInstance();
  14.  
  15. private Connection connection;
  16.  
  17. private String host = "jdbc:sqlserver://localhost";
  18. private String database;
  19. private String username = "user";
  20. private String password = "pass";
  21.  
  22. public void refreshConnection() {
  23. try {
  24. if ((this.connection == null) || (this.connection.isClosed())) {
  25. initialize(this.database);
  26. }
  27. }
  28. catch (SQLException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32.  
  33. public void closeConnection() {
  34. try {
  35. this.connection.close();
  36. }
  37. catch (SQLException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41.  
  42. public boolean initialize(String database) {
  43. try {
  44. this.database = database;
  45. Class.forName("com.mysql.jdbc.Driver");
  46. this.connection = DriverManager.getConnection(this.host + this.database + "?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", this.username, this.password);
  47. return true;
  48. }
  49. catch (ClassNotFoundException e) {
  50. e.printStackTrace();
  51. return false;
  52. }
  53. catch (SQLException e) {
  54. e.printStackTrace();
  55. return false;
  56. }
  57.  
  58. }
  59.  
  60. /**
  61. * Any query which does not return a ResultSet object. Such as : INSERT,
  62. * UPDATE, CREATE TABLE...
  63. *
  64. * @param query
  65. */
  66. public void standardQuery(String query) throws SQLException {
  67. this.refreshConnection();
  68. super.standardQuery(query, this.connection);
  69. }
  70.  
  71. /**
  72. * Check whether a field/entry exists in a database.
  73. *
  74. * @param query
  75. * @return Whether or not a result has been found in the query.
  76. * @throws java.sql.SQLException
  77. */
  78. public boolean existanceQuery(String query) throws SQLException {
  79. this.refreshConnection();
  80. return super.sqlQuery(query, this.connection).next();
  81. }
  82.  
  83. /**
  84. * Any query which returns a ResultSet object. Such as : SELECT Remember to
  85. * close the ResultSet object after you are done with it to free up
  86. * resources immediately. ----- ResultSet set = sqlQuery(
  87. * "SELECT * FROM sometable;"); set.doSomething(); set.close(); -----
  88. *
  89. * @param query
  90. * @return ResultSet
  91. */
  92. public ResultSet sqlQuery(String query) throws SQLException {
  93. this.refreshConnection();
  94. return super.sqlQuery(query, this.connection);
  95. }
  96.  
  97. /**
  98. * Check whether the table name exists.
  99. *
  100. * @param table
  101. * @return
  102. */
  103. public boolean doesTableExist(String table) throws SQLException {
  104. this.refreshConnection();
  105. return super.checkTable(table, this.connection);
  106. }
  107.  
  108. @Override
  109. public void run() {
  110. main.getServer().getScheduler().runTaskAsynchronously(main, new Runnable() {
  111.  
  112. @Override
  113. public void run() {
  114. try {
  115. existanceQuery("SELECT * FROM OPS LIMIT 1, 0;");
  116. }
  117. catch (SQLException e) {
  118. main.getUtilLogger().logLevel(LogLevel.WARNING, "SQLManager> Failed to keep connection!");
  119. e.printStackTrace();
  120. }
  121. }
  122. });
  123. }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement