0

I've been unable to connect my Java program to my MS Access Database. The database file is in my Netbeans project folder under the name PhonesDatabase.accdb . This is the error I am receiving:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Any feedback would be appreciated. The following is my current code:

import java.sql.*;

public class DB {

private static Connection conn = null;

public DB() {
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        conn = DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb,         *.accdb)};DBQ=" + System.getProperty("user.dir") + "\\PhonesDatabase.accdb");
    } catch (Exception ex) {
        System.out.println("Error");
    }


}

ResultSet queryTbl (String sqlStmt) throws SQLException
{
Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(sqlStmt);
return rs;
}//end queryTbl

void updateTbl (String update) throws SQLException
{
Statement stmt = conn.createStatement();
stmt.executeUpdate(update);
stmt.close();
}//end updateTbl
}//end DB class

Thanks NF

Gord Thompson
  • 98,607
  • 26
  • 164
  • 342
Nik F
  • 71
  • 1
  • 11
  • Where are you getting the error? – Nahuel Ianni Oct 07 '14 at 18:44
  • public DB() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + System.getProperty("user.dir") + "\\PhonesDatabase.accdb"); } catch (Exception ex) { System.out.println("Error"); } } – Nik F Oct 07 '14 at 18:56
  • i.e. my catch statement returns "Error" – Nik F Oct 07 '14 at 18:57
  • What does `System.getProperty("java.version")` return? – Gord Thompson Oct 07 '14 at 19:34
  • I solved the issue. JDK 8 doesn't support that database driver. If anyone else had a similar issue, you must down grade to JDK 7. Thanks for the help – Nik F Oct 08 '14 at 07:28
  • possible duplicate of [Manipulating an Access database from Java without ODBC](http://stackoverflow.com/questions/21955256/manipulating-an-access-database-from-java-without-odbc) – Gord Thompson Oct 08 '14 at 09:07

0 Answers0