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

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

Lets see one by one each method

JDBC Insert Employee Details in MySql

JDBC Update Employee Details in MySQL

JDBC Delete Employee Details from MySQL

JDBC Fetch Employee Details based on Id

JDBC Fetch Employee Details based on Email Id

JDBC Fetch Employee Details based on Mobile Number

JDBC Search Employee Details based on Name

JDBC Fetch Employee Details based on City Name

JDBC Fetch Employee Details based on Salary Range

JDBC Fetch Employee Details based on Date of Birth (DOB)

JDBC Fetch Employee Details based on Date of Joining Range (DOJ)

JDBC Fetch Employee Details Order by Id DESC

All Complete code for JDBC Operations

Download this Java Project with source code for github

1 thought on “MySql Jdbc Operations for beginner”

Comments are closed.