5

In JDBC, I only see examples using

Class.forName("com.mysql.jdbc.Driver", true, cl);

and haven't seen one using

import com.mysql.jdbc.Driver;

Is it because we want to let a driver package be dynamically provided at execution time, so can be known only at execution time?

If we have a fixed driver package known before execution, is it possible to go with the second way? How would you compare the two ways?

Thanks.

Tim
  • 1
  • 141
  • 372
  • 590
  • It is possible to use vendor-specific classes. The JDBC API is primarily designed to provide a standard way of interacting with databases in a vendor-agnostic, portable way. – ernest_k Jun 10 '18 at 15:09
  • Do you mean using `import com.mysql.jdbc.Driver;` means I must use vencor-specific classes? Can't `import com.mysql.jdbc.Driver;` be used with JDBC API? – Tim Jun 10 '18 at 15:10
  • You just don't ever need the `Driver` class directly, it's not a member of the API - just something that provides the actual interface implementations. The main API consists of `DriverManager`, `Connection`, `Statement` and `ResultSet`. Nowadays, you don't need to load the `Driver` class directly but use `DriverManager#getConnection` with a proper `String` argument. – daniu Jun 10 '18 at 15:33

1 Answers1

10

I only see examples using

Then you're reading really old stuff about JDBC. This is not useful anymore, for quite a long time. It was necessary to load the driver class to make sure the necessary driver was loaded, and able to handle connections to the provided database URLs, before trying to do so.

The JDBC abstractions are all you need to access a database, and you shouldn't care whether you're dealing with a MySQL driver or an Oracle driver, or whatever. Loading the driver dynamically, at runtime, allows removing the driver jar file from the compile classpath, and making sure you only rely on the standard JDBC classes and interfaces.

Note that importing a class doesn't do anything, other than allowing you to use the simple class name in your code. It's not equivalent to loading and initializing a class, which is what the first snippet does.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks. (1) can you tell me what new stuff about JDBC is? (2) "importing a class doesn't do anything, other than allowing you to use the simple class name in your code. It's not equivalent to loading and initializing a class". Could you give some examples to show that, and explain when I need to load and initialize a class, when I don't? – Tim Jun 10 '18 at 15:13
  • 1
    1. The official JDBC tutorial, which answers your question, BTW: https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html. 2. You basically never have to load and initialize a class that way. That was a hack that is not used anymore. – JB Nizet Jun 10 '18 at 15:17
  • `Class.forName()` can still be necessary if the driver is not on the initial classpath. For example, if packed in a WAR, then it is on the context class path of the web app, and needs to be loaded explicitly. But then again, for those types of apps, one should use `javax.sql.DataSource` instead any way. – Mark Rotteveel Jun 10 '18 at 17:00
  • Without using `Class.forName("com.mysql.jdbc.Driver", ...)`, will `com.mysql.jdbc.Driver` be dynamically loaded? If yes, how? – Tim Jun 10 '18 at 20:07
  • It's described in the javadpc of DriverManager: https://docs.oracle.com/javase/8/docs/api/java/sql/DriverManager.html#getConnection-java.lang.String- – JB Nizet Jun 10 '18 at 20:35
  • Thanks. "The JDBC abstractions are all you need to access a database, and **you shouldn't care whether you're dealing with a MySQL driver or an Oracle driver, or whatever**." But `Connection con = DriverManager.getConnection( "jdbc:myDriver:myDatabase", username, password);` still has the database URL? – Tim Jun 11 '18 at 00:01
  • 1
    Yes, but this URL can (and often is) externalized into some properties file. My point is: you shouldn't make your code dependant on a specific proprietary driver class, and loading a class using reflection to trigger the registration of a driver was a hack that has been unnecessary for years. – JB Nizet Jun 11 '18 at 06:12
  • Thanks. If I understand your comment correctly, JDBC is DBMS agnostic. Does a JDBC driver implement a DBMS specific or DBMS agnostic interface? – Tim Jun 12 '18 at 21:02
  • Thanks. Regarding "this URL can (and often is) externalized into some properties file", Is https://stackoverflow.com/a/4085484/156458 the way to do it? Or https://stackoverflow.com/questions/50827926/is-properties-still-used-for-loading-configuration-of-jdbc-from-a-file? – Tim Jun 13 '18 at 01:28