Establish Connection to the database

Steps for creating connection with the database -


Step 2- Establish Connection

The next step is to ask the DriverManager class to open a connection to a given database, where the database is specified by a specially formatted URL.
DriverManager class provides the static method getConnection() to create connection. The 2 most commonly used version of getConnection() method are as follows -
1-
Connection getConnection(String URL, String User_Name, String Password) throws SQLException
Where - 
URL - is the url of  the RDBMS you are using.
User_Name and Password - are the user name and password of RDBMS that you specified at the time of RDBMS installation.
It returns a class that implements the java.sql.Connection interface.
For Example -
Connection conn = DriverManager.getConnection("URL", "User_Name", "Password");
if (conn != null) {
    System.out.println("Connected");
}
        
2-
Connection getConnection(String URL) throws SQLException
If database we are using does not require user name and password then we can use this version.
For Example -
Connection conn = DriverManager.getConnection("URL");
if (conn != null) {
    System.out.println("Connected");
}
3-
Connection getConnection(String URL, Properties info) throws SQLException
Where-  
URL - is the url of  the RDBMS you are using.
Properties - is a class available in java.util package. It can hold a list of string key/value pairs as connection 
arguments; normally at least a "user" and "password" property should be included. 
For Example -
Properties properties = new Properties();

// Use Hashtable put() method to put RDBMS user name and password in Properties object.
properties.put("user", "User_Name");  
properties.put("password", "Password");
 
Connection conn = DriverManager.getConnection("URL", properties);
if (conn != null) {
    System.out.println("Connected");
}


Different RDBMS makes use of different URLs -
1- for MySql -
jdbc:mysql://localhost:3306/databaseName
Here localhost is the host name, 3306 is the port number on which MySql RDBM run and databaseName is the name of
database, you created.
2- for Oracle -
jdbc:oracle:thin:@localhost:1521:databaseName
Here localhost is the host name, 1521 is the port number on which Oracle RDBM run and databaseName is the name of
database, you created.
3- for IBM DB2 -
jdbc:db2://localhost:50000/databaseName
Here localhost is the host name, 50000 is the port number on which DB2 RDBM run and databaseName is the name of 
database, you created.

Example -

This example creates a connection with the MySql RDBMS.
To see the detailed steps required to connect with MySql using NetBeans or Eclipse IDE please refer to the Connectivity using MySql RDBMS section.
package my_jdbc_demo;

import java.sql.*;
public class MY_JDBC_DEMO {

    public static void main(String[] args) {
        
        Connection con;
        try
        {
           // Load the driver
            Class.forName("com.mysql.jdbc.Driver");

         // Create Connection
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Company","root","root");
            if(con != null)
            {
                System.out.println("Connection created successfully");
            }
            else
            {
                System.out.println("Connection not created");
            }
        }
        catch(Exception ex)
        {
            System.out.println(ex);
        } 
    }
}