01: JDBC with MySQL Beginner Tutorial Step by Step

This assumes that MySQL database is setup as per the tutorial Getting started with MySQL database beginner tutorial and ⏯ MySQL database beginner video tutorial.

Step 1: Create a maven based Java project from a command-line.

Step 2: Import the project into Eclipse as “File” –> “Import” –> “Existing Maven Projects”.

pom.xml for the MySQL Driver

Step 3: In order to work with MYSQL database or any other databases like Oracle, Sybase, etc you need the driver jar, which is the JDBC implementation. JDBC is an an abstraction layer to which the Database providers write drivers.

This brings in the “mysql-connector-java-6.0.6.jar“, which needs to be in the classpath.

Step 4: Create a new package “com.mytutorial.util”, and create util class that returns a MySQL connection.

Connect to MySQL database
Model class Account

Step 5: Create a POJO (i.e. Plain Old Java Object) class “Account.java” to read the data from the database and map to this model object. Create it under a new package “com.mytutorial.model”.

Data Access Object layer that talks to a database

Step 6: Define the DAO interface “AccountDao.java” inside a new package “com.mytutorial.dao”.

Step 7: Define the DAO implementation “AccountDaoImpl.java” inside a the package “com.mytutorial.dao”.

Main class

Step 8: Define the executable “JdbcExample.java” class inside a new package “com.mytutorial.main”.

Run inside eclipse

Select the Java artefact “JdbcExample.java”, and select “Run as” –> “Java Application”.


Run outside eclipse
Add shade plugin to build an uber jar

with mysql-connector-java-6.0.6.jar classes inside “simple-persistence-1.0-SNAPSHOT.jar”.

Now you can run it as:

800+ Java Interview Q&As Menu

Learn by categories on the go...
Learn by categories such as FAQs – Core Java, Key Area – Low Latency, Core Java – Java 8, JEE – Microservices, Big Data – NoSQL, Architecture – Distributed, Big Data – Spark, etc. Some posts belong to multiple categories.