0

This is my code:

public class ConnectDB {
Connection conn;
Scanner kb=new Scanner(System.in);
public String ID;
public ConnectDB()
{
try
     {
        Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
        System.out.println ("Driver successfully loaded");
     }
         catch (ClassNotFoundException c)
        {
           System.out.println ("Unable to load database driver");
        }

  //connect to the database
     try
     {
    String filename = "PATPhase2DB.mdb";
    String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=";
    database += filename.trim () + ";DriverID=22;READONLY=true}";
    conn = DriverManager.getConnection (database,"","");
    System.out.println ("Connection to database successfully established");

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

The Messages Are:
Driver successfully loaded
Unable to connect to the database
java.lang.NullPointerException

This has worked on a different computer than mine, connecting to the database through exactly the same code. I am also running Windows 8.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
user2340779
  • 1
  • 1
  • 1
  • you're capturing an exception - what information does that give? – Chris May 01 '13 at 20:45
  • Which Exception? and the nullointerexcpetion is pointing to the line conn = DriverManager.getConnection (database,"",""); – user2340779 May 01 '13 at 20:58
  • Exception e, can you post that on the bottom of your question – Chris May 01 '13 at 21:03
  • I'm new to java and when we received the coding in class, that's how we got it. The program works on other computers. And i have a 32 bit access. – user2340779 May 01 '13 at 21:11
  • Now it's giving this message Exception: [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0x1d38 Thread 0x13dc DBC 0x1c317114 – user2340779 May 01 '13 at 21:24

4 Answers4

3

This is very common error.

Reason is that some other application is using your database (most commonly you have opened database in Microsoft Access).

Close it and enjoy.

EDIT:
This answer is for error user mentioned in comment(unable to open registry key 'temporary (volatile))

A.s. Bhullar
  • 2,680
  • 2
  • 26
  • 32
0

add this to your code:

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

then you can get a deeper glimpse of what went wrong

Chris
  • 2,955
  • 1
  • 30
  • 43
  • Now it's giving this message Exception: [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0x1d38 Thread 0x13dc DBC 0x1c317114 – user2340779 May 01 '13 at 21:19
  • hmm, seems like the computer you're running it on now down't have the right driver, have a look here: http://answers.microsoft.com/en-us/office/forum/office_2007-access/can-anyone-please-help-re-access-connection-errors/68a6f9eb-ef3a-4291-9ebf-5ce212665283 – Chris May 01 '13 at 21:26
  • Thanks a lot for your help. I didn't seem to be able to resolve the problem – user2340779 May 01 '13 at 21:45
0

Your Driver= name is missing a space. You've defined it as...

Driver={Microsoft Access Driver (*.mdb,*.accdb)};

...but it should be...

Driver={Microsoft Access Driver (*.mdb, *.accdb)};

You also have a stray } at the end of your connection string, but that doesn't seem to hurt anything.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
0

In this case you need to mention the entire path of your access database file and the url between extensions (.mdb, *.accdb) space is mandatory e.g.:

String database="C:/Users/GIRI/Desktop/fdsfkdsfj/abc.accdb";
String url="jdbc:odbc:Driver={Microsoft Access Driver (.mdb, *.accdb)};DBQ=" + database + ";DriverID=22;READONLY=true";
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146