bonjour,
je développe une petite appli en java qui a besoin de visualiser certaines infos stockées dans une BD MS Access. j'ai créé donc une classe qui me permet de me connecter à une base de donnée choisie par l'utilisateur... Voici le code de ma classe :
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
| public class Connection_Agent {
public String DBPath; //String that stores the database path
public Connection connection;
public Connection_Agent(String DBPath) {
this.DBPath=DBPath;
}
/*
* This method is used to connect to the database whose path is given in the attribute DBPath
*/
public boolean connect() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e1) {
System.out.println("Error : Problem with the ODBC Driver. "+e1.getMessage());
return false;
}
String connectionstring="jdbc:odbc:Driver={Microsoft Access Driver(*.mdb)};DBQ="+DBPath;
try {
this.connection=DriverManager.getConnection(connectionstring);
} catch (SQLException e) {
System.out.println("Error : Unable to connect to the Database. "+e.getMessage());
return false;
}
return true;
}
/*
* This method is used to close the connection
*/
public boolean disconnect(){
try {
this.connection.close();
} catch (SQLException e) {
System.out.println("Error : Unable to close the connection");
}
return true;
}
} |
voici ma classe de test qui me permet de voir si tout marche correctement :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
public class TestClass {
/**
* @param args
*/
public static void main(String[] args) {
String path="C:/Data/meavy/workspace/db1.mdb";
Connection_Agent connection = new Connection_Agent(path);
System.out.println("Connexion créée?"+connection.connect());
}
} |
J'ai en fait l'exception suivante :
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
pourriez vous m'aider à y voir plus clair ? Merci.
Partager