Advertisement
Guest User

Untitled

a guest
Jan 9th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. package td3.exercice4.bdd;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.sql.*;
  7. import java.util.Properties;
  8.  
  9. public class ConnexionMySQL {
  10.  
  11. //-----------------------------------------------------------------------------[SINGLETON]
  12. private static ConnexionMySQL instance;
  13. private Connection connexion;
  14.  
  15. private ConnexionMySQL(){
  16. setConnexion(this.creeConnexion());
  17. }
  18.  
  19. public static ConnexionMySQL getInstance(){
  20.  
  21. if( instance == null){
  22. instance = new ConnexionMySQL();
  23. }
  24. return instance;
  25.  
  26. }
  27.  
  28. public Connection creeConnexion(){
  29.  
  30. /*String url = "jdbc:mysql://localhost:8000/moniez1u_bddJava";
  31.  
  32. String login = "moniez1u_appli";
  33. String pwd = "31016843";
  34. Connection maConnexion = null;
  35.  
  36. try {
  37. maConnexion = DriverManager.getConnection(url, login, pwd);
  38. } catch (SQLException sqle) {
  39. System.out.println("Erreur connexion" + sqle.getMessage());
  40. }
  41.  
  42. return maConnexion;*/
  43.  
  44. Connection maConnexion = null;
  45. Properties accesBdd = new Properties();
  46. File fichierBdd = new File("src/td3/exercice4/bdd/infoConnexionMySQL.xml");
  47.  
  48. try{
  49. FileInputStream source = new FileInputStream(fichierBdd);
  50. accesBdd.loadFromXML(source);
  51. }catch (IOException ioe){
  52. ioe.printStackTrace();
  53. }
  54.  
  55. try {
  56. String url = accesBdd.getProperty("api")+":"+accesBdd.getProperty("type")+"://"+ accesBdd.getProperty("adresse_ip")+":"+accesBdd.getProperty("port")+"/"+accesBdd.getProperty("bdd");
  57. maConnexion = DriverManager.getConnection(url, accesBdd.getProperty("login"), accesBdd.getProperty("pass"));
  58. return maConnexion;
  59.  
  60. } catch (SQLException sqle) {
  61. System.out.println("Erreur connexion" + sqle.getMessage());
  62. }
  63.  
  64. return null;
  65. }
  66.  
  67. //-----------------------------------------------------------------------------[GET/SET]
  68. public Connection getConnexion() {
  69. return connexion;
  70. }
  71.  
  72. public void setConnexion(Connection connexion) {
  73.  
  74. if(connexion != null){
  75. this.connexion = connexion;
  76. }
  77.  
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement