Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Conexao {
- private static Conexao instance = null;
- private final String USERNAME = user;
- private final String PASSWORD = pass;
- private final String CONN_STRING = "jdbc:sqlserver://<ip>:<port>;DatabaseName=<database>";
- private Connection conn = null;
- public static Conexao getInstance() {
- if (instance == null) {
- instance = new Conexao();
- }
- return instance;
- }
- private boolean openConnection()
- {
- try {
- conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
- return true;
- }
- catch (SQLException e) {
- System.err.println(e);
- return false;
- }
- }
- public Connection getConnection()
- {
- if (conn == null) {
- if (openConnection()) {
- System.out.println("Conexão aberta!");
- return conn;
- } else {
- return null;
- }
- }
- System.out.println("Sem necessidade...");
- return conn;
- }
- public void close() {
- System.out.println("Fechando conexão...");
- try {
- conn.close();
- conn = null;
- System.out.println("Conexão fechada!");
- } catch (Exception e) {}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement