Advertisement
Guest User

Développement d'un plugin de Hub #03 - MySQL.java

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