做了个jdbc连接access,需要数据库的绝对路径,我要动态获得,请问要怎么写?package com.zjm.cctw66.tools;import java.sql.*;   
import java.io.*;   public class ConnDB {
public static Connection conn;
private static String strurl = null;

public static void CreateConn() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=pmis.mdb";
conn=DriverManager.getConnection(strurl);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static Connection getConn() {
try {
if(conn == null || conn.isClosed()) {
CreateConn();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return conn;
}
}
放到tomcat下运行,要怎么得到绝对路径啊,因为这就是一个普通的类文件,没有request,我不会写了

解决方案 »

  1.   

    如果是web工程一定是通过request获得的
      

  2.   

    好像是通过request.getContextPath()得到吧。。
      

  3.   

    如果是web工程一定是通过request获得的,没有request是不可能获取的,web只能通过这个对象来对话本地信息
      

  4.   

    File 中有个方法获得该类的真实路径,在api中查一下啊
      

  5.   

    感觉是可以的,他这里是jdbc-odbc桥的方式,而不是数据源的方式。
    如果那里能建立起连接,那么pmis.mdb应该就是跟这个class一个目录,也就可以获取了。
      

  6.   

    操作io流中 的file中有个方法吧
      

  7.   

    request.getContextPath() 获取工程名字
      

  8.   

    request当做参数一层一层的传递进来
      

  9.   

    谢谢这个this.getClass().getResource("/").getFile();
      

  10.   

    谢谢这个 this.getClass().getResource("/").getFile();
    正解,其他的都可能无效
      

  11.   


    这个对头 通过这个获得你当前类所在的位置 然后去寻找数据库文件也可以的 刚想回答让人抢了..另外 通过request获得是用 getRealPath方法吧.. 获得绝对路径