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
|
/**
* Nom du champ de la classe du Driver dans le fichier de properties.
*/
private static final String JDBC_DRIVER_PROPERTY = "database.driver.classname";
/**
* Nom du champ de l'URL de la base de donnée dans le fichier de properties.
*/
private static final String JDBC_URL_PROPERTY = "database.driver.url";
/**
* Nom du champ de l'utilisateur de la base de donnée dans le fichier de properties.
*/
private static final String JDBC_USER_PROPERTY = "database.user";
/**
* Nom du champ du mot de passe de la base de donnée dans le fichier de properties.
*/
private static final String JDBC_PASSWORD_PROPERTY = "database.password";
/**
*Nom du champ de l'URL du serveur de base de donnée dans le fichier de properties.
*/
private static final String JDBC_URL_SERVER = "database.url.server";
public Connection getConnection() throws SQLException, FileNotFoundException, IOException {
if (connect != null)
return connect;
if (driverManager == null) {
loadProperties();
try {
driverManager = Class.forName(driverName).newInstance();
} catch (ClassNotFoundException e) {
throw new SQLException();
} catch (InstantiationException e) {
throw new SQLException();
} catch (IllegalAccessException e) {
throw new SQLException();
}
}
if (driverManager == null) {
throw new SQLException("Driver Manager null");
}
try {
connect = DriverManager.getConnection(url, user, pwd);
if (connect.isClosed()) {
return null;
}
} catch (SQLException e) {
if (e.getErrorCode() == 1049) {
connect = DriverManager.getConnection(urlServeur, "root",
"secret");
createDB();
} else {
throw new SQLException("Connection impossible");
}
}
return connect;
}
private void loadProperties() throws SQLException, FileNotFoundException, IOException {
PropertyResourceBundle resource = new PropertyResourceBundle(new FileInputStream("/home/jgislard/workspace/chrono/src/com/atosorigin/toulon/chrono/ressources/jdbcCHRONO.properties"));
if (resource == null) {
StringBuffer strException =
new StringBuffer("Impossible de récupérer le fichier ");
strException.append(JdbcCHRONO.JDBC_CONFIG_FILE);
strException.append(".properties dans le classpath");
throw new SQLException(strException.toString());
}
//On récupère les paramètres
try {
this.driverName = resource.getString(JDBC_DRIVER_PROPERTY);
this.url = resource.getString(JDBC_URL_PROPERTY);
this.user = resource.getString(JDBC_USER_PROPERTY);
this.pwd = resource.getString(JDBC_PASSWORD_PROPERTY);
this.urlServeur=resource.getString(JDBC_URL_SERVER);
} catch (MissingResourceException mre) {
throw new SQLException("Problèmes de récupération des paramètres JDBC");
}
} |
Partager