Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. /**
  2. * Class: MySQL
  3. * Package: de.centumvenatus.util.mysql
  4. */
  5.  
  6. package de.centumvenatus.util.mysql;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. /**
  16. * @author SightCrafterHD
  17. */
  18. public class MySQL {
  19.  
  20. private static String username = "lobby";
  21. private static String password = "WAZJpURtSae8YqZ6";
  22. private static String database = "minelabs";
  23. private static String host = "localhost";
  24. private static Integer port = 3306;
  25.  
  26. private static Connection connection;
  27.  
  28.  
  29. public static Connection getConnection() {
  30. return connection;
  31. }
  32.  
  33. public static Boolean isConnected() {
  34.  
  35. if(connection == null) return false;
  36.  
  37. try {
  38. if(connection.isClosed()) return false;
  39. } catch (SQLException ex) {
  40. Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  41. return false;
  42. }
  43. return true;
  44. }
  45.  
  46.  
  47. public static Boolean connect() {
  48. if(!isConnected()) {
  49. try {
  50. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  51. return true;
  52. } catch (SQLException ex) {
  53. Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  54. return false;
  55. }
  56. } else {
  57. return true;
  58. }
  59. }
  60.  
  61. public static Boolean forceConnect() {
  62. try {
  63. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  64. return true;
  65. } catch (SQLException ex) {
  66. Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  67. return false;
  68. }
  69. }
  70.  
  71. public static Boolean close() {
  72. if(!isConnected()) {
  73. try {
  74. connection.close();
  75. return true;
  76. } catch (SQLException ex) {
  77. Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  78. return false;
  79. }
  80. } else {
  81. return true;
  82. }
  83. }
  84.  
  85. public static void update(String query) throws SQLException {
  86. if(isConnected()) {
  87. connection.createStatement().executeUpdate(query);
  88. }
  89. }
  90.  
  91. public static ResultSet getResult(String query) throws SQLException {
  92. if(isConnected()) {
  93. return connection.createStatement().executeQuery(query);
  94. } else {
  95. return null;
  96. }
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement