Updated April 17, 2023
Introduction to JDBC getConnection
The following article provides an outline for JDBC getConnection. Java database connectivity gets connection method is used for establishing the connection between the java application program where you are working and the database that will be used for storing and manipulating the data of your application. There are certain basic steps that you should follow to establish the connection. There are also multiple approaches using which you can establish the connection.
Connection Establishment in JDBC
After the installation of the required driver, you have to just simply follow the below steps for connection establishment in JDBC.
- Import all the necessary packages: All the java programming packages that will be required in you program should be included and imported right at the beginning of the file by using the import statement.
- Registration of JDBC driver: The loading of the required driver inside the memory of the system is done by the Java Virtual Machine for the handling and fulfilling of all the further requests that will be made.
- Formation of the uniform resource locator un database: The URL plays an important role and is necessary to build while establishing a connection. This helps in recognizing the appropriate address and the target database which needs to be connected.
- Object creation for connection: getConnection() method is given the call in this step which is an object of the driver manager that will, in turn, create an object of the connection establishing the actual connection to the database from our application.
Syntax of JDBC getConnection
Given below is the syntax in detail about each of the steps seen above:
1. Import Package
The syntax of importing is basically writing the import statement followed by the name of the package that you wish to import. This helps the java compiler to identify the classes that should be called and referred. This import statements are always written in the beginning part of the code covering the first few lines. For most of the manipulations related to SQL database operations, we will import the following two packages.
Import java.math. *;
Import java.sql.*;
The first package helps in support of big integers and big decimals while the second package is used in the standard programs of JDBC.
2. Driver Registration
The class files of the driver get loaded inside the memory which can be further used further by interfaces of JDBC. The registration of the driver can be done by using either of the two approaches mentioned below.
a. Using Class.forName()
This the most common method of registering the driver which loads all the files automatically. This is also preferable due to its portability and configuration of the driver registration. The loading is done dynamically in this approach.
The following example demonstrates the syntax of the same.
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException sampleException) {
System.out.println("Sorry! We cannot load the class for the driver");
System.exit(1);
}
b. Using DriverManager.registerDriver()
When we are making the use of the Java Virtual Machine which is non- JDK compliant like Microsoft then we can make the use of this static method called DriverManager.registerDriver() that will help you to register your driver.
try {
Driver sampleExampleDriver = new com.mysql.jdbc.Driver();
DriverManager.registerDriver( sampleExampleDriver );
}
catch(ClassNotFoundException sampleException) {
System.out.println("Sorry! We could not establish the driver registration");
System.exit(1);
}
Formulation of URL for Database
We can create the connection by using the DriverManager.getConnection() method that is overloaded in three ways.
- geConnection(String UniformResourceLocator, String username, String associatedPassword)
- geConnection(String UniformResourceLocator)
- geConnection(String UniformResourceLocator, Properties associatedProps)
The URL is formed for each of the databases and driver for the database you use by using the below table:
Format of Uniform Resource Locator | Name of the JDBC Driver | Relational Database Management System |
jdbc:oracle:thin:@name of host:number of port:name of database | oracle.jdbc.driver.OracleDriver | Oracle |
jdbc:sybase:Tds:name of host: port Number/name of database | com.sybase.jdbc.SybDriver | Sybase |
jdbc:mysql://name of host/ name of database | com.mysql.jdbc.Driver
|
MySQL |
jdbc:db2:name of host:port Number/name of database | COM.ibm.db2.jdbc.net.DB2Driver | DB2 |
We can specify the information of the connection URL of the user name, associated password and the port address information of the host along with IP address and the TCP/ IP protocol for transportation of the data containing request and response.
The below extract shows the sample of the info that will show this method.
String URL_TO_CONNECT = “jdbc:oracle:thin:@payal:1603:EDUCBA”;
String USERNAME = “payal”;
String ASSOCIATED_PASSWORD = “payal@123”
Connection conn = DriverManager.getConnection(URL_TO_CONNECT, USERNAME, ASSOCIATED_PASSWORD);
Alternatively, you can also specify just a single url for connection establishment but in this case, the url should contain the information of username and the password in it as shown in the below sample.
String educba_URL = “jdbc:oracle:thin:payal/payal@123@payal:1603:EDUCBA”;
Connection sampleConnection = DriverManager.getConnection(educba_URL);
One more method includes the specification of properties username and password same as above.
Closing the Connection
The closing of the connection that is established from the java program to the database for clearing the environment and the associated memory can be done by using the close() method that should be given a call by using the object of connection.
For sample statement consider shown below:
sampleConnectionObject.close();
Example of JDBC getConnection
Given below is the example mentioned:
Let us consider one sample program for setting up the connection by using the getConnection() method.
Code:
import java.sql.Connection;
import java.sql.DriverManager;
public class sampleEducbaPayalConnectionEstablishment{
public static void main(String args[]) throws ClassNotFoundException
{
String sampleURL;
Connection sampleConnection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
sampleURL="jdbc:mysql://localhost:3306/educba";
username="root";
associatedpassword="";
sampleConnection = DriverManager.getConnection(sampleURL,username,associatedpassword);
System.out.println("Successfully Established the connection!");
sampleConnection.close();
System.out.println("The established connection has been closed!");
}
catch (Exception sampleException) {
System.out.println(sampleException.toString());
}
}
}
Output:
Conclusion
The creation of the connection to the database from the java application can be done by using the method getConnection by using the methodologies. Note that creating the connection consumes memory. Hence, after you are done with performing all your operations related to the database, you should close the created connection to clear up the resources.
Recommended Articles
This is a guide to JDBC getConnection. Here we discuss the introduction, how it works & the steps required? formulation of URL for database & example. You may also have a look at the following articles to learn more –