Advertisement
Irlan

Factory

Apr 2nd, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. public class FactoryCon {
  2.    
  3.     private static Connection con;
  4.    
  5.     public static Connection getConection(){
  6.  
  7.         try{
  8.             //Está é a forma de verifcar se já existe conexão se
  9.             //for "vazio" ele cria se não ele só retorna a
  10.             //a conexão aberta.
  11.             if(con == null){
  12.                
  13.                 Class.forName( "com.mysql.jdbc.Driver" ); // Conexão com o banco
  14.  
  15.                 String url = "jdbc:mysql://127.0.0.1:3306/headquarter";
  16.                 String user = "root";
  17.                 String password = "Irlanzinhobr2";
  18.  
  19.                 con = DriverManager.getConnection(url,user,password); // Conexão com o banco
  20.                 return con;
  21.            
  22.             }else{
  23.                
  24.                 return con; //Retorna a conexão existente.
  25.             }
  26.            
  27.         }
  28.         //Depois do uso do Throw o erro no retorno foi resolvido.
  29.         catch(SQLException | ClassNotFoundException e){ //Exceção generica.
  30.            
  31.             JOptionPane.showMessageDialog(null,"Error na classe FactoryCon: "+ e.getMessage());
  32.             return null;
  33.         }
  34.        
  35.  
  36.     }
  37.    
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement