Advertisement
Guest User

Untitled

a guest
Jan 28th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. package com.sdzee.dao;
  2.  
  3. //import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.SQLException;
  9. import java.util.Properties;
  10.  
  11. public class DAOFactory {
  12.  
  13. private static final String FICHIER_PROPERTIES = "/com/sdzee/dao/dao.properties";
  14. private static final String PROPERTY_URL = "url";
  15. private static final String PROPERTY_DRIVER = "driver";
  16. private static final String PROPERTY_NOM_UTILISATEUR = "nomutilisateur";
  17. private static final String PROPERTY_MOT_DE_PASSE = "motdepasse";
  18.  
  19. private String url;
  20. private String username;
  21. private String password;
  22.  
  23. DAOFactory( String url, String username, String password ) {
  24. this.url = url;
  25. this.username = username;
  26. this.password = password;
  27. }
  28.  
  29. /*
  30. * Méthode chargée de récupérer les informations de connexion à la base de
  31. * données, charger le driver JDBC et retourner une instance de la Factory
  32. */
  33. public static DAOFactory getInstance() throws DAOExceptionConfiguration {
  34. Properties properties = new Properties();
  35. String url;
  36. String driver;
  37. String nomUtilisateur;
  38. String motDePasse;
  39.  
  40. ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  41. InputStream fichierProperties = classLoader.getResourceAsStream( FICHIER_PROPERTIES );
  42.  
  43. if ( fichierProperties == null ) {
  44. throw new DAOExceptionConfiguration( "Le fichier properties " + FICHIER_PROPERTIES + " est introuvable." );
  45. }
  46.  
  47. try {
  48. properties.load( fichierProperties );
  49. url = properties.getProperty( PROPERTY_URL );
  50. driver = properties.getProperty( PROPERTY_DRIVER );
  51. nomUtilisateur = properties.getProperty( PROPERTY_NOM_UTILISATEUR );
  52. motDePasse = properties.getProperty( PROPERTY_MOT_DE_PASSE );
  53. } catch ( IOException e ) {
  54. throw new DAOExceptionConfiguration( "Impossible de charger le fichier properties " + FICHIER_PROPERTIES, e );
  55. }
  56.  
  57. try {
  58. Class.forName( driver );
  59. } catch ( ClassNotFoundException e ) {
  60. throw new DAOExceptionConfiguration( "Le driver est introuvable dans le classpath.", e );
  61. }
  62.  
  63. DAOFactory instance = new DAOFactory( url, nomUtilisateur, motDePasse );
  64. return instance;
  65. }
  66.  
  67. /* Méthode chargée de fournir une connexion à la base de données */
  68. /* package */ Connection getConnection() throws SQLException {
  69. return DriverManager.getConnection( url, username, password );
  70. }
  71.  
  72. /*
  73. * Méthodes de récupération de l'implémentation des différents DAO (un seul
  74. * pour le moment) ICI ON VA INSTANCUER UtilisateurAO
  75. */
  76. public UtilisateurDAO getUtilisateurDao() {
  77. return new UtilisateurDAOImpl( this );
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement