UcanaccessDriver throws ExceptionInInitializerErro

2019-09-06 20:46发布

问题:

I checked everywhere i could and including this page but i keep getting weird error on Netbeans 8 and Ucanaccess.

Here is what i did first. Imported org-apache-commons-logging.jar, ucanaccess-2.0.9.jar, commons-lang-2.6.jar, hsqldb.jar, jackcess-2.0.4.jar as a new driver.

Set the class path under options in Netbeans (added the ucanaccess.zip file)

import sun.jdbc.odbc.JdbcOdbcDriver;
import net.ucanaccess.jdbc.UcanaccessDriver;
import java.sql.*;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaApplication7 {

static final String JDBC_DRIVER = "net.ucanaccess.jdbc.UcanaccessDriver";
static final String DB_URL = "jdbc:ucanaccess://C:\\Users\\Magneto\\Documents\\Database1.mdb";
static final String USER = "";
static final String PASS = "";
static final Scanner in = new Scanner(System.in);
//insert data variables
public JavaApplication7() throws SQLException {
}
public static void main(String[] args){

// databaseForm dbf = new databaseForm();
// dbf.setVisible(true);
 getDatabase();
}

public static void connectToDatabase()
{
try{
System.out.println("Connection Successfull");
System.out.println("----------------------------------------------");
}
catch(Exception e)
{

}
}
public static void getDatabase()
{
try {
Class.forName(JDBC_DRIVER);
Statement statement = null;
Connection con = DriverManager.getConnection(DB_URL, "", "");
//execute a query
statement = con.createStatement();
String sql = "Select id, FirstName, LastName FROM PhoneBook";
ResultSet rs = statement.executeQuery(sql);
//pull database
while(rs.next())
{
int id = rs.getInt("id");
String FirstName = rs.getString("FirstName");
String LastName = rs.getString("LastName");

System.out.println("id " + id + " FirstName " + FirstName + " LastName " + LastName);   
}
rs.close();
con.close();
} catch (Exception ex) {
System.out.println("Got an Exception!");
System.err.println(ex.getMessage());
}
}

And here is the error I keep getting:

Exception in thread "main" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at javaapplication7.JavaApplication7.getDatabase(JavaApplication7.java:54)
at javaapplication7.JavaApplication7.main(JavaApplication7.java:34)
Caused by: java.lang.RuntimeException: org.hsqldb.jdbc.JDBCDriver
at net.ucanaccess.jdbc.UcanaccessDriver.<clinit>(UcanaccessDriver.java:56)
... 4 more

Java Result: 1

回答1:

The exception originates in the class initializer of net.ucanaccess.jdbc.UcanaccessDriver:

Caused by: java.lang.RuntimeException: org.hsqldb.jdbc.JDBCDriver

According to the UCanAccess site, UCanAccess depends on HSQLDBC:

Dependencies

UCanAccess 2.x.x requires (at least) the following dependencies in your classpath:

  • jackcess-2.0.0.jar or later
    • commons-lang-2.4.jar
    • commons-logging-1.0.4.jar
  • hsqldb.jar(2.2.5)

You probably haven't included the HSQLDB library on the classpath of the application. Defining a driver in Netbeans is purely for use of the driver in Netbeans, not for the classpath of an application.

As mentioned by Gord Thompson in the comment below, an explanation of setting up UCanAccess can be found in this question: Manipulating an Access database from Java without ODBC