Guest User

Untitled

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