I have tried to make assets folder in different locations in the project and I have MS Access DB file in it. I want to access it using ucanaccess driver. but it gives file not found exception.
DB connectivity code.
public static Statement getConnectionStatement(){
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
String url = "jdbc:ucanaccess://.\assets\BC190201004.accdb";
connection = DriverManager.getConnection(url);
return connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return null;
}
}
but with this code, it is working fine
String url = "jdbc:ucanaccess://C:\Users\kha33\NetBeansProjects\CS506-Assignment-3\src\main\webapp\WEB-INF\assets\BC190201004.accdb";
I tried to put assets folder under the project main folder, java folder, webapp folder and WEB-INF folder.
I also tried in NetBeans IDE.
Advertisement
Answer
The path .assetsBC190201004.accdb
you use to access your file is relative to the working directory of Tomcat’s process. That is usually $CATALINA_BASE
, but can be anything (especially under an IDE).
You should give the path in a more stable way, e.g. by using ServletContext#getRealPath()
:
public static Statement getConnectionStatement(ServletContext ctx){
final String realPath = ctx.getRealPath("/WEB-INF/assets/BC190201004.accdb");
final String url = "jdbc:ucanaccess://" + realPath;
}
Obviously this will fail if your Tomcat does not unpack WARs. However, that is not a common situation.