JDBC on Linux – Part 1: Connecting to Database

Java + MySQL + Linux.

JDBC is something you must get yourself familiar with if you want to be a Java developer. SQL and DB skills are a crucial part of coding, and what’s interesting is that playing with DB is really fun! Every SQL query makes logically beautiful sense and it’s way more powerful than Excel!

My driving force to develop new skills and experiment with technology is almost unstoppable. When it comes to Java, my primary language for both my work and my portfolio language, JDBC is something I’ve been getting myself familiar with. For those who don’t know what it is, it’s the driver that connects Java programs with SQL databases.

For my own work experiences, I was in a team to develop a Spring-powered Database app for our client two years ago, and as you may guess, Spring is of course powered by JBDC in its deep backend system. Database, DB for short, is a crucial part of an app development regardless of desktop or mobile. Can you imagine any real-life app that has nothing to do with DB? Not many.

Additionally, since I’ve been posting a lot about Java lately, I think this is a great opportunity not only to give myself a chance to re-learn the core-database technology but also to give you a glimpse into the DB world.

Without further adieu, let’s get into it!

DB:

Here’s the DB’s table I interact with – this is a budget-spending-tracker DB and there are two records in the table (sample_tbl). And as you can guess from the title, I’m running this DB on my Linux server, more specifically Ubuntu Server 20.04. Also, I use MySQL as my D database.

image 01

Java code:

And this is the Jaca code that connects itself with the DB. For security reasons, I covered my user name and password. And without any explanation, I expect you to understand the basic mechanisms here.

image 02

Run the code:

When you run the code on the command line, you’ll need a MySQL connector driver (probably you can download it from Oracle’s official MySQL page).

Once you get the driver, run your app just like the below:

Example:

java -cp .:mysql-connector-java-8.0.11.jar YourAppName

My sample:

java -cp .:mysql-connector-java-8.0.11.jar FirstApp

Executed result:

Here’s the result of it.

image 03

Easy right? Next time, I’ll explore more about the JDBC world as well as the SQL techniques!

Leave a Reply