Updated April 18, 2023
Definition of JDBC URL
JDBC provides the URL to identify the database, so we can easily recognize the required driver and we can connect it. Basically JDBC URL we can use as database connection URL as per user requirement. When the driver loaded successfully we need to specify the required database connection URL to connect the database that the user wants. We know that the JDBC URL always starts with the JDBC keyword for the database connection; basically, the URL depends on the JDBC driver. We also need to provide the different parameters with the JDBC URL that is port number, hostname, database name, user name, and password, etc.
Syntax:
specified protocol name//[specified host name][/specified database name][username and password]
Explanation
By using the above syntax, we try to implement the database connection, here we use different parameters such as protocol name, the hostname that we want, specified database name that we need to connect with the username and password, the database name depends on the user.
How URL works in JDBC?
Now let’s see how the URL works in JDBC as follows.
For establishing a connection with the database we need to follow the same step as follows.
Import JDBC Packages: First step we need to import the JDBC packages into the Java program that we require the class in code.
Register the JDBC Driver: After importing the class we need to load the JVM to fulfill that is it loaded the required driver as well as memory for JDBC request.
Database URL Formation: In this step, we need to provide the correct parameter to connect the specified database that we already discussed in the above point.
Create the Connection Object: After the formation of the URL, we need to create the object of connection that means we can call the DriverManager with grtConnection() methods to establish the connection with a specified database name.
Now let’s see in detail how we can import the JDBC Driver as follows.
Basically, the import statement is used to compile the java program and is also used to find the classes that are helpful to implement the source code as per user requirements. By using these standard packages, we can perform different operations such as insertion, delete and update as per user requirements.
import java.sql.*;
Now let’s see how we can register the JDBC Driver as follows.
We just need to import the driver before using it. Enlisting the driver is the cycle by which the Oracle driver’s class document is stacked into the memory, so it tends to be used as an execution of the JDBC interfaces.
You need to do this enrollment just a single time in your program. You can enlist a driver in one of two different ways.
1. By using Class.forName():
The most widely recognized way to deal with registering a driver is to utilize Java’s Class.forName() technique, to progressively stack the driver’s class document into memory, which naturally enlists it. This technique is ideal since it permits you to make the driver enrollment configurable and compact.
2. By using DriverManager.registerDriver():
The second methodology you can use to enroll a driver is to utilize the static DriverManager.registerDriver() strategy.
You should utilize the registerDriver() technique in case you are utilizing a non-JDK agreeable JVM, for example, the one given by Microsoft.
After you’ve stacked the driver, you can set up an association utilizing the DriverManager.getConnection() technique. JDBC provides the different JDBC drivers for the different database systems and we can utilize them as per the user requirement.
1. MySQL JDBC URL format:
This is the first JDBC URL format that can be used in MySQL to establish the connection with the required database name. The format of this URL is as follows.
(Connection con_obj = DriverManager.getConnection(specifed_jdbcUrl, user defined username, user defined password))
Explanation
In the above format, we use DriverManager.getConnection method to establish the connection with the database; here we need to pass the specified JDBC URL as well as we need to pass the username and password. The username and password fields are depending on the user. In JDBC URL we need to pass all parameters that we require to make the connection such as database name, protocol, etc.
2. Microsoft SQL Server URL format:
This is another famous URL format for the database system. Suppose we need to connect to the Microsoft SQL Server from a Java application at that time we can use the below-mentioned format as follows.
jdbc:sqlserver://[specified serverName[\ specified instanceName][:required portNumber]][;property(that user defined properties)]
Explanation
In the above syntax, we need to mention the server name that is the address of the server, or we can say that domain name or IP address. Also, we need to mention the instance name for server connection if we leave then it uses the default. In the same way, we can use port numbers and properties.
3. PostgreSQL JDBC URL format:
PostgreSQL is a famous open-source database system. So we can use the below-mentioned JDBC format as follows.
Jdbc:postgresql://hostname:port number/specified database name and properties.
Examples
Now let’s see different examples of JDBC URLs for better understanding as follows.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class connection_t {
public static void main(String args[]){
String m_url = " jdbc:mysql://localhost ";
Connection con_obj = DriverManager.getConnection(m_url, "root", "root");
System.out.println("Connection successfully established with database. . .");
}
}
Explanation
In the above example, we import the dependencies that are required to establish the connection with the database such as SQL. connection, SQL.DriverManger etc. After that, we import the class as shown. Here we also mentioned a connection string with connection parameters such as DriverManager.getConnection() method as shown. The final output or end result of the above example we illustrated by using the following screenshot as follows.
In the same way, we can connect to the Microsoft server and PostgreSQL as per our requirements.
Conclusion
We hope from this article you learn the JDBC URL. From the above article, we have learned the basic syntax of JDBC URLs as well we also see the different connection string URLs with different examples of JDBC URLs. From this article, we learned how and when we use the JDBC URL.
Recommended Articles
This is a guide to JDBC URL. Here we discuss the definition, syntax, How URL work in JDBC ? and examples respectively. You may also have a look at the following articles to learn more –