Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. package br.com.ab.services;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.util.Properties;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. /**
  13. *
  14. * @author Pedro Enju
  15. */
  16. public class Conexao {
  17.  
  18. private Connection conn;
  19. private static Conexao instance = null;
  20.  
  21. private Conexao() {
  22. this.conn = Conexao.createConnection();
  23. }
  24.  
  25. private static Connection createConnection() {
  26. Properties config = getConfig();
  27.  
  28. Connection conn = null;
  29. try {
  30. Class.forName(config.getProperty("conndriver"));
  31.  
  32. conn = DriverManager.getConnection(
  33. config.getProperty("connurl"),
  34. config
  35. );
  36.  
  37. } catch (Exception e) {
  38. System.out.println("Erro: " + e.getMessage());
  39. }
  40. return conn;
  41. }
  42.  
  43. public static Conexao getInstance() {
  44. if (Conexao.instance == null) {
  45. Conexao.instance = new Conexao();
  46. }
  47. return Conexao.instance;
  48. }
  49.  
  50. public Connection getConn() {
  51. return conn;
  52. }
  53.  
  54. public static Properties getConfig() {
  55. Properties config = new Properties();
  56. try {
  57. config.load(new FileInputStream(System.getProperty("user.dir").concat("/db.properties")));
  58. } catch (FileNotFoundException ex) {
  59. Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
  60. } catch (IOException ex) {
  61. Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
  62. }
  63. return config;
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement