/*
|
* 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);
|
}
|
}
|