Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Conexao;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class ConnectionFactory {
- private static ConnectionFactory connectionFactory = null;
- private static Connection connection = null;
- private String url = "jdbc:mysql://localhost/clockpoint";
- private String username = "root";
- private String password = "franklin2";
- private String driver ="com.mysql.jdbc.Driver" ;
- private ConnectionFactory(){ }
- public static ConnectionFactory getInstance(){
- if(connectionFactory == null)
- connectionFactory = new ConnectionFactory();
- return connectionFactory;
- }
- public Connection getConnection() throws Exception{
- if(connection == null){
- try {
- Class.forName(driver);
- connection = DriverManager.getConnection(url, username, password);
- }
- catch (SQLException e) {
- System.out.println("\n>> Problema ao obter conexao\n");
- e.printStackTrace();
- throw new Exception();
- }
- }
- return connection;
- }
- public void closeConnection(){
- if(connection != null){
- try {
- connection.close();
- }
- catch (SQLException e) {
- System.out.println("\n>> Problema ao fechar conexao\n");
- e.printStackTrace();
- }
- }
- }
- public void finalize(){
- closeConnection();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement