Java数据库连接异常

时间:2013-10-18 08:06:48

标签: java exception ms-access jdbc jdbc-odbc

我正在将java连接到Microsoft Access数据库,但我有以下异常

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

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);
     }

如何解决我的问题?

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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());
    }
}
}

答案 2 :(得分:-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;
}