java 数据库路径,如何在Java中为数据库提供相对路径

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值