This discussion is archived
1 Reply Latest reply: Nov 3, 2012 9:00 AM by jschellSomeoneStoleMyAlias RSS

Cannot connect to an Access 2007 Database (mdb)

936354 Newbie
Currently Being Moderated
I copy the code from the web that I want to connect to an Access 2007 database to Java
x86 office and x64 windows
public class Database {
    public Database()
    {
         connectDB();
    }
    
    boolean connectDB()
    {
        try
        {
            
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

 
        String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=patient.mdb;";
        Connection conn = DriverManager.getConnection(database, "", "");
        PreparedStatement pst;
        String str = "select * from patient p, hospital h where p.patient_id = h.patient_id"
                    + "ORDER BY p.patient_id";
           
          
            pst = conn.prepareStatement(str);
            
            
            pst.executeQuery(); 
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        
        return true;
    }
}
the patient.mdb file is located in the same folder as the class files.

There are some messy codes in the stack trace.
java.sql.SQLException: [Microsoft][ODBC ?X?ʵ{???޲z??] ?䤣???ƨӷ??W?٥B????w?w?]???X?ʵ{??
     at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
     at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
     at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
     at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
     at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
     at java.sql.DriverManager.getConnection(DriverManager.java:579)
Here is where it happened
 private static Connection getConnection(
        String url, java.util.Properties info, ClassLoader callerCL) throws SQLException {
        /*
         * When callerCl is null, we should check the application's
         * (which is invoking this class indirectly)
         * classloader, so that the JDBC driver class outside rt.jar
         * can be loaded from here.
         */
        synchronized(DriverManager.class) {
          // synchronize loading of the correct classloader.
          if(callerCL == null) {
              callerCL = Thread.currentThread().getContextClassLoader();
           }
        }

        if(url == null) {
            throw new SQLException("The url cannot be null", "08001");
        }

        println("DriverManager.getConnection(\"" + url + "\")");

        // Walk through the loaded registeredDrivers attempting to make a connection.
        // Remember the first exception that gets raised so we can reraise it.
        SQLException reason = null;

        for(DriverInfo aDriver : registeredDrivers) {
            // If the caller does not have permission to load the driver then
            // skip it.
            if(isDriverAllowed(aDriver.driver, callerCL)) {
                try {
                    println("    trying " + aDriver.driver.getClass().getName());
                    Connection con = aDriver.driver.connect(url, info); <<<<<<<<<<<<<<<<<<<<<<<here
                    if (con != null) {
                        // Success!
                        println("getConnection returning " + aDriver.driver.getClass().getName());
                        return (con);
                    }
                } catch (SQLException ex) {
                    if (reason == null) {
                        reason = ex;
                    }
                }

            } else {
                println("    skipping: " + aDriver.getClass().getName());
            }

        }

        // if we got here nobody could connect.
        if (reason != null)    {
            println("getConnection failed: " + reason);
            throw reason;
        }

        println("getConnection: no suitable driver found for "+ url);
        throw new SQLException("No suitable driver found for "+ url, "08001");
    }
Thanks.

Note that I have installed the AccessDatabaseEngine.exe file
I have created a DSN entry. Strange

Edited by: 933351 on Oct 31, 2012 6:58 PM

Edited by: 933351 on Oct 31, 2012 7:35 PM

Edited by: 933351 on Oct 31, 2012 8:22 PM

Edited by: 933351 on Oct 31, 2012 8:24 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points