Advertisement
Guest User

Conexao

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