Advertisement
Guest User

conexao

a guest
Sep 25th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. package util;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class Conexao {
  8.  
  9.     private static Connection conexao;
  10.  
  11.     private static Connection conectar() {
  12.         try {
  13.             Class.forName("org.postgresql.Driver");
  14.             return DriverManager.getConnection("jdbc:postgresql://localhost/clubefatesg2016", "postgres", "123456");
  15.         } catch (ClassNotFoundException e) {
  16.             System.out.println("Driver não encontrado no CLASSPATH");
  17.             return null;
  18.         } catch (SQLException e) {
  19.             System.out.println("Erro na conexao com o banco verifique a url, o usuário e a senha");
  20.             return null;
  21.         }
  22.        
  23.     }
  24.  
  25.     public static Connection getConexao() {
  26.         try {
  27.             if (conexao == null || conexao.isClosed()) {
  28.                 conexao = conectar();
  29.             }
  30.         } catch (SQLException e) {
  31.             System.out.println(e.getMessage());
  32.             e.printStackTrace();
  33.         }
  34.        
  35.         return conexao;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement