DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป is a community of 968,547 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Introduction to JDBC
Lorena C Silva for Vaultree

Posted on

Introduction to JDBC

The JDBC was one of the first libraries developed for Java. It was released in 1997, as part of the JDK 1.1. Initially, it was developed for the client side (java.sql), where it was possible to assist and manage database connections allowing queries, commands and data manipulation. With JDBC 2.0, an optional package that supported connections to the server side (javax.sql) was also included. Until the present day we constantly have updates for both client and server-side packages.

What is JDBC used for?

JDBC stands for Java Database Connectivity, a Java API that manages the connection to a database. This type of API is called "call-level", which means that SQL statements are transmitted as strings to the API, which then takes care of executing them in the RDMS. When the value of this sequence can be changed at runtime, we can consider JDBC dynamic, thus allowing a JDBC program to be carried between different systems with minimal expense.

Databases

To better understand the need for JDBC, we can consider a project where it is necessary to develop a Java application desktop type and make an integration with MySQL that will run locally and another bank such as PostgreSQL that will be accessed by a windows server. In cases that require relational databases, it is necessary to have access in an easy way with necessary resources. That's how JDBC API was introduced in JDK.

Relational databases still represent the vast majority of data solutions found on the market.

Image description

Java Packages

An API is composed of the java.sql and javax.sql packages, included in JavaSE. Through the classes and interfaces provided by these two packages, people can develop software that accesses any data source, from relational databases to spreadsheets.

JDBC and ODBC

Before JDBC developers used ODBC - Open Database Connectivity - a standard language-independent approach to accessing a relational database management system or RDBMS.

There is indeed a similarity between JDBC and ODBC - previously used to connect to databases. This is because JDBC was created as an inspiration: Java applications could use any database that had an ODBC driver available from native code. In this way the popularity of JDBC existed because of the ODBC driver used for any database on the market.

The JDBC differential is Java specialisation, offering a programming level interface that can handle the mechanics of Java applications and their communication with the database.

Drivers

A JDBC driver is a class that implements the java.sql.Driver interface. Many drivers are fully developed using Java, which helps them to be loaded dynamically.

The drivers can also be written natively, accessing other libraries or other system drivers that allow access to a given database.

Like ODBC, JDBC also works through drivers which are responsible for connecting to the database and executing SQL instructions. These drivers have been divided into four types:

JDBC type 1: First to be created and does not make a real connection with a database, but with ODBC. Its use can be considered relatively low for today because it was written in native language, thus hindering portability and requiring extra configurations that can overload a system.

JDBC type 2: This is the moment when the driver eliminates any ODBC dependency, but the writing in native language is maintained. This code allows calls to the Client API. It is necessary to install libraries of native code where the system will be executed as in type 1, thus being a driver also not very recommended by the large amount of requirements.

JDBC type 3: Written in Java, it eliminates the necessity of installing native code libraries, allowing a greater portability. This driver allows the conversion of JDBC calls into generic network protocol calls that can then be converted to specific DBMS API calls - intermediate software layer.

JDBC type 4: Also totally written in Java, it uses the DBMS proprietary network protocol, converting JDBC calls to direct calls to the DBMS without needing an intermediate client API. Performance is higher since it accesses the database management system directly.

Let's practice?

In the following article, we will show how JDBC works and relates to cryptography! We'll share hands-on content for the members of our community only (Join here!), but if you're down for some theory as well, a few articles that might help you on a mission to be a better developer in a safe way:

At Vaultree we are building an encrypted future. We love sharing valuable information and trends to help you keep your data safe. Sign up to stay in the loop and discuss the hottest trends in cybersec with a team of experts.

Image description

Top comments (0)

๐ŸŒš Friends don't let friends browse without dark mode.

Sorry, it's true.