Home > Blog > Introduction to Java Database Connectivity - JDBC Tutorial

Introduction to Java Database Connectivity - JDBC Tutorial

Introduction to Java Database Connectivity - JDBC Tutorial

By Upskill Campus
Published Date:   27th September, 2024 Uploaded By:    Priyanka Yadav
Table of Contents [show]

Just suppose you're working on a Java app and need to save and load information from a file or database. One way is to write new code every time you need to do this, but that's slow and can lead to mistakes. A better way is to use a database like MySQL, Oracle, or SQL Server. These databases handle the data for you, so you don't have to worry about the details. That's where Java database connectivity comes in.

 

Understanding Java Database Connectivity

 

JDBC is a powerful tool that simplifies database interactions in Java applications. In addition, it provides a standard API for connecting to various database systems, executing SQL queries, and retrieving results. By using JDBC, developers can focus on writing business logic without worrying about the complexities of database interactions. As a result, this leads to more efficient and maintainable code.
 

Java database connectivity is a tool that helps Java programs talk to databases. In short, it's a bridge that connects Java apps to different kinds of databases so they can share information. Moreover, JDBC has rules and guidelines that make it easy for Java apps to work with databases. It works with both databases and spreadsheets.
 

For example, you go to a bank. When the bank employee wants to see your transactions, they just click a button on their computer. However, this button uses JDBC to send a unique request to the bank's database, and the database sends back your transaction information. So, the employee doesn't have to write complicated computer code to get the data.

 

Components of JDBC Database Connectivity

 

Java Database Connectivity has four prominent parts that help it talk to databases:
 

    • JDBC API: This is a set of tools that makes it easy for Java apps to communicate with databases.
  • Java DBC Driver Manager: This is a bridge that connects your Java app to the specific database you're using.
  • JDBC Test Suite: This is a way to check if the JDBC tools are working correctly.
  • JDBC-ODBC Bridge Drivers: This is a translator that helps JDBC talk to older databases that use a different language called ODBC.

Under this Java Database Connectivity (JDBC) tutorial, we will now discuss the architecture.

 

Architecture of JDBC

 

JDBC is a tool that helps Java apps talk to databases. Here are the main parts of JDBC:
 

  • Application: This is the Java part of your app that needs to talk to the database.
  • JDBC API: This is a set of rules that tells Java apps how to talk to databases. In addition, it has different parts called interfaces and classes.
  • DriverManager: This is a bridge that connects your Java app to the specific database you're using.
  • JDBC Drivers: These are the advanced translators that help the DriverManager understand the language of the database.

 

Overview of API

 

Before we talk about JDBC drivers, let's understand what an API is. An API is a set of rules that helps different software programs talk to each other. In other words, it's a bridge that lets one app ask another app for information or do something without needing to know how the other app works. Java database connectivity uses APIs to talk to databases.

 

What is the JDBC Driver for SQL Server?

 

JDBC drivers are translators that help Java apps talk to databases. In addition, they are installed on your computer and convert the messages from your Java app into a language that the database can understand. Nevertheless, there are four different types of JDBC drivers:
 

  • Type-1 driver: This is an older-style translator that needs another program called ODBC to work.
  • Type-2 driver: This is a translator that uses some special parts from the database to talk to it.
  • Type-3 driver: This is a modern translator that can talk to many different databases.
  • Type-4 driver: This is a very simple translator that is designed to work with a specific database.

 

How to Connect Two Databases in Java?

 

JDBC is a better version of ODBC. ODBC was hard to use because it worked differently on different computers. However, it was written in languages like C, C++, and Python, which also don't work the same way on all computers. To fix this, JDBC was made by a company that makes databases. In addition, JDBC is written in Java, which works the same way on all computers. 

 

Java Database Connectivity Steps

 

Let's break down the process of using JDBC to connect your Java app to a database:
 

  • Gather Your Tools: First, start by importing the necessary Java tools that help you communicate with databases.
  • Find the Right Translator: Different databases use different languages. You need a translator (JDBC driver) that can talk to your specific database.
  • Introduce the Translator: After that, let your Java app know about the translator you found.
  • Build a Bridge: Then, create a connection between your app and the database.
  • Make a Plan: Write a plan (SQL statement) for what you want the database to do (like getting or changing information).
  • Send the Plan: Tell the database to follow your plan.
  • Close the Connection: When you're finished, end the connection between your app and the database.

 

Java Database Connectivity Example

 

To connect your Java app to a MySQL database, you need to follow these steps:
 

Gather Information: Get the following details about your MySQL database:
 

  • Driver Class: com.mysql.jdbc.Driver
  • Connection URL: jdbc:mysql://localhost:3306/sonoo (replace "sonoo" with your database name)
  • Username: root
  • Password: The password you set when you installed MySQL
  • Create a Database: Before you can create a table, you need to create the database itself.


Remember: You can replace "localhost" with the IP address of your MySQL server if it's on a different computer.

create database sonoo;

use sonoo;

create table emp(id int(10),name varchar(40),age int(3));

Example:

import java.sql.*;

class MysqlCon{

public static void main(String args[]){

try{

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection(

"jdbc:mysql://localhost:3306/sonoo","root","root");

//here sonoo is database name, root is username and password

Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("select * from emp");

while(rs.next())

System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));

con.close();

}catch(Exception e){ System.out.println(e);}

}

}

 

Concluding Thoughts

 

Java Database Connectivity is a helpful tool that lets Java apps talk to databases. In addition, it has simple rules for working with different kinds of databases, making it easier for developers to use data in their apps. By learning how JDBC works, you can connect your Java apps to databases and do things with data.

 

Frequently Asked Questions

 
Q1. Which package is used for Java database connectivity?

Ans. JDBC is a tool that lets Java apps talk to different kinds of data. It's a translator that can understand many languages, so you can use Java to get information from databases, spreadsheets, and other places where data is stored.


Q2. How many ways can connect to database in Java?

Ans. JPQL, HQL, Criteria API, Querydsl, jOOQ, JaQu, JDBC, and ActiveJDBC are some ways that can help you connect to the database in Java.

About the Author

Upskill Campus

UpskillCampus provides career assistance facilities not only with their courses but with their applications from Salary builder to Career assistance, they also help School students with what an individual needs to opt for a better career.

Recommended for you

Leave a comment