Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class.forName still seems necessary

Tags:

java

jar

jdbc

From the Java documentation

In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method Class.forName.

Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)

I have a jersey Webservice which connects to SQL Server Express 2016. It has sqljdbc42.jar which is 4.2 driver, in the CLASSPATH

However, if I omit the Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver") call, my DriverManager.getConnection throws a SQLException(No suitable driver found for jdbc:sqlserver://localhost:1433; ....")

The getConnection starts succeeding once I add the Class.forName call.

I am on Java 8.

What am I missing?

UPDATE: I just tried a command line program and it works without the forName. However, from my Eclipse IDE where I am running my REST service as a Tomcat 8.0 Server on localhost, it doesn't work.

like image 841
user93353 Avatar asked Jun 17 '16 08:06

user93353


People also ask

Is class forName necessary?

forName is no longer required. Just put any JDBC 4. x driver in the project classpath, and Java is able to detect it.

Why do we use class forName?

The Class. forName is used to load any given class (within double quotes as String) at run time. For example, when we use IDE, we see there will be a GUI builder which allows us to drag and drop the buttons, text fields, etc. This drag and drop mechanism internally requires certain classes to be loaded at run time.

What is the role of class forName () in JDBC?

The forName() method dynamically loads a Java class at runtime. When an application calls the forName() method, the Java Virtual Machine (JVM) attempts to find the compiled form (the bytecode) that implements the requested class. The EDB Postgres Advanced Server JDBC driver is named com.

What is the role of class forName while loading drivers?

It is used to create an instance of a driver and register it with the DriverManager. When you have loaded a driver, it is available for making a connection with a DBMS.


1 Answers

The drivers are automatically initialized when the class DriverManager is itself initialized thanks to SPI (Service Provider Interface). Which means that internally it will try to find any file META-INF/services/java.sql.Driver available in the context class loader and for each file found, it will create an instance of the class that is defined in the file which in this case is actually the FQN of the JDBC driver, this is how JDBC drivers are automatically initialized starting from JDBC 4.0.

But this can only work, if your driver is available from the context class loader while initializing the class DriverManager. A good way to ensure that is to make your driver available from a Class Loader high enough in the hierarchy. In your case you should put your driver in tomcat/lib. Indeed, this way your driver will be available from the Common CL which should be high enough. More details about the CL hierarchy in Tomcat here.

like image 167
Nicolas Filotto Avatar answered Oct 09 '22 01:10

Nicolas Filotto