0

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?

ikhebgeenaccount
  • 353
  • 6
  • 20

1 Answers1

3

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;";
Thirumalai Parthasarathi
  • 4,541
  • 1
  • 25
  • 43
  • You're right. Thank you for this tip! See my edit for the error I got. – ikhebgeenaccount May 09 '14 at 11:12
  • This helped, I get another error however... It is saying: `Exception: [Microsoft][ODBC Microsoft Access Driver] Not a valid file name.` Does this mean that the driver I use doesn't exist? – ikhebgeenaccount May 09 '14 at 11:20
  • 1
    @ikhebgeenaccount (btw: please change your username), more likely you need to specify the full path to the database file. – Mark Rotteveel May 09 '14 at 11:33
  • @MarkRotteveel But when I export my project and I put the database in the jar and move the jar, the path isn't right anymore... That wouldn't be very useful. – ikhebgeenaccount May 09 '14 at 11:39
  • @ikhebgeenaccount Did you check your DSN – Thirumalai Parthasarathi May 09 '14 at 11:46
  • @BlackPanther Yes, I did. It seemed fine... I was looking at this [link](http://stackoverflow.com/questions/778187/getting-directory-path-to-class-file-containing-main/778246#778246), I thought this could help me solve the path issue. – ikhebgeenaccount May 09 '14 at 11:52
  • @MarkRotteveel I think the file path won't be an issue, if the .accdb file is in the same directory as the java class file. – Thirumalai Parthasarathi May 09 '14 at 11:54
  • @ikhebgeenaccount That is to create an .mdb file. I think [**this**](http://stackoverflow.com/questions/14073581/database-extension-is-accdb-when-i-want-it-to-be-mdb) will help you better – Thirumalai Parthasarathi May 09 '14 at 11:55
  • 2
    @ikhebgeenaccount Putting the database in the jar will make it entirely inaccessible. And my suggestion was as a starting point for troubleshooting the issue. Eg: if it works with a fixed path, then you simply need to (for example) locate the database file and create the connection string at runtime, or find out which relative path will work. – Mark Rotteveel May 09 '14 at 11:58
  • @BlackPanther I am not 100% sure about the rules used to locate the file from ODBC, it could also be from the root of the application or IDE project (not necessarily the same as the class file location) – Mark Rotteveel May 09 '14 at 11:59
  • @ikhebgeenaccount Mark is right.. you should rethink about putting the database file in the jar. You can try to extract the database file in from the jar and place it at a predefined place and use it. – Thirumalai Parthasarathi May 09 '14 at 12:00
  • @MarkRotteveel So, I tried the path-thingy, but when I insert the path: `DBQ=D:\Users\Lars\Desktop\ratingdb.accdb;`, I get this error: `Unresolved compilation problem: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )`. – ikhebgeenaccount May 09 '14 at 12:02
  • 2
    @ikhebgeenaccount it simply means you have to escape your backslashes.. that is D:\\Users\\Lars and so on – Thirumalai Parthasarathi May 09 '14 at 12:09
  • @BlackPanther The path worked, when escaping the backslashes. Thank you for your help, I will now try to find a way of finding the path to the main class (which I found [here](http://stackoverflow.com/questions/778187/getting-directory-path-to-class-file-containing-main/778246#778246). – ikhebgeenaccount May 09 '14 at 12:11
  • re: the last sample connection string in your answer: `Microsoft Access Driver ( *.accdb)` is not a valid ODBC driver name. The correct name is `Microsoft Access Driver (*.mdb, *.accdb)`for the newer Access Database Engine ODBC driver. – Gord Thompson May 09 '14 at 14:18