我想连接ACCESS,把下面这段代码写到了ECLIPSE中。但是在appliacation下有红线表示错误,有朋友说是我url的路径错了。如果是这个路径D:\driver\Classenviroment\JDBC\db1.mdb,要怎么写到这个句子中?
                  String url="/Data/ReportDemo.mdb";
String strurl="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+application.getRealPath(url); //application在这

解决方案 »

  1.   

    https://sdlc5b.sun.com/ECom/EComActionServlet/DownloadPage:~:com.sun.sunit.sdlc.content.DownloadPageInfo;jsessionid=FC5E56F9BF4378425B801DAA7AF20E72;jsessionid=FC5E56F9BF4378425B801DAA7AF20E72
    http://dev.mysql.com/get/Downloads/MySQL-5.0/mysql-5.0.51a-linux-i686.tar.gz/from/pick#mirrors
      

  2.   


    import java.sql.*;public class UNSDBConnection 
    {
            //jdbc driver, jdbc-odbc bridge
    String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";

    //the database to be connected
    String dbName = "jdbc:odbc:user";

    //database's user name
    String dbUser = "";

    //database's password
    String dbPass= "";
    /**
     * constructor
     * */
    public UNSDBConnection()
    {
    try
    {
    Class.forName( dbDriver );
    }
    catch( ClassNotFoundException classNotFoundException ) 
    {
    System.err.println( "DBConnection(): " + classNotFoundException.getMessage() );
    }
    }
    }
      

  3.   

    具体点行不?我找到的例子是这两段.
    Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver“) ; 
    String url=“jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=“+application.getRealPath(“/Data/ReportDemo.mdb“); 
    Connection conn = DriverManager.getConnection(url,““,“”); 
    Statement stmtNew=conn.createStatement() ; <%  
    String sitePath = request.getRealPath("/");  
    String dbPath = sitePath+"db1.mdb";  
    String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+dbPath;   
    try   
    {   
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
    out.println("access yes1");   
    Connection conn2=DriverManager.getConnection(strurl);  
    out.println("access yes2");   
    Statement stmt=conn2.createStatement();  
    out.println("access yes3");   
    }   
    catch (Exception e)   
    {   
        out.println(e.getMessage());   
    }   
    %>
      

  4.   

    你找到的代码应该是JSP的代码吧
      

  5.   

    里面的都是JAVA代码...谁来帮下啊?
    如果是这个路径D:\driver\Classenviroment\JDBC\db1.mdb,DBQ后面要怎么写?
      

  6.   


    String   sitePath   =   request.getRealPath("D:/driver/Classenviroment/JDBC/");     
    String   dbPath   =   sitePath+"db1.mdb";     
    String   strurl="jdbc:odbc:driver={Microsoft   Access   Driver   (*.mdb)};DBQ="+dbPath;       
    try       
    {       
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");     
    out.println("access   yes1");       
    Connection   conn2=DriverManager.getConnection(strurl);     
    out.println("access   yes2");       
    Statement   stmt=conn2.createStatement();     
    out.println("access   yes3");       
    }       
    catch   (Exception   e)       
    {       
            out.println(e.getMessage());       
    }       
      

  7.   


    try{
    Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver“) ;
    String url=“jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=“+application.getRealPath(“D:\driver\Classenviroment\JDBC\db1.mdb“);
    System.out.println("before connection");
    Connection conn = DriverManager.getConnection(url,““,“”);
    Statement stmtNew=conn.createStatement() ;
    ResultSet rs=stmt.executeQuery("select * from test");
    if(rs.next())
    {
    System.out.println(rs.getString("id"));
    }
    }catch(Exception e)
    {
    System.out.println(e);
    }
      

  8.   

    你直接把那个数据库的路径放在DBQ=后面就可以了String   strurl="jdbc:odbc:Driver={MicroSoft   Access   Driver   (*.mdb)};DBQ=D:\\driver\\Classenviroment\\JDBC\\db1.mdb";你那应该是路径写得不对。
      

  9.   

    问题出在  application  是 JSP里面的内置对象。。
    如果你直接写在一个类里面  application  下面当然是红色的啦。
    如果直接写成一个类  需要继承 HttpServlet
    application 在servlet中的获得方
    ServletContext application = request.getSession().getServletContext();
    希望对你有所帮助
      

  10.   

    谢谢10楼...application疑问解决.也谢谢大家