Зміст курсу
Java Data Manipulation with Hibernate
Java Data Manipulation with Hibernate
JDBC Introduction
How Applications Interact with Databases
Obviously, merely telling the compiler that we will be using an external database is not enough to connect to a database. We need tools for that.
In programming, code constantly interacts with various components, such as a server or a database; code is just a big set of instructions for a device.
For example, let's say a new user has registered on your website, and you need to store that user's data somewhere so they can log in and use the product again. There are two ways to do this:
-
Save the user's data locally on their device in RAM, similar to how we store data in a variable. However, there's a slight catch: the user can use their profile until their device is rebooted or the RAM is cleared. After that, all information about their profile will be lost, including any purchases they made;
-
Save the data on the server in a database: This is exactly what we'll be learning in this course. All user data or different products' information is stored in the database. When a user registers, their data is recorded in the database. Then, when the user logs in, the code sends a request to the server's database, asking it to check if that user exists. If the response is positive, the user logs in under their profile and continues working. If the server cannot find the user's data in the database, it will display an error message, suggesting that the user may not exist.
Let's look at a visualization of such interaction. First, a user registers on our website, and their data goes into the database:
Then, when the user logs in to the platform again, the database will look for the user's data, after which they can either log in or receive an error:
I think we can draw a conclusion from all of this: Everyone uses databases, storing user data and product data in them. It's reliable, fast, and convenient.
But let's answer the main question of this course: how does Java connect to a database?
The answer is JDBC.
JDBC
JDBC stands for Java Database Connectivity. It is an API that allows developers to create Java applications and connect them to databases.
In simple terms, JDBC is a tool that allows us to connect our program to a database.
Note
It's worth noting that JDBC is used all the time; it's a low-level aspect of programming. Even the ORM framework Hibernate, which we will use to create and modify databases in this course, uses JDBC.
JDBC has its own structure and hierarchy; let's take a closer look at it:
Let's take a closer look at what's happening in this diagram:
- The JDBC API provides a link between the application and JDBC management;
- A JDBC Driver Manager facilitates the interaction between JDBC and the database driver;
- A JDBC Driver is a component that implements the JDBC interface for a specific DBMS. Each DBMS has its own driver. The JDBC Driver Manager is responsible for managing database drivers and establishing connections between the Java application and a specific database.
Conclusion
We can conclude that the JDBC working principle is not as complex as it may seem; in fact, it resembles the operation of a typical IT company. A Java application contacts the JDBC API, requesting communication with a database. At the same time, the JDBC API reaches out to the JDBC Driver Manager to find the appropriate driver for database communication. The JDBC Driver Manager excels at its job and selects the necessary driver for a specific DBMS, after which we connect our code to that database.
JDBC also includes various components, which serve as instructions for their use in code. We will discuss these components and their usage in the next chapter!
Дякуємо за ваш відгук!