-2

我正在将 java 连接到 Microsoft 访问数据库,但出现以下异常

java.sql.SQLException: [Microsoft][ODBC Driver Manager] 未找到数据源名称且未指定默认驱动程序

try{
     String ProjectPath= System.getProperties().getProperty("user.dir");
     System.out.println(ProjectPath);
     String path,fullstring;
     path=ProjectPath+"\\data.mdb";
     fullstring="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" +path;
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     Connection con=DriverManager.getConnection(fullstring);
     System.out.println("Connected");
 }catch(Exception e){

     System.out.println("Connected Error: "+ e);
     }

我该如何解决我的问题?

4

3 回答 3

1

{Microsoft Access Driver (*.mdb)}是旧版 Microsoft Jet 驱动程序的名称,它只适用于 32 位应用程序。(没有 64 位版本的 Jet 数据库引擎或 Jet ODBC 驱动程序。)

要从 64 位应用程序连接.mdb和文件,您需要从此处.accdb下载并安装 64 位版本的 Access 数据库引擎(又名“ACE”),然后使用驱动程序名称在您的应用程序中引用它。{Microsoft Access Driver (*.mdb, *.accdb)}

于 2013-10-19T13:11:09.783 回答
0
    import java.sql.*;
    class dbTst {
public static void main(String args[]) throws Exception{
    try{


    //Driver for JDBC-ODBC Bridge
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    //Establishing the Connection through DSN
    Connection con= DriverManager.getConnection("jdbc:odbc:db","","");

    //Creating the Statement Object
    Statement st=con.createStatement();


    ResultSet rs=st.executeQuery("Select * from aman");

    while(rs.next()==true){

    System.out.println(rs.getString("name")+" - " + rs.getString("basic"));
    }

    rs.close();
    st.close();
    con.close();
    }
            catch(Exception ee){
                System.out.println(ee.getMessage());
    }
}
}
于 2013-10-20T09:30:51.423 回答
-1

我认为你想以某种奇怪的方式联系..

http://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html

代码(使用 DriverManager):

public Connection getConnection() throws SQLException {
    Connection conn = null;
    Properties connectionProps = new Properties();
    connectionProps.put("user", this.userName);
    connectionProps.put("password", this.password);

    if (this.dbms.equals("mysql")) {
        conn = DriverManager.getConnection(
               "jdbc:" + this.dbms + "://" +
               this.serverName +
               ":" + this.portNumber + "/",
               connectionProps);
    } else if (this.dbms.equals("derby")) {
        conn = DriverManager.getConnection(
               "jdbc:" + this.dbms + ":" +
               this.dbName +
               ";create=true",
               connectionProps);
    }
    System.out.println("Connected to database");
    return conn;
}
于 2013-10-18T08:12:14.587 回答