Domanda

I am trying to create a program that will read a database and output it. I have followed the tutorial at http://www.codeproject.com/Articles/35018/Access-MS-Access-Databases-from-Java. But when I run my program, nothing happens. Not even errors...

I probably have missed something, but I don't know what it could be. Here's my code:

import java.sql.*;

public class ReadDB {

public static void ReadDB() {

    try{
        //Source: http://www.codeproject.com/Articles/35018/Access-MS-Access-Databases-from-Java
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=ratingdb.accdb;";
        Connection conn = DriverManager.getConnection(database, "", "");
        Statement s = conn.createStatement();

        //Read Table
        String selTable = "SELECT * FROM RATINGS";
        s.execute(selTable);
        ResultSet rs = s.getResultSet();
        while((rs!=null) && (rs.next()))
        {
           System.out.println(rs.getString(1) + " : " + rs.getString(2));
        }




        s.close();
        conn.close();

    } catch(Exception e){
        System.out.println ("Unable to connect to the database");
        System.out.println ("Exception: " + e.getMessage());
    }



}

} After Black Panther's comment, I got this error: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0x13f4 Thread 0x1204 DBC 0x42170d4

EDIT: A new error occured: Exception: [Microsoft][ODBC Microsoft Access Driver] Not a valid file name. Does this mean that the driver I use does not exist?

È stato utile?

Soluzione

Even if it throws the exceptions you wouldn't be able to see them because you have handled the errors in a wrong way. You shouldn't just catch the exceptions and move on. That defies the whole concept of exception handling.

You should do something like this for your exceptions to be printed in the console.

try {

  //code that throws exceptions

} catch(Exception e) {
  e.printStackTrace(); //prints the error to the console and you missed it
}

EDIT: It seems you are having some permissions issue.

An Excerpt from the site http://support.sas.com/kb/40/228.html

This problem occurs for several reasons, including not having permissions on an ODBC registry key. In such a case, change the permissions on the registry key as follows:

Start the registry editor using the regedit command: select Start ► Run and enter regedit.

If your SAS PC Files Server is on a 64-bit machine, expand the following key: HKEY_LOCAL_MACHINE ► SOFTWARE ► WOW6432NODE ► ODBC.

If your SAS PC Files Server is on a 32-bit machine, expand the following key: HKEY_LOCAL_MACHINE ► SOFTWARE ► ODBC.

Right-click the ODBC folder and select Permissions.

Make sure that the logon ID that is running the SAS process has full control. The problem might also occur due to older ODBC drivers from Microsoft, particularly from Office 2007. To install the newer ODBC drivers, go to Microsoft Access Database Engine 2010 Redistributable.

If you have 32-bit Microsoft Office, download the AccessDatabaseEngine.exe file. Download the other ODBC driver only if you have 64-bit version of Microsoft Office.

If you are trying to create an .mdb file and use it then do this

go to File -> Options -> General, and set the Default File Format to Access 2002-2003

And change your database URL to

"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=ratingdb.mdb;";

to use the .mdb file.

To use an .accdb file try doing this,

"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=ratingdb.accdb;";
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top