MySql Jdbc Operations for beginner

Database connection with java is one of the essential feature of java, In java to connect with database we use java database connectivity(JDBC) programs.

JDBC is an Application Program Interface(API) that is used to interact with database and java program in database independent way.

So JDBC (full form Java Database Connectivity) code to interact with mysql, oracle database and with other database.

Here we used jdbc with mysql to connect it we need a mysql jdbc driver to connect mysql with java.

Here we will see a java jdbc project to perform all basic operations involved in Database interaction.

Using Employee details program in java using jdbc opetations.

Setting Up Project

JDBC project setup is in two part

  • create database
  • Write Java, Jdbc codes

Creating database

  • Create a database user name with password.
  • given all privileges to user.
  • created database and table for employee.

Above all details are set in following script.

Run this script before running java jdbc program

Writing Java Jdbc Code

  • Create Java Bean
  • Creating Main class (Execution starts from here)
  • Create Action class
  • Create DAO class

Java Bean

A Java Bean is a simple java class, that holds all properties, getter setter methods and constructors of the class.

Java Bean for Employee class is given below

Create Main Class

Below is main class for running all java jdbc examples

Here you find various examples like to insert in table, to update table ,delete a recored from mysql table and select a specific or all record from table.

To run below code see all actions and operations in below table

Sr No Operation and code Action
1 ADD (1) Add Employee records in database should be initialize only once. First time after creating database
2 UPDATE (2) Update a record in table, based on primary key id
3 DELETE (3) Insert a new record to table and delete it from database
4 FETCH BY ID (4) Fetch a record from table based on id
5 FETCH BY EMAIL (5) Fetch a record from table based on email
6 FETCH BY MOBILE NO (6) Fetch a record from table based on mobile number
7 SEARCH BY NAME (7) Fetch records from table based on Name
8 FETCH BY CITY (8) Fetch records from table based on city name
9 FETCH BY SALARY RANGE (9) Fetch records from table based on Salary Range
10 FETCH BY DOB (10) Fetch records from table based on date of birth
11 FETCH BY DOJ RANGE (11) Fetch records from table based on Date of Joining Range
12 FETCH ALL (12) Fetch all records from table

Create Action Class

Above class will call method of below action class

Create DAO class

The above action class will call to below DAO class method

Here All JDBC Prepared statement are used to interact with mysql

Various JDBC operations used here are

  • Insert
  • Update
  • Delete
  • FetchById
  • FetchByEmailId
  • FetchByMobileNo
  • SearchByName
  • FetchByCity
  • FetchBySalaryRange
  • FetchByDob
  • FetchByDojRange
  • FetchAll

Download this Java Project with source code for github

1 thought on “MySql Jdbc Operations for beginner”

Comments are closed.