Advertisement
Guest User

MySQLClass

a guest
Jan 10th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. package me.mistercat.plugin.mysql;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.configuration.file.FileConfiguration;
  12.  
  13. import me.mistercat.plugin.Main;
  14. import me.mistercat.plugin.util.FileManager;
  15.  
  16. public class MySQL {
  17.  
  18. private static String HOST = "";
  19. private static String DATABASE = "";
  20. private static String USER = "";
  21. private static String PASSWORD = "";
  22.  
  23. private static Connection con;
  24.  
  25. public static void createMySQLFile(String path){
  26. File f = FileManager.createNewFile("mysql.yml", path);
  27. FileConfiguration cfg = FileManager.getConfiguration("mysql.yml", path);
  28. cfg.options().copyDefaults(true);
  29. cfg.addDefault("username", "root");
  30. cfg.addDefault("password", "password");
  31. cfg.addDefault("database", "localhost");
  32. cfg.addDefault("host", "host");
  33. try {
  34. cfg.save(f);
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. HOST = cfg.getString("host");
  39. DATABASE = cfg.getString("database");
  40. USER = cfg.getString("user");
  41. PASSWORD = cfg.getString("password");
  42. }
  43.  
  44. public static boolean isConnected(){
  45. return con == null;
  46. }
  47.  
  48. public static void connect(){
  49. try {
  50. con = DriverManager.getConnection("jdbc:mysql://"+HOST+":3306/"+DATABASE+"?autoReconnect=true", USER,PASSWORD);
  51. Bukkit.getConsoleSender().sendMessage(Main.pr+"§2Verbindung zu MySQL aufgebaut!");
  52. } catch (SQLException e) {
  53. Bukkit.getConsoleSender().sendMessage(Main.pr+"§cVerbindung zu MySQL nicht aufgebaut! ERROR: "+e.getMessage());
  54. }
  55. }
  56. public static void close(){
  57. try {
  58. if(con != null){
  59. con.close();
  60. con = null;
  61. Bukkit.getConsoleSender().sendMessage(Main.pr+"§2Verbindung zu MySQL geschlossen!");
  62. }else{
  63. Bukkit.getConsoleSender().sendMessage(Main.pr+"§2Verbindung zu MySQL nicht geschlossen! Grund: nicht aktiv!");
  64.  
  65. }
  66. } catch (SQLException e) {
  67. Bukkit.getConsoleSender().sendMessage(Main.pr+"§cVerbindung zu MySQL geschlossen! ERROR: "+e.getMessage());
  68. }
  69. }
  70.  
  71. public static void update(String qry){
  72. try {
  73. Statement st = con.createStatement();
  74. st.executeQuery(qry);
  75. st.close();
  76. } catch (SQLException e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. public static ResultSet getResult(String qry){
  81. ResultSet rs = null;
  82. try {
  83. Statement st = con.createStatement();
  84. rs = st.executeQuery(qry);
  85.  
  86. } catch (SQLException e) {
  87. e.printStackTrace();
  88. }
  89. return rs;
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement