Advertisement
Guest User

MySQL

a guest
Jan 10th, 2017
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. package de.shorty.friendsgui.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.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.configuration.file.FileConfiguration;
  13.  
  14. import de.shorty.friendsgui.FriendsGUI;
  15.  
  16.  
  17. public class MySQL {
  18.  
  19.  
  20. private FriendsGUI friends;
  21. private static String pr = "§3FileManager§8> ";
  22. private static String HOST = "";
  23. private static String DATABASE = "";
  24. private static String USER = "";
  25. private static String PASSWORD = "";
  26. private static String PORT = "";
  27.  
  28. private static Connection con;
  29. private boolean ic;
  30.  
  31.  
  32. public MySQL(FriendsGUI friends){
  33. this.friends = friends;
  34. }
  35.  
  36.  
  37. public boolean isConnected(){
  38. return ic;
  39. }
  40.  
  41. public void CreateMySQLFile(String path){
  42. friends.api.createNewFile("mysql.yml", path);
  43. File f = friends.api.getFile("mysql.yml", path);
  44. FileConfiguration cfg = friends.api.getConfiguration("mysql.yml", path);
  45. cfg.options().copyDefaults(true);
  46. cfg.addDefault("username", "root");
  47. cfg.addDefault("password", "password");
  48. cfg.addDefault("database", "localhost");
  49. cfg.addDefault("host", "localhost");
  50. cfg.addDefault("port", "3306");
  51. try {
  52. cfg.save(f);
  53. } catch (IOException e) {
  54. Bukkit.getConsoleSender().sendMessage(pr+"§cCould not save file 'mysql.yml'.");
  55. }
  56. USER = cfg.getString("username");
  57. PASSWORD = cfg.getString("password");
  58. DATABASE = cfg.getString("database");
  59. HOST = cfg.getString("host");
  60. PORT = cfg.getString("port");
  61. connect();
  62. }
  63.  
  64. public void connect() {
  65. try {
  66. con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":" + PORT + "/" + DATABASE + "?autoReconnect=true", USER, PASSWORD);
  67. Bukkit.getConsoleSender().sendMessage(pr+"§aSuccessfully connected to MySQL-Database.");
  68. ic = true;
  69. } catch (SQLException e) {
  70. Bukkit.getConsoleSender().sendMessage(pr+"§cCould not connect to MySQL-Database, please check your MySQL-Settings.");
  71. ic = false;
  72. }
  73. }
  74.  
  75. public void close() {
  76. try {
  77. if(con != null) {
  78. con.close();
  79. Bukkit.getConsoleSender().sendMessage(pr+"§aSuccessfully closed MySQL-Connection.");
  80. ic = false;
  81. }else{
  82. Bukkit.getConsoleSender().sendMessage(pr+"§cThere is no running connection to MySQL-Database.");
  83. }
  84. } catch (SQLException e) {
  85. Bukkit.getConsoleSender().sendMessage(pr+"§cCould not close MySQL-Connection, ERROR-CODE: "+e.getMessage());
  86. }
  87. }
  88.  
  89. public void update(String qry) {
  90. try {
  91. Statement st = con.createStatement();
  92. st.executeUpdate(qry);
  93. st.close();
  94. } catch (SQLException e) {
  95. System.err.println(e);
  96. }
  97. }
  98.  
  99. public ResultSet getResult(String qry) {
  100. ResultSet rs = null;
  101.  
  102. try {
  103. Statement st = con.createStatement();
  104. rs = st.executeQuery(qry);
  105. } catch (SQLException e) {
  106. System.err.println(e);
  107. }
  108. return rs;
  109. }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement