Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. package de.finn.lobbysystem.adventskalender;
  2.  
  3. import de.finn.lobbysystem.LobbySystem;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.scheduler.BukkitRunnable;
  6.  
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13.  
  14. public class MySQL {
  15.  
  16. private String host;
  17. private String database;
  18. private String user;
  19. private String password;
  20.  
  21.  
  22. public MySQL(String host, String database, String user, String password) throws ClassNotFoundException {
  23.  
  24. Class.forName("com.mysql.jdbc.Driver");
  25.  
  26. this.host = host;
  27. this.database = database;
  28. this.user = user;
  29. this.password = password;
  30.  
  31. }
  32.  
  33. private Connection con;
  34. public Connection openConnection() throws Exception {
  35.  
  36. System.out.println("MySQL Connected.");
  37. return con = DriverManager.getConnection("jdbc:mysql://" + this.host + "/" + this.database + "?user=" + this.user + "&password=" + this.password + "&?autoreconnect=true");
  38.  
  39. }
  40.  
  41. public void createTable() {
  42.  
  43. this.queryUpdate("CREATE TABLE `DATENBANK`.`adventskalender` ( `id` INT(11) NOT NULL AUTO_INCREMENT , `UUID` VARCHAR(255) NOT NULL , `LastReward` INT(11) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;");
  44.  
  45. }
  46.  
  47. public Connection getConnection() {
  48. return con;
  49. }
  50.  
  51. public void queryUpdate(String query) {
  52.  
  53. PreparedStatement st = null;
  54. try {
  55. st = (PreparedStatement)con.prepareStatement(query);
  56. st.executeUpdate();
  57. } catch (SQLException e) {
  58. e.printStackTrace();
  59. } finally {
  60. closeRessources(null, st);
  61. }
  62.  
  63. }
  64.  
  65. public void closeRessources(ResultSet rs, PreparedStatement st) {
  66.  
  67. if(rs != null) {
  68. try {
  69. rs.close();
  70. } catch (SQLException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. if(st != null) {
  75. try {
  76. st.close();
  77. } catch (SQLException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81.  
  82. }
  83.  
  84. public void closeConnection() {
  85. try {
  86. con.close();
  87. } catch (SQLException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91.  
  92. public ResultSet query(String query) {
  93. ResultSet rs = null;
  94. try {
  95. Statement st = con.createStatement();
  96. rs = st.executeQuery(query);
  97. } catch (SQLException e) {
  98. e.printStackTrace();
  99. }
  100. return rs;
  101. }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement