Home United States USA — software JDBC Tutorial Part 1: Connecting to a Database

JDBC Tutorial Part 1: Connecting to a Database

94
0
SHARE

In this series, learn the basics of Java Database Connectivity. In Part 1, learn what a JDBC driver is and how to open and safely close database connections.
Join the DZone community and get the full member experience. In this series of articles (and videos), you’ll learn the basics of Java Database Connectivity, most frequently abbreviated as JDBC. All of the most popular persistence frameworks use JDBC behind the scenes, so having a solid understanding of the key concepts in JDBC is key when using JPA, Hibernate, MyBatis, jOOQ, or any other database framework for Java. The most important concepts that you need to know are: JDBC driver Connection pool In addition to these concepts, you need to understand how to make calls to the database: specifically, how to run an SQL query from a Java program, how to process the results, and how to insert, update, and delete data. This article focuses on what a JDBC driver is and how to open and safely close database connections. The next articles talk about executing SQL statements and using connection pools. The source code is available on GitHub with each part in independent Maven projects. Here’s a video version of this article, in case you want to see the concepts in action: Before we start, we need a database to play with. I assume you have installed MariaDB on your computer or have a SkySQL account with a database instance running in the Cloud (you can create an account for free). In my case, I have it running on my computer, so I can connect to the server using the mariadb client tool using the database user that I created beforehand: You might have to specify the host, port, and database username for your own instance. For example, in the case of SkySQL, you can use something like the following: Let’s create a new database with the name jdbc_demo and a table with the name programming_language as follows: Use the quit command to exit the client tool. JDBC is an API specification: a set of interfaces that define what the technology can do. It doesn’t implement the details of how to connect to a specific database. Instead, it lets database vendors implement the logic required to “talk” to their databases. Each database has a different way to “speak” through the network (the database client/server protocol), so each database needs custom Java code compatible with JDBC.

Continue reading...