Ever looked for a comprehensive intro to JDBC that is fun and entertaining at the same time? Then have a look at this brand-new episode of the “Marco Codes” YouTube channel: JDBC Tutorial: Nice and Easy.
In this video, you’ll learn how to use JDBC, the basic API that every Java program uses to access databases. Understand what JDBC drivers are and where to get them, as well as learn how to use an embedded H2 database in addition to the usual suspects like MySQL or Postgres. Fire off SQL select, insert, update and delete statements from Java. Finally, learn about connection pools and the overall Java database framework/library landscape. By the end of the tutorial, there won’t be many questions left when it comes to accessing databases with Java.
What’s in the Video?
Sooner or later, most Java applications need to talk to a database. That’s what the JDBC API is for: Java Database Connectivity. Let’s talk about how we’re going to learn about JDBC.
First of all, you need to get a grasp of what a JDBC driver is and more specifically, how to find the correct one for your database. While you can follow along with this video with a MySQL or Postgres database, we’ll take a shortcut and use an embedded H2 database for this tutorial. Why? Because it needs a special setup and can literally be added to any Java project with just a couple of lines of code.
Once you have your JDBC Driver setup, you can start connecting to databases. There are two main ways of doing that, one being the
DriverManager, and the other one being
DataSources. You’ll first learn about the
DriverManager and its pros and cons.
While trying to connect to our H2 database, we’ll make sure to understand JDBC URLs, including the different vendor peculiarities and how to navigate database vendor websites.
Then it’s finally time to take a look at firing off SQL queries from your tiny Java project. We’ll start with the CRUD basics, which means executing
DELETE statements, and learn about the importance of
PreparedStatements along the way.
PreparedStatements? They are an easy and efficient way to pre-compile SQL statements on your clients, while at the same time protecting against SQL injection attacks. That’s why we are going to spend some time explaining what those two concepts are.
Then, we’ll look at the
DataSource interface (as opposed to
DriverManager), which is the more commonly used method to connect to databases. Frameworks like Spring Boot and literally all other web/database frameworks build on top of
This finally brings us to the concept of connection pools, which also use the
DataSource interface. What’s a connection pool? Well, opening up database connections is a rather expensive process, and instead of opening up a new connection for every SQL statement, you could just pre-open a pool of connections that all users of your application share.
If you managed to watch the entire video, you’ll be presented with some hints, tips, and resources, that will help you continue your JDBC learning journey on your own. Check it out and let us know what you think!