Guest User

Untitled

a guest
Oct 20th, 2016
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. package leitung.mysqlapi.elkman;
  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.  
  14.  
  15. public class MySQL {
  16. private String host;
  17. private int port;
  18. private String user;
  19. private String password;
  20. private String database;
  21.  
  22. private Connection conn;
  23.  
  24. public MySQL() throws Exception{
  25.  
  26. File file = new File("plugins/MySQLAPI" , "database.yml");
  27. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  28.  
  29. String db = "database.";
  30.  
  31. cfg.addDefault(db + "host", "localhost");
  32. cfg.addDefault(db + "port", 3306);
  33. cfg.addDefault(db + "user", "user");
  34. cfg.addDefault(db + "password", "password");
  35. cfg.addDefault(db + "database", "database");
  36.  
  37. cfg.options().copyDefaults(true);
  38. try {
  39. cfg.save(file);
  40. } catch (IOException e) {
  41. }
  42.  
  43. this.host=cfg.getString(db + "host");
  44. this.port=cfg.getInt(db + "port");
  45. this.user=cfg.getString(db + "user");
  46. this.password=cfg.getString(db + "password");
  47. this.database=cfg.getString(db + "database");
  48.  
  49. this.openConnection();
  50.  
  51. }
  52.  
  53. public Connection openConnection() throws Exception{
  54.  
  55. Class.forName("com.mysql.jdbc.Driver");
  56. Connection conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?user=" + this.user +"&password=" + this.password + "&autoReconnect=true");
  57. this.conn = conn;
  58. return this.conn;
  59. }
  60.  
  61. public Connection getConnection(){
  62. return this.conn;
  63. }
  64.  
  65. public boolean hasConnection(){
  66. try {
  67. return this.conn != null || this.conn.isValid(1);
  68. } catch (SQLException e) {
  69. return false;
  70. }
  71. }
  72.  
  73. public void queryUpdate(String query){
  74. Connection conn = this.conn;
  75. java.sql.PreparedStatement st = null;
  76. try {
  77. st = conn.prepareStatement(query);
  78. st.executeUpdate();
  79. } catch (SQLException e) {
  80. System.err.print("Failed to send Update '" + query + "'.");
  81. } finally {
  82. this.closeRessources(null, st);
  83. }
  84.  
  85. }
  86.  
  87. public void closeRessources(ResultSet rs , PreparedStatement st){
  88.  
  89. if(rs != null){
  90. try {
  91. rs.close();
  92. } catch (SQLException e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. if(st !=null){
  97. try {
  98. st.close();
  99. } catch (SQLException e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. }
  104.  
  105. public void closeConnection(){
  106.  
  107. try {
  108. this.conn.close();
  109. } catch(SQLException e) {
  110. e.printStackTrace();
  111. } finally{
  112. this.conn=null;
  113. }
  114. }
  115. }
Add Comment
Please, Sign In to add comment