Advertisement
Guest User

MySQL Klasse

a guest
Dec 11th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. public static Connection con;
  2.  
  3. public static void connect(){
  4.  
  5. try {
  6. File file = new File("plugins/OneLine", "config.yml");
  7. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  8. String host = cfg.getString("host");
  9. String database = cfg.getString("database");
  10. String username = cfg.getString("user");
  11. String pass = cfg.getString("password");
  12. con = DriverManager.getConnection("jdbc:mysql://"+host+":3306"+"/"+database, username, pass);
  13. Bukkit.getConsoleSender().sendMessage("§f[§eMySQL§f] §aVerbunden");
  14. createTable();
  15. } catch (SQLException e) {
  16. e.printStackTrace();
  17. Bukkit.getConsoleSender().sendMessage("§f[§eMySQL§f] §cFehler beim Verbinden");
  18. }
  19. }
  20.  
  21. public static void close(){
  22. if(!isConnected()){
  23. try {
  24. con.close();
  25. Bukkit.getConsoleSender().sendMessage("§f[§eMySQL§f] §cVerbindung geschlossen");
  26. } catch (SQLException e) {
  27. e.printStackTrace();
  28. Bukkit.getConsoleSender().sendMessage("§f[§eMySQL§f] §cFehler beim schließen der Verbindung!");
  29.  
  30. }
  31.  
  32. }
  33. }
  34.  
  35. public static boolean isConnected(){
  36. return con != null;
  37. }
  38.  
  39. public static void createTable(){
  40.  
  41.  
  42. if(isConnected()){
  43. try {
  44.  
  45. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS stats (Spielername VARCHAR(100), " +
  46. "Kills INTEGER(100), Deaths INTEGER(100))");
  47.  
  48. Bukkit.getConsoleSender().sendMessage("§f[§eMySQL§f] §6Tabellen erstellt, falls noch nicht vorhanden");
  49. } catch (SQLException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53.  
  54. }
  55.  
  56. public static void update(String qry){
  57. if(isConnected()){
  58. try {
  59. con.createStatement().executeUpdate(qry);
  60. } catch (SQLException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65.  
  66. public static ResultSet getResult(String qry){
  67. if(isConnected()){
  68. try {
  69. return con.createStatement().executeQuery(qry);
  70. } catch (SQLException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. return null;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement