- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is it mandatory to register the driver while working with JDBC?
Initially, till Java6 it is needed to register the driver using Class.forname() or the registerDriver() method before establishing connection with the database.
But, since Java 1.6, JDBC 4.0 API, there is no need to register the driver explicitly, You Just need to set the Class path for the JDBC 4.X driver, Java automatically detects the Driver class and loads it.
Example
In the following JDBC program we are trying connect with MySQL database first of all include the dependency for the MySQL driver in the pom.xml of your project.
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency>
Then, without registering the MySQL driver class com.mysql.jdbc.Driver we are trying to invoke the getConnection object.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class RunningScripts { public static void main(String args[]) throws SQLException { //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/talakai_noppi"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established: "+ con); } }
Output
Soon we execute this JVM automatically detects the MySQL driver and loads it automatically and this program executes successfully and generates the following output −
Connection established: com.mysql.jdbc.JDBC4Connection@424c0bc4
- Related Articles
- Is it mandatory to close JDBC connections?
- How to de-register a driver from driver manager’s drivers list using JDBC?
- How to handle Null values while working with JDBC?
- How to handle Exceptions while working with JDBC applications?
- What is the command used to register gecko driver in Selenium?
- What is the MySQL JDBC driver connection string?
- What is type1 driver of JDBC what are the advantages and disadvantages of it?
- What is type2 driver of JDBC what are the advantages and disadvantages of it?
- What is type3 driver of JDBC what are the advantages and disadvantages of it?
- What is type4 driver of JDBC what are the advantages and disadvantages of it?
- Among all 4 JDBC driver types, when to use which driver?
- Is it mandatory to use T for type-parameter, while defining Generics classes/methods in Java?
- Is it mandatory to link my Aadhaar with bank account and mobile?
- How to get the properties of a driver using JDBC?
- How many ways are there to register a driver in Java?
