Guest User

Untitled

a guest
Aug 27th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. package com.map.singleton;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8.  
  9. /**
  10. * Singleton Pattern
  11. *
  12. **/
  13. public class Conexao {
  14.  
  15. private Conexao () { }
  16. private static Connection con = null;
  17.  
  18. public static Connection getConnection() {
  19. System.out.println("Conectando ao banco...");
  20.  
  21. if (con == null) {
  22.  
  23. try {
  24.  
  25. Class.forName("com.mysql.jdbc.Driver");
  26. con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/banco","usuario","senha");
  27. System.out.println("Conectado.");
  28. return this.con;
  29.  
  30. } catch(SQLException e) {
  31. throw new RuntimeException(e);
  32. }
  33.  
  34. } else {
  35.  
  36. return this.con;
  37. }
  38. }
  39. }
Add Comment
Please, Sign In to add comment