Advertisement
Guest User

Untitled

a guest
Feb 20th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. public class MySQL
  2. {
  3. private String host;
  4. private int port;
  5. private String user;
  6. private String password;
  7. private String database;
  8. private Connection conn;
  9.  
  10. public MySQL()
  11. {
  12. File file = new File("plugins/MySQLBan/", "database.yml");
  13. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  14. cfg.options().header("Ersetze den localhost durch den MySQL-Serverport. [localhost ist Standard]\nErsetze den port durch den MySQL-Serverport. [3306 ist Standard]\nErsetze das root durch deinen MySQL-Server User. [z.B. root]\nErsetze das root durch dein MySQL-Server Passwort. [z.B. root]\nErsetze die Datenbank durch deine MySQL Datenbank die du verwenden willst.");
  15.  
  16. String db = "database.";
  17. cfg.addDefault(db + "MySQLHost", "localhost");
  18. cfg.addDefault(db + "MySQLPort", Integer.valueOf(3306));
  19. cfg.addDefault(db + "MySQLUser", "root");
  20. cfg.addDefault(db + "MySQLPasswort", "root");
  21. cfg.addDefault(db + "MySQLDatenbank", "Datenbank");
  22. cfg.options().copyDefaults(true);
  23. try
  24. {
  25. cfg.save(file);
  26. }
  27. catch (IOException e)
  28. {
  29. e.printStackTrace();
  30. }
  31. this.host = cfg.getString(db + "MySQLHost");
  32. this.port = cfg.getInt(db + "MySQLPort");
  33. this.user = cfg.getString(db + "MySQLUser");
  34. this.password = cfg.getString(db + "MySQLPasswort");
  35. this.database = cfg.getString(db + "MySQLDatenbank");
  36.  
  37. this.conn = openConnection();
  38. }
  39.  
  40. public Connection openConnection()
  41. {
  42. try
  43. {
  44. Class.forName("com.mysql.jdbc.Driver");
  45. Connection conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.user, this.password);
  46. this.conn = conn;
  47. return conn;
  48. }
  49. catch (SQLException e)
  50. {
  51. e.printStackTrace();
  52. }
  53. catch (ClassNotFoundException e)
  54. {
  55. e.printStackTrace();
  56. }
  57. return null;
  58. }
  59.  
  60. public Connection getConnection()
  61. {
  62. return this.conn;
  63. }
  64.  
  65. public boolean hasConnection()
  66. {
  67. try
  68. {
  69. return this.conn != null | this.conn.isValid(1);
  70. }
  71. catch (SQLException e) {}
  72. return false;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement