Advertisement
Guest User

Untitled

a guest
Jan 9th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. public class Conexao {
  2.  
  3. private static Conexao instance = null;
  4. private final String USERNAME = user;
  5. private final String PASSWORD = pass;
  6. private final String CONN_STRING = "jdbc:sqlserver://<ip>:<port>;DatabaseName=<database>";
  7. private Connection conn = null;
  8.  
  9. public static Conexao getInstance() {
  10. if (instance == null) {
  11. instance = new Conexao();
  12. }
  13. return instance;
  14. }
  15.  
  16.  
  17. private boolean openConnection()
  18. {
  19. try {
  20. conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
  21. return true;
  22. }
  23. catch (SQLException e) {
  24. System.err.println(e);
  25. return false;
  26. }
  27. }
  28.  
  29. public Connection getConnection()
  30. {
  31. if (conn == null) {
  32. if (openConnection()) {
  33. System.out.println("Conexão aberta!");
  34. return conn;
  35. } else {
  36. return null;
  37. }
  38. }
  39. System.out.println("Sem necessidade...");
  40. return conn;
  41. }
  42.  
  43. public void close() {
  44. System.out.println("Fechando conexão...");
  45. try {
  46. conn.close();
  47. conn = null;
  48. System.out.println("Conexão fechada!");
  49. } catch (Exception e) {}
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement