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
|
package com.lamelcolor;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
public class Database {
Connection conn = null;
private String err="";
private boolean statut = false;
/** Creates a new instance of Database */
public Database() {
System.out.println("connection");
this.initConnection();
}
/** Créer une connexion à partir des propriétés spécifiées dans le fichier database.properties **/
public void initConnection() {
// Load the JDBC-ODBC bridge driver
try {
Class.forName("com.mysql.jdbc.Driver");
} catch( ClassNotFoundException ee) {
setErr("Impossible de charger le driver JdbcOdbc");
}
Properties props=new Properties();
try {
props.load(new FileInputStream("database.properties"));
String drivers=props.getProperty("jdbc.drivers");
String url=props.getProperty("jdbc.url");
String username=props.getProperty("jdbc.username");
String password=props.getProperty("jdbc.password");
conn = DriverManager.getConnection(url,username,password);
props = null;
if(conn!=null) {
setStatut(true);
}
} catch (FileNotFoundException ex) {
setErr("Impossible de trouver le fichier de properties");
} catch (IOException ioex) {
setErr("Impossible de lire le fichier des propriétés");
} catch (SQLException sqlex) {
setErr("Impossible d'initialiser la connexion à la base de données (utilisateur ou mot de passe erroné)");
}
}
public Connection getConnection() {
return this.getConnection();
}
public void close() {
try {
this.conn.close();
} catch (SQLException sqlex) {
}
}
public boolean isStatut() {
return statut;
}
public void setStatut(boolean statut) {
this.statut = statut;
}
public String getErr() {
return err;
}
public void setErr(String err) {
this.err = err;
}
} |
Partager