Advertisement
Guest User

Untitled

a guest
Oct 4th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. public class MySQL
  2. {
  3.  
  4. private static Connection conn;
  5.  
  6.  
  7. public void connect(String host, String database, int port, String user, String password){
  8. if(!isConnected()){
  9. try {
  10. conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user, password);
  11. } catch (SQLException e) {
  12. e.printStackTrace();
  13. System.out.println("[SkyWarStats] Errore con il database");
  14. }
  15. }
  16. }
  17.  
  18. public void disconnect(){
  19. if(isConnected()){
  20. try {
  21. conn.close();
  22. }catch (SQLException e){
  23. e.printStackTrace();
  24. }
  25. }
  26. }
  27.  
  28. public boolean isConnected(){
  29. try{
  30. if((conn == null) || (conn.isClosed()) || (conn.isValid(5))){
  31. return false;
  32. }
  33. return true;
  34. }catch(SQLException e){
  35. e.printStackTrace();
  36. }
  37. return false;
  38. }
  39.  
  40. public static Connection getConnection(){
  41. return conn;
  42. }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement