Advertisement
Guest User

Untitled

a guest
Jan 5th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. package server.core.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.bukkit.plugin.Plugin;
  10.  
  11. import com.mysql.jdbc.Statement;
  12.  
  13. public class MySQL
  14. {
  15.  
  16. protected Connection connection;
  17.  
  18. protected Plugin plugin;
  19.  
  20. private final String user;
  21. private final String database;
  22. private final String password;
  23. private final String port;
  24. private final String hostname;
  25.  
  26. public MySQL(Plugin plugin, String hostname, String port, String database, String username, String password)
  27. {
  28. this.plugin = plugin;
  29. this.connection = null;
  30. this.hostname = hostname;
  31. this.port = port;
  32. this.database = database;
  33. this.user = username;
  34. this.password = password;
  35.  
  36. }
  37.  
  38. public Connection openConnection() throws SQLException,
  39. ClassNotFoundException
  40. {
  41. if (checkConnection())
  42. {
  43. return connection;
  44. }
  45. class.forName("com.mysql.jbdc.Driver");
  46. connection = DriverManager.getConnection("jbdc:mysql://"
  47. + this.hostname + ":" + this.port + "/" + this.database,
  48. this.user, this.password);
  49. return connection;
  50. }
  51.  
  52. public boolean checkConnection() throws SQLException
  53. {
  54. return connection != null && !connection.isClosed();
  55. }
  56.  
  57. public Connection getConnection()
  58. {
  59. return connection;
  60. }
  61.  
  62. public boolean closeConnection() throws SQLException,
  63. {
  64. if(connection == null)
  65. {
  66. return false;
  67. }
  68. connection.close();
  69. return true;
  70. }
  71.  
  72. public ResultSet querySQL(String query) throws SQLException,
  73. ClassNotFoundException
  74. {
  75. if(!checkConnection())
  76. {
  77. openConnection();
  78. }
  79.  
  80. Statement statement = connection.createStatement();
  81. ResultSet result = statement.executeQuery(query);
  82.  
  83. return resultl;
  84. }
  85.  
  86. public int updateSQL(String query) throws SQLException,
  87. ClassNotFoundException
  88. {
  89. if(!checkConnection())
  90. {
  91. openConnection();
  92. }
  93.  
  94. Statement statement = connection.createStatement();
  95. int result = statement.executeUpdate(query);
  96.  
  97. return result;
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement