문제

I'm confused about loading database driver for Apache Derby. In it's documentation it is said that:

"If your application runs on JDK 6 or higher, you do not need to explicitly load the EmbeddedDriver. In that environment, the driver loads automatically." https://db.apache.org/derby/docs/10.7/devguide/cdevdvlp40653.html


My connection URL is:

jdbc:derby:testdb;create=true


When I try to connect to Java Derby without loading driver, I always get the following error:

Error: java.sql.SQLException: No suitable driver found for jdbc:derby:testdb


If I load the driver explicitly by adding the following line, the error goes away.

Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();


Now my question is why should I need to load this driver explicitly? I'm running my database application on Eclipse with a reference to JRE7. Why it isn't loaded automatically?

도움이 되었습니까?

해결책

This behavior is the responsibility of the JDK's DriverManager class, not of Derby itself. You can read about the DriverManager class here: http://docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.html

All Derby does is to provide a JDBC driver with the proper META-INF.

I think the DriverManager behavior may depend on the details of your CLASSPATH.

If the derby.jar in the system CLASSPATH, the automatic loading will occur. But if the derby.jar is in an container with a separate classloader, such as servlet containers, application servers, or sophisticated IDEs, aren't in the classes that are searched by DriverManager when it is initialized.

다른 팁

Strange. I do that all the time (but not with Eclipse) without problems. Do you use DriverManger.getConnection() to obtain the connection? I assume that you are using Derby 10.7 or newer?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top