Home > Blockchain >  Class.forName("org.sqlite.JDBC") vs JDBC.PREFIX to load JDBC class
Class.forName("org.sqlite.JDBC") vs JDBC.PREFIX to load JDBC class

Time:02-26

We had recently moved to building projects using Maven. Earlier, all the JARs were added in libs folder and added to classpath. While moving to the Maven build, I missed to add a dependency to the 'sqlite-jdbc'. This library is required to read data from a local .db3 file.

As the 'org.sqlite.JBDC' class was loaded by calling the code Class.forName("org.sqlite.JDBC"), there was no compilation error and I deployed the WAR file and servlet failed in the server. I was thinking of a way to find the issue at the compilation time itself to avoid any such mistakes in the future. Can I simply call the JDBC.PREFIX to load the JDBC, so that, If I forget to add the dependency to the pom.xml file, I can find the issue at the compile time, itself?

Is there was difference between Class.forName("org.sqlite.JDBC") vs JDBC.PREFIX to load JDBC class?

CodePudding user response:

No need for Class.forName

There is no generally no need to call Class.forName.

Such calls were used in the early years. Modern Java was changed so that JDBC drivers are automatically loaded and registered with the JVM via the Java Service Provider Interface (SPI) facility.

If you are using books or tutorials advising Class.forName, you may want to obtain more up-to-date learning materials.

DataSource

Furthermore, in Servlet work you generally should not be explicitly accessing the JDBC driver.

Setting the database server address, username, and password would require hard-coding text. When the deployment sysadmins change the IP address, or rotate passwords, your code breaks. You would then have to modify your source code, re-compile, and re-deploy.

Instead, you should externalize such configuration details.

JNDI

You can externalize database configuration by using the the DataSource interface. After obtaining a DataSource object at runtime, make database connections by calling its getConnection method. That DataSource object holds the database server address, username, password, and all other settings needed to make a connection to the database.

Obtain a DataSource object at runtime by using JNDI. Your Servlet container may act as the naming/directory server to provide the DataSource object, if your sysadmin so configures it. Or the DataSource can be obtained via JNDI from an external server such as an LDAP server.

Again, the beauty of using DataSource and JNDI is that you as the Servlet programmer need not be involved, or even informed, when the deployment details change.

JDBC driver location

For Servlet work, you generally do not bundle the JDBC driver with your app.

Instead, the JDBC driver goes into a folder managed by your Servlet container.

In development, your IDE may need access to the JDBC driver to compile. If so, in your Maven POM, mark the dependency with a <scope>provided</scope> element. This tag tells Maven to omit that dependency from the final build because the dependency will already be present (provided) at runtime.

This topic has been addressed many times on Stack Overflow. Search to learn more.

  • Related