Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. public class Database extends Source{
  2. private Connection connection;
  3. private Statement statement;
  4. private String host, database, username, password;
  5. private int port;
  6.  
  7. public Database(String host, String database, String username,
  8. String password, int port) {
  9. super(SourceType.Database);
  10. this.setHost(host);
  11. this.setDatabase(database);
  12. this.setUsername(username);
  13. this.setPassword(password);
  14. this.setPort(port);
  15. try {
  16. connectToDatabase();
  17. }catch(Exception e) {
  18. Bukkit.broadcastMessage("§cConnection to " + host + ":" + port + " failed");
  19. Bukkit.broadcastMessage("§c" + e.getMessage());
  20. return;
  21. }
  22. }
  23.  
  24. private void connectToDatabase() throws SQLException,
  25. ClassNotFoundException {
  26. if (connection != null && !connection.isClosed()) {
  27. return;
  28. }
  29.  
  30. synchronized (this) {
  31. if (connection != null && !connection.isClosed()) {
  32. return;
  33. }
  34. Class.forName("com.mysql.jdbc.Driver");
  35. connection = DriverManager.getConnection("jdbc:mysql://"
  36. + this.host + ":" + this.port + "/" + this.database,
  37. this.username, this.password);
  38. setStatement(connection.createStatement());
  39. }
  40. }
  41.  
  42.  
  43. @Override
  44. public void cut(SeaInfos s) {
  45. // TODO Auto-generated method stub
  46.  
  47. }
  48.  
  49. @Override
  50. public void writeConfig(String way, Object o) {
  51. // TODO Auto-generated method stub
  52.  
  53. }
  54.  
  55. @Override
  56. public void writeDatabase(String table, String field, String o) {
  57. // TODO Auto-generated method stub
  58.  
  59. }
  60.  
  61.  
  62. @Override
  63. public Object readConfig(String path) {
  64. return null;
  65. }
  66.  
  67. @Override
  68. public Object readDatabase(String table, String fieldName,
  69. String condition, String pass) {
  70. return getObjects(fieldName, table, condition, pass);
  71. }
  72.  
  73. @SuppressWarnings("null")
  74. public List<Object> getObjects(String fieldName, String table, String condition, String pass) {
  75. try {
  76. ResultSet result = statement.executeQuery("SELECT "+fieldName+" FROM " + table + " WHERE " + condition + " = " + pass + ";");
  77. List<Object> objects = null;
  78. while(result.next()) {
  79. objects.add(result.getObject(fieldName));
  80. }
  81. return objects;
  82. } catch (SQLException e) {
  83. e.printStackTrace();
  84. }
  85. return null;
  86. }
  87.  
  88.  
  89. public void setObject(String table, String[] keys, String[] values, Object c) {
  90. try {
  91. statement.executeQuery("INSERT INTO " + table + "(" + keys +") VALUES ("+values+");");
  92. } catch (SQLException e) {
  93. e.printStackTrace();
  94. }
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. public Connection getConnection() {
  108. return connection;
  109. }
  110.  
  111. public void setConnection(Connection connection) {
  112. this.connection = connection;
  113. }
  114.  
  115. public String getHost() {
  116. return host;
  117. }
  118.  
  119. public void setHost(String host) {
  120. this.host = host;
  121. }
  122.  
  123. public String getDatabase() {
  124. return database;
  125. }
  126.  
  127. public void setDatabase(String database) {
  128. this.database = database;
  129. }
  130.  
  131. public String getUsername() {
  132. return username;
  133. }
  134.  
  135. public void setUsername(String username) {
  136. this.username = username;
  137. }
  138.  
  139. public String getPassword() {
  140. return password;
  141. }
  142.  
  143. public void setPassword(String password) {
  144. this.password = password;
  145. }
  146.  
  147. public int getPort() {
  148. return port;
  149. }
  150.  
  151. public void setPort(int port) {
  152. this.port = port;
  153. }
  154.  
  155. public Statement getStatement() {
  156. return statement;
  157. }
  158.  
  159. public void setStatement(Statement statement) {
  160. this.statement = statement;
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement