Guest User

12

a guest
Dec 22nd, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. package de.fallenbreak.skyfantasy.database;
  2.  
  3. import com.sun.rowset.CachedRowSetImpl;
  4. import com.zaxxer.hikari.HikariDataSource;
  5. import de.fallenbreak.skyfantasy.SkyFantasyDEAPI;
  6. import org.bukkit.scheduler.BukkitRunnable;
  7.  
  8. import java.sql.*;
  9.  
  10. public class MySQL {
  11.  
  12. String ip, database, username, password;
  13. Integer port;
  14.  
  15. HikariDataSource hikariDataSource;
  16.  
  17. public MySQL(MySQLFile mySQLFile) {
  18. this.ip = mySQLFile.getIp();
  19. this.password = mySQLFile.getPassword();
  20. this.username = mySQLFile.getUsername();
  21. this.database = mySQLFile.getDatabase();
  22. this.port = mySQLFile.getPort();
  23.  
  24. hikariDataSource = new HikariDataSource();
  25.  
  26. hikariDataSource.setJdbcUrl("jdbc:mysql://" + mySQLFile.getIp() + ":" + mySQLFile.getPort() + "/" + mySQLFile.getDatabase());
  27. hikariDataSource.setUsername(mySQLFile.getUsername());
  28. hikariDataSource.setPassword(mySQLFile.getPassword());
  29. hikariDataSource.setMaximumPoolSize(25);
  30.  
  31. }
  32.  
  33. public void execute(final String query) {
  34. new BukkitRunnable() {
  35. @Override
  36. public void run() {
  37.  
  38. try(PreparedStatement preparedStatement = hikariDataSource.getConnection().prepareStatement(query)){
  39. preparedStatement.executeUpdate();
  40. preparedStatement.close();
  41. } catch (SQLException e) {
  42. System.out.println("[]------[ MYSQL-ERROR ]------[]");
  43. System.out.println("Error whilest performing execute.");
  44. System.out.println("Command: " + query);
  45. System.out.println("-> " + e.getMessage());
  46. }
  47. }
  48. }.runTaskAsynchronously(SkyFantasyDEAPI.getInstance());
  49. }
  50.  
  51. public ResultSet executeQuery(final String qry) {
  52. Connection connection = null;
  53. Statement stmt = null;
  54. ResultSet resultSet = null;
  55. CachedRowSetImpl crs = null;
  56.  
  57. try {
  58. connection = hikariDataSource.getConnection();
  59.  
  60. stmt = connection.createStatement();
  61. resultSet = stmt.executeQuery(qry);
  62.  
  63. crs = new CachedRowSetImpl();
  64. crs.populate(resultSet);
  65. } catch (SQLException e) {
  66. e.printStackTrace();
  67. } finally {
  68. try {
  69. if (resultSet != null) {
  70. resultSet.close();
  71. }
  72. if (stmt != null) {
  73. stmt.close();
  74. }
  75. if (connection != null) {
  76. connection.close();
  77. }
  78. } catch (SQLException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. return crs;
  83. }
  84.  
  85. public String getSpezificString(String table, String whatreturn, String bedingung, String value) {
  86. ResultSet rs = executeQuery("SELECT * FROM " + table + " WHERE " + bedingung + "='" + value + "'");
  87. String returner = null;
  88.  
  89. try {
  90. if(rs.next()) {
  91. returner = rs.getString(whatreturn);
  92. }
  93. } catch (SQLException e) {
  94. return null;
  95. }
  96.  
  97. return returner;
  98. }
  99.  
  100. public String getString(String table, String whatreturn) {
  101. ResultSet rs = executeQuery("SELECT * FROM " + table);
  102. String returner = null;
  103.  
  104. try {
  105. if(rs.next()) {
  106. returner = rs.getString(whatreturn);
  107. }
  108. } catch (SQLException e) {
  109. return null;
  110. }
  111.  
  112. return returner;
  113. }
  114. }
Add Comment
Please, Sign In to add comment