Answer the question
In order to leave comments, you need to log in
Why is a Driver needed when connecting to a database?
Hello.
I started to study databases in Java, found many different articles, lessons. Many people inject an instance of the com.mysql.cj.jbdc.Driver class and register it with the DriverManager, after which it is not applied, someone registers it through Class.forName("com.mysql.jdbc.Driver").newInstance(), someone does not prescribe it at all, however, CRUD requests do not interfere with this.
Actually, the question is: is this driver needed if all requests are fulfilled, and if so, why?
Answer the question
In order to leave comments, you need to log in
Technically, it usually goes like this. When you call , the JVM loads that class and calls that class's static initializer. In the case of JDBC drivers, they usually call a static method there and register themselves ( example for mysql ). Registering is simply storing in an internal static list. Further, when you call , DriverManager looks for a suitable driver for this (pulling the method that all drivers must implement), then it connects using the method of this driver. Of course, there are many more nuances, especially when it comes to web applications.
I didn’t see what they called , it seems to be no different from just .Class.forName("...")
DriverManager.registerDriver(...)
DriverManager.getConnection(jdbcUrl)
jdbcUrl
acceptsURL
connect
Actually, the answer to the question: yes, of course it is needed. The driver implements the communication of the application with the database (ie, the transfer of SQL queries to the database and return of results), without it there is no way. If you do not see that the driver class and its settings are written explicitly, most likely they are still there somewhere in the internals of the code / resources.
The word driver ("driver") in the first place means the element that will direct your queries to the database and return the answers to you. Many databases have their own driver, so it must be designated. Actually, as in other languages, the code does not directly access the driver, the driver in this case acts as a constant that determines the type of request at a lower level.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question