Kenmegne
2025-12-10 e9d80d486b912144b59ebd5939d4837105b37b99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.megatim.queryadhoc.factory;
 
import com.megatim.queryadhoc.enums.SgbdType;
import com.megatim.queryadhoc.exceptions.ConnectionException;
import com.megatim.queryadhoc.exceptions.ConnectionParameterException;
import com.megatim.queryadhoc.exceptions.NotSupportedDataBaseException;
import com.megatim.queryadhoc.model.ConnectionParameter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
 
/**
 *
 * @author ASUS
 */
public class DbConnectionFactory {
 
    private DbConnectionFactory() {
    }
 
    public static Connection createConnection(SgbdType sgbdType, ConnectionParameter connectionParameter) throws NotSupportedDataBaseException, ConnectionParameterException, ConnectionException {
 
        if (sgbdType == null) {
            throw new IllegalArgumentException("Le SGBD ne peut ĂȘtre nul");
        }
 
        switch (sgbdType) {
            case ORACLE:
                return createOracleConnection(connectionParameter);
            case MYSQL:
                return createMySqlConnection(connectionParameter);
            case SQLSERVER:
                return createSqlServerConnection(connectionParameter);
            case POSTGRESQL:
                return createPostgresConnection(connectionParameter);
            default:
                throw new NotSupportedDataBaseException(sgbdType.name());
        }
    }
 
    public static void closeConnection(Connection connection) throws ConnectionException {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(DbConnectionFactory.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
            throw new ConnectionException();
        }
    }
 
    private static Connection createOracleConnection(ConnectionParameter connectionParameter) throws ConnectionException {
        try {
            Class.forName("oracle.jdbc.OracleDriver");
            String url = "jdbc:oracle:thin:@//" + connectionParameter.getIpAddress() + ":" + connectionParameter.getPort() + "/" + connectionParameter.getDatabaseName();
 
            Connection connection = connection(url, connectionParameter.getUserName(), connectionParameter.getPassword());
 
            // Check if the connection is not successful
            if (connection == null) {
                throw new ConnectionException(SgbdType.POSTGRESQL, connectionParameter.getDatabaseName());
            }
            return connection;
        } catch (SQLException | ClassNotFoundException ex) {
            throw new ConnectionException(SgbdType.ORACLE, connectionParameter.getDatabaseName());
        }
    }
 
    private static Connection createSqlServerConnection(ConnectionParameter connectionParameter) throws ConnectionException {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url = "jdbc:sqlserver://" + connectionParameter.getIpAddress() + ":" + connectionParameter.getPort() + ";databaseName=" + connectionParameter.getDatabaseName()
                    + ";encrypt=false;trustServerCertificate=false;";
 
            Connection connection = connection(url, connectionParameter.getUserName(), connectionParameter.getPassword());
 
            // Check if the connection is not successful
            if (connection == null) {
                throw new ConnectionException(SgbdType.POSTGRESQL, connectionParameter.getDatabaseName());
            }
            return connection;
        } catch (SQLException | ClassNotFoundException ex) {
            throw new ConnectionException(SgbdType.SQLSERVER, connectionParameter.getDatabaseName());
        }
    }
 
    private static Connection createMySqlConnection(ConnectionParameter connectionParameter) throws ConnectionException {
 
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://" + connectionParameter.getIpAddress() + ":" + connectionParameter.getPort() + "/" + connectionParameter.getDatabaseName() + "?useSSL=false";
 
            Connection connection = connection(url, connectionParameter.getUserName(), connectionParameter.getPassword());
 
            // Check if the connection is not successful
            if (connection == null) {
                throw new ConnectionException(SgbdType.POSTGRESQL, connectionParameter.getDatabaseName());
            }
            return connection;
        } catch (SQLException | ClassNotFoundException ex) {
            throw new ConnectionException(SgbdType.MYSQL, connectionParameter.getDatabaseName());
        }
    }
 
    private static Connection createPostgresConnection(ConnectionParameter connectionParameter) throws ConnectionException {
        try {
            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://" + connectionParameter.getIpAddress() + ":" + connectionParameter.getPort() + "/" + connectionParameter.getDatabaseName();
 
            Connection connection = connection(url, connectionParameter.getUserName(), connectionParameter.getPassword());
 
            // Check if the connection is not successful
            if (connection == null) {
                throw new ConnectionException(SgbdType.POSTGRESQL, connectionParameter.getDatabaseName());
            }
            return connection;
        } catch (SQLException | ClassNotFoundException ex) {
            throw new ConnectionException(SgbdType.POSTGRESQL, connectionParameter.getDatabaseName());
        }
    }
 
    private static Connection connection(String url, String userName, String password) throws ConnectionException, SQLException {
        // Create the connection
        return DriverManager.getConnection(url, userName, password);
    }
}