Advertisement
Guest User

Untitled

a guest
May 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. package connection;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.swing.JOptionPane;
  15.  
  16. /**
  17.  *
  18.  * @author carlosrorato
  19.  */
  20. public class ConnectionFactory {
  21.    
  22.    
  23.     private static String DRIVER;
  24.     private static String URL;
  25.     private static String USER;
  26.     private static String PASS;
  27.    
  28.     public static void obterDadosBD() throws IOException{
  29.         try {
  30.             FileReader conf = new FileReader("config.txt");
  31.             BufferedReader lerArq = new BufferedReader(conf);
  32.             ConnectionFactory.DRIVER = lerArq.readLine();
  33.             ConnectionFactory.URL = lerArq.readLine();
  34.             ConnectionFactory.USER = lerArq.readLine();
  35.             ConnectionFactory.PASS = lerArq.readLine();
  36.            
  37.         } catch (FileNotFoundException ex) {
  38.             JOptionPane.showMessageDialog(null,"Erro ao abrir o arquivo config.txt");
  39.         }
  40.        
  41.     }
  42.    
  43.     public static Connection getConnection(){
  44.  
  45.         try {
  46.             try {
  47.                 obterDadosBD();
  48.             } catch (IOException ex) {
  49.                 Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
  50.             }
  51.             Class.forName(DRIVER);
  52.            
  53.             return DriverManager.getConnection(URL,USER,PASS);
  54.            
  55.         } catch (ClassNotFoundException | SQLException ex) {
  56.             throw new RuntimeException("Erro na conexĂŁo: ",ex);
  57.         }    
  58.     }
  59.    
  60.     public static void closeConnection(Connection con){
  61.        
  62.             try {
  63.                 if(con!=null){
  64.                     con.close();
  65.                 }
  66.             } catch (SQLException ex) {
  67.                 Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
  68.             }
  69.        
  70.     }
  71.    
  72.     public static void closeConnection(Connection con, PreparedStatement stmt){
  73.            
  74.             closeConnection(con);
  75.            
  76.             try {
  77.                
  78.                 if(stmt!=null){
  79.                     stmt.close();
  80.                 }
  81.            
  82.             } catch (SQLException ex) {
  83.                 Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
  84.             }
  85.        
  86.     }
  87.    
  88.     public static void closeConnection(Connection con, PreparedStatement stmt, ResultSet rs){
  89.            
  90.             closeConnection(con,stmt);
  91.            
  92.             try {
  93.                
  94.                if(rs!=null){
  95.                    rs.close();
  96.                }
  97.            
  98.             } catch (SQLException ex) {
  99.                 Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
  100.             }
  101.        
  102.     }
  103.    
  104.    
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement