Advertisement
Guest User

Mysql Class

a guest
Apr 12th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. package de.proxysocke.net.joey.joeyvotesystem.Database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7. import java.text.MessageFormat;
  8. import java.util.concurrent.ExecutorService;
  9. import java.util.concurrent.Executors;
  10.  
  11. public class MySQLManager {
  12.  
  13. public static String host = "localhost";
  14. public static String port = "3306";
  15. public static String database = "devserver";
  16. public static String username = "devserver";
  17. public static String password = "EvcJJlrBNf0JF251";
  18. public static Connection con;
  19.  
  20. private static ExecutorService mservice = Executors.newCachedThreadPool();
  21.  
  22. public static void connect(){
  23.  
  24. if(isConnectet() == false){
  25.  
  26. try {
  27.  
  28. String url = MessageFormat.format("jdbc:mysql://{0}:{1}/{2}", host, port, database);
  29.  
  30. con = DriverManager.getConnection(url, username, password);
  31.  
  32. System.out.println("[JoeyMySQLManager @JoeyVoteSystem] Verbindung wurde hergestellt");
  33.  
  34. } catch (SQLException e) {
  35.  
  36. e.printStackTrace();
  37. }
  38.  
  39. }
  40.  
  41. }
  42.  
  43. public static void checkConnection(){
  44.  
  45. if(!isConnectet()){
  46.  
  47. connect();
  48.  
  49. }
  50.  
  51. }
  52.  
  53.  
  54. public static void disconect(){
  55.  
  56. if(isConnectet() == true){
  57.  
  58. try {
  59. con.close();
  60. System.out.println("[JoeyMySQLManager @JoeyVoteSystem] Verbindung wurde getrennt");
  61. } catch (SQLException e) {
  62.  
  63. e.printStackTrace();
  64. }
  65.  
  66. }
  67.  
  68. }
  69.  
  70. public static void UpdateAsync(final PreparedStatement statement){
  71.  
  72. mservice.execute(()-> {
  73. try {
  74. update(statement);
  75. } catch (SQLException e) {
  76.  
  77. e.printStackTrace();
  78. }
  79. });
  80.  
  81. }
  82.  
  83. private static void update(PreparedStatement statement) throws SQLException{
  84.  
  85. if(isConnectet() == true){
  86.  
  87. statement.executeUpdate();
  88. statement.close();
  89.  
  90. }
  91.  
  92. }
  93.  
  94. private static boolean isConnectet(){
  95.  
  96. if(con == null){
  97.  
  98. return false;
  99.  
  100. } else {
  101.  
  102. return true;
  103.  
  104. }
  105.  
  106. }
  107.  
  108. public static Connection getConnection(){
  109.  
  110. return con;
  111.  
  112. }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement