java access相对路径_java中Access数据库相对路径的获得

/**

*@Title:AccessPathTest

*@Description:新建一个数据库,放在工程(MyCode)目录下面的db目录下面,然后利用数据库相对路径测试访问。数据库中有一张user表,含有userName和userPswd两个字段,存储有一条记录admin,admin。

*@Date&&Time:2007-8-3上午07:52:43

*@Author:明♂Cheung

*@E-mail:piaobo06@163.com

*@Blog:http://hi.baidu.com/cheung_ming

*/

//本例采用Eclipse 3.2编译平台测试通过。

package com.cheung;

import java.sql.*;

public class AccessPathTest {

public static void main(String[] args) {

Connection conn = null;

Statement stmt = null;

ResultSet result = null;

//

数据库驱动,JDK中自带的有,所以不必象其它数据库驱动一样拷贝到lib目录下

String strDriver = "sun.jdbc.odbc.JdbcOdbcDriver";

// 取得用户文件的绝对路径

String strFilePath = System.getProperty("user.dir").replace('\\',

'/');

// System.out.println(strFilePath);

// 用户文件绝对路径加上数据库的路径既为数据库的相对路径

String strDataBasePath = strFilePath + "/db/mytest.mdb";

// System.out.println(strDataBasePath);

String strURL = "jdbc:odbc:driver={Microsoft Access Driver

(*.mdb)};DBQ="

+ strDataBasePath;

// System.out.println(strURL);

try {

Class.forName(strDriver);

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

try {

conn = DriverManager.getConnection(strURL);

} catch (SQLException e) {

e.printStackTrace();

}

try {

stmt = conn.createStatement();

String sql = "select * from user";

result = stmt.executeQuery(sql);

while (result.next()) {

System.out.println("userName=" +

result.getString("userName"));

System.out.println("userPswd=" +

result.getString("userPswd"));

}

System.out.println("Good Luck---数据库连接成功");

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

if (result != null) {

result.close();

}

} catch (SQLException e1) {

e1.printStackTrace();

}

try {

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值