Guest User

Untitled

a guest
Dec 24th, 2017
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. package org.dementhium.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. import org.dementhium.RS2ServerBootstrap;
  10. import org.dementhium.util.Misc;
  11.  
  12. import com.mysql.jdbc.Driver;
  13.  
  14. /**
  15. *
  16. * @author 'Mystic Flow <Steven@rune-server.org>
  17. */
  18. public class DatabaseManager {
  19.  
  20. private static Driver driver;
  21.  
  22. public static DatabaseManager create(String host, String database, String username, String password) throws SQLException {
  23. if (driver == null) {
  24. driver = new Driver();
  25. }
  26. return new DatabaseManager(host, database, username, password);
  27. }
  28.  
  29. private String host;
  30. private String database;
  31. private String username;
  32. private String password;
  33.  
  34. private Connection connection;
  35. private Statement statement;
  36.  
  37. private DatabaseManager(String host, String database, String username, String password) {
  38. this.host = host;
  39. this.database = database;
  40. this.username = username;
  41. this.password = password;
  42. }
  43.  
  44. public void establishConnection() {
  45. try {
  46. connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database + "?jdbcCompliantTruncation=false", username, password);
  47. statement = connection.createStatement();
  48. } catch (Exception e) {
  49. if (Misc.isWindows()) { //So it's only for Emperor.
  50. RS2ServerBootstrap.sqlDisabled = true;
  51. }
  52. e.printStackTrace();
  53. }
  54. }
  55.  
  56. public ResultSet executeQuery(String query) throws SQLException {
  57. boolean closed = statement.getConnection().isClosed();
  58. //System.out.println("Con closed? "+closed);
  59. if (closed) {
  60. statement = statement.getConnection().createStatement();
  61. }
  62. ResultSet results = statement.executeQuery(query);
  63. return results;
  64. }//go :p
  65.  
  66. public void executeUpdate(String query) throws SQLException {
  67. if(this.statement == null) {
  68. statement = statement.getConnection().createStatement();
  69. }
  70. if(statement.getConnection().isClosed()) {
  71. statement = statement.getConnection().createStatement();
  72. }
  73. statement.executeUpdate(query);
  74. }
  75.  
  76. }
Add Comment
Please, Sign In to add comment