Advertisement
Guest User

Untitled

a guest
Jun 14th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4.  
  5. public class BDD {
  6. private String serveur, bdd, user, mdp;
  7. private Connection maConnexion;
  8.  
  9. public BDD (String serveur, String bdd, String user, String mdp){
  10. this.serveur = serveur;
  11. this.bdd = bdd;
  12. this.user = user;
  13. this.mdp = mdp;
  14. this.maConnexion = null;
  15. }
  16.  
  17. public void chargerPilote() {
  18. //verification de l'import du pilote Mysql
  19. try{
  20. Class.forName("com.mysql.jdbc.Driver");
  21. }
  22. catch (ClassNotFoundException exp) {
  23. System.out.println("Erreur de chargement de pilote !");
  24. }
  25. }
  26.  
  27. public void seConnecter() {
  28. this.chargerPilote();
  29. String url = "jdbc:mysql://"+this.serveur+"/"+this.bdd;
  30. try{
  31. this.maConnexion = DriverManager.getConnection(url,this.user,this.mdp);
  32. }
  33. catch(SQLException exp){
  34. System.out.println("Erreur de connexion au "+url);
  35. }
  36. }
  37.  
  38. public void seDeconnecter() {
  39. try {
  40. if (this.maConnexion != null)
  41. {
  42. this.maConnexion.close();
  43. }
  44. }
  45. catch (SQLException exp)
  46. {
  47. System.out.println("Erreur de deconnexion !");
  48. }
  49. }
  50.  
  51. public Connection getMaConnexion() {
  52. return maConnexion;
  53. }
  54.  
  55. public void setMaConnexion(Connection maConnexion) {
  56. this.maConnexion = maConnexion;
  57. }
  58.  
  59.  
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement