Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Steps of Database Connectivity in Java

  • What is Verb
    What is Verb? Types of Verbs and Examples Grammar
  • Ramayan : Short Story of ramayana in Hindi | Qualities of Rama
    Ramayan : Short Story of ramayana in Hindi | Qualities of Rama Spiritual
  • P V Sindhu Biography in Hindi
    P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति Biography
  • World Environment Day in Hindi : Objective, Importance, Theme
    World Environment Day in Hindi : Objective, Importance, Theme General Knowledge
  • World Red Cross Day and Red Crescent Day | विश्व रेडक्रॉस दिवस
    World Red Cross Day and Red Crescent Day | विश्व रेडक्रॉस दिवस General Knowledge
  • Diwali The Festival of Lights
    Diwali 2022, Indian Festival of Lights essay (दिवाली त्योहार पर निबंध, कहानी) Festival
  • Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी
    Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी Biography
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
    Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी Biography

Steps of Database Connectivity in Java

Posted on August 27, 2021November 13, 2021 By GeekCer Education
Steps of Database Connectivity in Java

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?
  • Steps of Database Connectivity in Java
    • Step 1. Registering the driver
      • Syntax of forName() method
      • Example of forName() method
    • Step 2: Connecting to a driver
      • Syntax of getConnection() method
      • Example of getConnection() method
    • Step 3: Preparing SQL statements
      • Syntax of createStatement() method
      • Example of createStatement() method
    • Step 4: Executing the SQL statements on the database
      • Syntax of executeQuery() method
      • Example of executeQuery() method
    • Step 5: Closing the Database Connectivity
      • Syntax of close() method for Database Connectivity
      • Example of close() method for Database Connectivity
  • How can you register a driver for Database Connectivity?

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:

  1. Registering the driver
  2. Connecting to a driver
  3. Preparing SQL statements
  4. Executing the SQL statements on the database
  5. 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:

  1. By creating an object to driver class.
  2. By sending the driver class name to Class.forName() method.
  3. Using DriverManager.registerDeriver() method.
  4. By using getProperty() method of System class.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Java Tags:Database Connectivity, DBMS, Query, SQL Command, SQL Statement

Post navigation

Previous Post: JDBC Driver in Java
Next Post: Difference between GenericServlet and HttpServlet

More Related Articles

Java LinkedList Java LinkedList Java
Difference between GenericServlet and HttpServlet Difference between GenericServlet and HttpServlet Differences
Fork/Join Framework in Java | RecursiveTask, RecursiveAction Fork/Join Framework in Java | RecursiveTask, RecursiveAction Java
Immutable class in Java Immutable class in Java | How to create Immutable class in Java Important
Java ArrayList Java ArrayList Java
Java Keywords Java Keywords Java
  • Java Home
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Keywords
  • Java Operators
  • Java If-else Statement
  • Java Switch
  • Java Loop
  • Java Arrays
  • Method Overloading in Java
  • Java OOP
  • Java Collections
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Multithreading in java
  • Thread Synchronization
  • Exception Handling
  • Java JDBC Driver
  • Java Database Connectivity steps
  • Lambda Expressions
  • Concurrent Collections
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved