
There are several steps we use to establish connectivity with the database in Java. In this section we will go through the steps of Java Database Connectivity in details.
Let’s first get the basic idea about the database driver and then we will know the steps of database connectivity with syntax and example.
Table of Contents
What is Database Driver?
A driver is a set of classes and interfaces, written according to the JDBC API to communicate with the database.
Steps of Database Connectivity in Java
Programmers use the following steps for database connectivity:
- Registering the driver
- Connecting to a driver
- Preparing SQL statements
- Executing the SQL statements on the database
- Closing the Database Connectivity
Step 1. Registering the driver
This is the very first step to connect to database, programmer specifies which database driver he is going to use to connect to the database and then programmer follows other steps.
Basically, We use forName() method to register a driver in Java and it is the most common approach and this method belongs to Class class.
Syntax of forName() method
public static void forName(String className) throws ClassNotFoundException
Example of forName() method
Class.forName("oracle.jdbc.driver.OracleDriver");
Step 2: Connecting to a driver
Before preparing SQL statement we use getConnection() method of the DriverManager class to connect the Java application to the database. This method takes single parameters “url” as well as 3 parameters “url”, “name” and “password”.
Syntax of getConnection() method
public static Connection getConnection(String url, String name, String password)
throws SQLException
public static Connection getConnection(String url) throws SQLException
Example of getConnection() method
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "manager");
Step 3: Preparing SQL statements
Before creating statements you must have a basic idea about SQL which is useful to perform various tasks like adding data to database, updating data, deleting records and retrieving data from database.
First we create a statement using createStatement() of Connection interface to pass the SQL statement to the database and then we execute the query.
Syntax of createStatement() method
public Statement createStatement() throws SQLException
Example of createStatement() method
Statement stmt = con.createStatement();
Step 4: Executing the SQL statements on the database
Before closing connection we use executeQuery() method of the Statement interface to execute query in database. We pass the SQL statement in this method and then it executes the query on database and gets back the resultant rows.
Syntax of executeQuery() method
public ResultSet executeQuery(String sql) throws SQLException
Example of executeQuery() method
ResultSet rs = stmt.executeQuery("select * from emp");
Step 5: Closing the Database Connectivity
This is the last step in which we close the connection using close() method and it is a method of the Connection interface. If we close the connection then it will automatically close the ResultSet.
Syntax of close() method for Database Connectivity
public void close() throws SQLException
Example of close() method for Database Connectivity
con.close();
How can you register a driver for Database Connectivity?
Following are the 4 options by which you can register driver in Java:
- By creating an object to driver class.
- By sending the driver class name to Class.forName() method.
- Using DriverManager.registerDeriver() method.
- By using getProperty() method of System class.