Advertisement
Guest User

MySQL Mannager

a guest
Jan 14th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. package com.katho.herolandshg.Mysql;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10.  
  11. import org.bukkit.configuration.file.FileConfiguration;
  12. import org.bukkit.configuration.file.YamlConfiguration;
  13. public class MySQL {
  14.  
  15. private String ip;
  16. private int porta;
  17. private String usuario;
  18. private String senha;
  19. private String banco;
  20.  
  21. private Connection conn;
  22.  
  23. public MySQL() throws Exception {
  24. File file = new File("plugins/cHardcoreGames/", "banco.yml");
  25. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  26.  
  27. String db = "bancodedados.";
  28. cfg.addDefault(db + "ip", "talalal.tk");
  29. cfg.addDefault(db + "porta", 3306);
  30. cfg.addDefault(db + "usuario", "talala");
  31. cfg.addDefault(db + "senha", "talala");
  32. cfg.addDefault(db + "banco", "talala");
  33. cfg.options().copyDefaults(true);
  34. try {
  35. cfg.save(file);
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39.  
  40. this.ip = cfg.getString(db + "ip");
  41. this.porta = cfg.getInt(db + "porta");
  42. this.usuario = cfg.getString(db + "usuario");
  43. this.senha = cfg.getString(db + "senha");
  44. this.banco = cfg.getString(db + "banco");
  45.  
  46. this.openConnection();
  47. }
  48.  
  49.  
  50. public Connection openConnection() throws Exception {
  51. Class.forName("com.mysql.jdbc.Driver");
  52. Connection conn = DriverManager.getConnection("jdbc:mysql://" + this.ip + ":" + this.porta + "/" + this.banco, this.usuario, this.senha);
  53. this.conn = conn;
  54. return conn;
  55. }
  56.  
  57. public Connection getConnection() {
  58. return this.conn;
  59. }
  60.  
  61. public boolean hasConnection() {
  62. try {
  63. return this.conn != null || this.conn.isValid(1);
  64. } catch (SQLException e) {
  65. return false;
  66. }
  67. }
  68.  
  69. public void queryUpdate(String query) {
  70. Connection conn = this.conn;
  71. PreparedStatement st = null;
  72. try {
  73. st = conn.prepareStatement(query);
  74. st.executeUpdate();
  75. } catch (SQLException e) {
  76. System.err.println("Failed to send update '" + query + "'.");
  77. } finally {
  78. this.closeRessources(null, st);
  79. }
  80. }
  81.  
  82. public void refreshConnection() {
  83.  
  84. PreparedStatement st = null;
  85. ResultSet valid = null;
  86. try
  87. {
  88. st = conn.prepareStatement("SELECT 1 FROM Dual");
  89. valid = st.executeQuery();
  90. if (valid.next())
  91. return;
  92. } catch (SQLException e2)
  93. {
  94. System.out.println("Connection is idle or terminated. Reconnecting...");
  95. } finally
  96. {
  97. this.closeRessources(valid, st);
  98. }
  99.  
  100. long start = 0;
  101. long end = 0;
  102.  
  103. try
  104. {
  105. start = System.currentTimeMillis();
  106. System.out.println("Attempting to establish a connection the MySQL server!");
  107. Class.forName("com.mysql.jdbc.Driver");
  108. conn = DriverManager.getConnection("jdbc:mysql://" + this.ip + ":" + this.porta + "/" + this.banco, this.usuario, this.senha);
  109. end = System.currentTimeMillis();
  110. System.out.println("Conexao com mysql estabilizado!");
  111. System.out.println(((end - start)) + "ms!");
  112. } catch (SQLException e)
  113. {
  114. System.out.println("Could not connect to MySQL server! because: " + e.getMessage());
  115. } catch (ClassNotFoundException e)
  116. {
  117. System.out.println("JDBC Driver not found!");
  118. }
  119. }
  120.  
  121. public void closeRessources(ResultSet rs, PreparedStatement st) {
  122. if (rs != null) {
  123. try {
  124. rs.close();
  125. } catch (SQLException e) {
  126. }
  127. }
  128. if (st != null) {
  129. try {
  130. st.close();
  131. } catch (SQLException e) {
  132. }
  133. }
  134. }
  135.  
  136. public void closeConnection() {
  137. try {
  138. this.conn.close();
  139. } catch (SQLException e) {
  140. e.printStackTrace();
  141. } finally {
  142. this.conn = null;
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement