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

  • Java Switch Case
    Java Switch Case Java
  • Kumkum Bhagya Hindi TV Serial, Cast Name, Actress Name
    Kumkum Bhagya Hindi TV Serial, Cast Name, Actress Name News
  • Difference between Interface and Abstract class
    Difference between Interface and Abstract class Differences
  • Top Spring Boot Annotations
    Top Spring Boot Annotations Spring Boot
  • Agile Methodology of Software Development
    Agile Methodology of Software Development Important
  • Structured Vs Unstructured Data in Hindi | Key Difference
    Structured Vs Unstructured Data in Hindi | Key Difference Differences
  • Maven Tutorial
    Maven Tutorial Important
  • Visual Paradigm Online and integration with Visual Studio
    Visual Paradigm Online and integration with Visual Studio Important

Steps of Database Connectivity in Java

Posted on August 27, 2021November 13, 2021 By admin
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

Concurrent collections in Java, details, advantages, examples Concurrent collections in Java, details, advantages, examples Java
Object Oriented Programming Object Oriented Programming Java
Jenkins java | Installing Jenkins on Windows Jenkins java | Installing Jenkins on Windows Important
Java Comments Java Comments : Types of java comments, Syntax & Examples Java
Java ArrayList Java ArrayList Java
Java If-else Statement Java If-else Statement Java

Related Posts

  • Concurrent collections in Java, details, advantages, examples
    Concurrent collections in Java, details, advantages, examples Java
  • Object Oriented Programming
    Object Oriented Programming Java
  • Jenkins java | Installing Jenkins on Windows
    Jenkins java | Installing Jenkins on Windows Important
  • Java Comments
    Java Comments : Types of java comments, Syntax & Examples Java
  • Java ArrayList
    Java ArrayList Java
  • Java If-else Statement
    Java If-else Statement 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

Recent Posts

  • Structured Vs Unstructured Data in Hindi | Key Difference
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
  • World Environment Day in Hindi : Objective, Importance, Theme
  • Thomas Edison Biography in Hindi – थॉमस एडिसन जीवनी
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
  • DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार
  • Java Switch Case
    Java Switch Case Java
  • Kumkum Bhagya Hindi TV Serial, Cast Name, Actress Name
    Kumkum Bhagya Hindi TV Serial, Cast Name, Actress Name News
  • Difference between Interface and Abstract class
    Difference between Interface and Abstract class Differences
  • Top Spring Boot Annotations
    Top Spring Boot Annotations Spring Boot
  • Agile Methodology of Software Development
    Agile Methodology of Software Development Important
  • Structured Vs Unstructured Data in Hindi | Key Difference
    Structured Vs Unstructured Data in Hindi | Key Difference Differences
  • Maven Tutorial
    Maven Tutorial Important
  • Visual Paradigm Online and integration with Visual Studio
    Visual Paradigm Online and integration with Visual Studio Important
  • 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