如何在Java中为数据库提供相对路径 [英] How to give a relative path in Java for database
问题描述
我想问一下如何在java中为数据库提供相对路径(ms访问),这样当我将项目放在其他驱动器中时,就不必编辑路径部分了.
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"** );
但是如果我将项目更改为另一个文件夹,请假设d:则必须像下面这样编辑此路径部分:
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"** );
我想给出一个相对路径,以便我的项目可以在任何具有此路径的驱动器上运行 \ project \ a.mdb
I want give a relative path so that my project will run on any drive with this \project\a.mdb
推荐答案
好吧,这就是我们所说的 parameterize !只需将路径作为参数,然后在运行时将其传递进来.这是一个演示:
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
这篇关于如何在Java中为数据库提供相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!