I want ask how to give a relative path in java for a database(ms access) so that when I put my project in other drive then I don't have to edit the path section.
Given below is the absolute path for the database:
con=DriverManager.getConnection( "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};**DBQ=c:\\project\\a.mdb"** );
But if I change my project to another folder, suppose d: then I have to edit this path section like this:
con=DriverManager.getConnection( "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=**d:\\project\\a.mdb"** );
I want give a relative path so that my project will run on any drive with this
\project\a.mdb
解决方案
Well,this is what we called parameterize! Just make the path as parameter,and passed it in on the runtime.Here is a demo:
public class DBOperation {
public static void main(String[] args) {
String path=args[0];
String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};**DBQ="+path+"**)";
...
}
}
And run the programme by:
java DBOperation c:\project\a.mdb