Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. package MySQL;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.SQLException;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8.  
  9. import org.bukkit.Bukkit;
  10.  
  11. import com.mysql.jdbc.Connection;
  12.  
  13. public class MySQL {
  14.  
  15. public static java.sql.Connection con;
  16.  
  17. public static Connection connection(String host, int port, String database, String user, String password) {
  18. try {
  19. con = (Connection)DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", user, password);
  20. Bukkit.getConsoleSender().sendMessage("§7[§aMySQL§7] §aDatenbankverbindung hergestellt.");
  21. } catch (SQLException e) {
  22. Bukkit.getConsoleSender().sendMessage("§7[§aMySQL§7] §cDatenbankverbindung konnte nicht hergestellt werden: " + e.getMessage());
  23. e.printStackTrace();
  24. }
  25. return (Connection) con;
  26. }
  27.  
  28. public static void disconnect() {
  29. try {
  30. con.close();
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. public static boolean hasConnection() {
  37. if (con != null) {
  38. return true;
  39. }
  40. return false;
  41. }
  42.  
  43. public static void connect() {
  44. try {
  45. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/***autoReconnect=true", "benutzername", "pw");
  46. }
  47. catch (SQLException ex) {
  48. Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  49. }
  50. }
  51.  
  52. public static void close() {
  53. if (con != null) {
  54. try {
  55. con.close();
  56. }
  57. catch (SQLException ex) {
  58. Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  59. }
  60. }
  61. }
  62.  
  63. public static void createTable() {
  64. try {
  65. PreparedStatement ps = con.prepareStatement("CREATE TABLE IF NOT EXISTS Gundula (UUID VARCHAR(100), Zeit VARCHAR(100))");
  66. ps.executeUpdate();
  67. ps.close();
  68. }
  69. catch (SQLException ps) {
  70. // empty catch block
  71. }
  72. }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement