I’m trying to save a Java project in Eclipse as a jar file.
As I’m working with an Access database and I should export everything, I decided to include the database file in the same folder as Main.class and SingletonConnection.class (which is the class that manages the connection with the database).
So the code is:
private SingletonConnection()throws ConnessioneException{
idConnection = "root";
passConnection = "";
String slash="\\";
String path=this.getClass().getResource("").getPath().replaceFirst("^.*:", "").replaceFirst("!.*$", "").replace("/", slash.concat("\\"));
System.out.println("path è "+path);
driverConnection = "sun.jdbc.odbc.JdbcOdbcDriver";
stringConnection = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C:"+path+"eventi.mdb";
try {
Class.forName(driverConnection);
conn = DriverManager.getConnection(stringConnection,idConnection,passConnection);
} catch (Exception e) {
e.printStackTrace();
throw new ConnessioneException();
}
}
public static Connection getInstance()throws ConnessioneException{
if(conn==null)
new SingletonConnection();
return conn;
}
In Eclipse everything is ok. The project works and no exceptions, but when I try to export the project as a Runnable Jar File or a Jar File it always returns ConnessioneException =null so the connection to the db fails.
Exception is given in the method getInstance, line “new SingletonConnection()”
I need to run that program on other PCs, so I need to solve that. I cannot continue using Eclipse.
SOLVED : maybe it can be useful for someone: