Reputation: 4128
I have a Maven console application using JDBC and FirebirdSQL and Java 8, according to the specification, there is not need to add the class registration anymore, so the line Class.forName("org.firebirdsql.jdbc.FBDriver");
is commented, but when I run the Project I get the error: No suitable driver found for jdbc:firebirdsql://localhost/database
, but if a I uncomment the line it Works fine.
The curious thing if that I use a simple console Project w/o using Maven it works with the line commented as the specification says, so the question is: is there a way to getting working with Maven too commenting the line of the class registration?
Upvotes: 1
Views: 3128
Reputation: 14361
It appears you are using an old version of Jaybird (the FirebirdSQL JDBC Driver). Version 2.2 and above implement the JDBC 4.0 spec, which does not require the Class.forName()
syntax.
Thanks to the Java SE Service Provider mechanism included in Mustang, Java developers no longer need to explicitly load JDBC drivers using code like Class.forName() to register a JDBC driver. The DriverManager class takes care of this by automatically locating a suitable driver when the DriverManager.getConnection() method is called.
So, upgrade your Jaybird JDBC driver (link below) and simply leave out the Class.forName()
method. It's legacy, and not needed for JDBC drivers (JDBC version 4.0 and above).
As per the Jaybird documenation, you should use the following in your POM:
<groupId>org.firebirdsql.jdbc</groupId>
<artifactId>jaybird-jdkXX</artifactId>
<version>2.2.9</version>
- The artifactId depends on your target Java version: jaybird-jdk18, jaybird-jdk17, or jaybird-jdk16
http://www.firebirdsql.org/en/jdbc-driver/
http://www.onjava.com/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html
Upvotes: 4
Reputation: 70909
The old JDBC drivers don't use the new Java ServiceLoader framework. This means that the ClassLoader doesn't register the "JDBC Service Provider" because the hooks in META-INF/services/java.sql.Driver
file aren't present as the file doesn't exit.
The line of code you comment out has a "static" block which will get ran upon first instance creation, and the contents of that static block register the JDBC Driver into the DriverManager. That's the "older" way of doing it, and why you need that line for it to work.
Basically, you're trying to use a new technique with old code. Get a new JAR file (if available) and there's a chance it will work with the new technique.
Upvotes: 3