Advertisement
Guest User

Untitled

a guest
May 31st, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. package Conexao;
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7.  
  8. public class ConnectionFactory {
  9.  
  10.      private static ConnectionFactory connectionFactory = null;
  11.        private static Connection connection = null;
  12.  
  13.        private String url    = "jdbc:mysql://localhost/clockpoint";
  14.        private String username = "root";
  15.            private String password = "franklin2";
  16.            private String driver ="com.mysql.jdbc.Driver" ;
  17.  
  18.  
  19.  
  20.       private ConnectionFactory(){ }
  21.  
  22.  
  23.       public static ConnectionFactory getInstance(){
  24.  
  25.         if(connectionFactory == null)
  26.          connectionFactory = new ConnectionFactory();
  27.  
  28.         return connectionFactory;
  29.  
  30.       }
  31.  
  32.  
  33.  
  34.       public Connection getConnection() throws Exception{
  35.  
  36.         if(connection == null){
  37.           try {
  38.              Class.forName(driver);
  39.             connection = DriverManager.getConnection(url, username, password);
  40.           }
  41.           catch (SQLException e) {
  42.           System.out.println("\n>> Problema ao obter conexao\n");
  43.           e.printStackTrace();
  44.           throw new Exception();
  45.          }
  46.         }
  47.  
  48.        return connection;
  49.          }
  50.  
  51.  
  52.  
  53.  
  54.          public void closeConnection(){
  55.  
  56.         if(connection != null){
  57.  
  58.          try {
  59.                             connection.close();
  60.                         }
  61.          catch (SQLException e) {
  62.  
  63.           System.out.println("\n>> Problema ao fechar conexao\n");
  64.           e.printStackTrace();
  65.  
  66.          }
  67.        }
  68.  
  69.          }
  70.  
  71.  
  72.  
  73.  
  74.             public void finalize(){
  75.  
  76.                 closeConnection();
  77.            }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement