2013-10-18 99 views
-2

我连接Java来Microsoft Access数据库,但我有以下异常Java数据库连接异常

值java.sql.SQLException:[微软] [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); 
    } 

我该如何解决我的问题?

+0

题外话,但你有没有使用JPA?它比JDBC更舒适。看一看,你会喜欢它;) – NiziL

+0

可能的重复[如何使用Java程序连接MS Access数据库?](http://stackoverflow.com/questions/6880879/how-to-connect-ms- access-database-using-java-program) – SpringLearner

+0

您是否尝试过通过IDE进行连接?什么是网址? –

回答

-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; 
} 
+0

虽然这个链接可能回答这个问题,但最好在这里包含答案的基本部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 – justhalf

+1

我添加了代码。 –

+1

也有可能通过例如。 Eclipse透视图称为:数据库透视图。 –

1

{Microsoft Access Driver (*.mdb)}是上了年纪的Microsoft Jet驱动程序的名称,它只能在32位应用程序中使用。 (没有64位版本的Jet数据库引擎或Jet ODBC驱动程序。)

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

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()); 
    } 
} 
} 
+0

它给出同样的例外 [Microsoft] [ODBC驱动程序管理器]未找到数据源名称并且没有指定默认驱动程序 – Sarah