Spring MVC CRUD Example
CRUD (Create, Read, Update and Delete) application is the most important application for creating any project. It provides an idea to develop a large project. In spring MVC, we can develop a simple CRUD application.
Here, we are using JdbcTemplate for database interaction.
Create a table
Here, we are using emp99 table present in the MySQL database. It has 4 fields: id, name, salary, and designation. Here, id is auto incremented which is generated by the sequence.
Spring MVC CRUD Example
1. Add dependencies to pom.xml file.
pom.xml
2. Create the bean class
Here, the bean class contains the variables (along setter and getter methods) corresponding to the fields exist in the database.
Emp.java
3. Create the controller class
EmpController.java
4. Create the DAO class
Let’s create a DAO class to access the required data from the database.
EmpDao.java
5. Provide the entry of controller in the web.xml file
web.xml
6. Define the bean in the xml file
spring-servlet.xml
7. Create the requested page
index.jsp
8. Create the other view components
empform.jsp
empeditform.jsp
Here “/SpringMVCCRUDSimple” is the project name, change this if you have different project name. For live application, you can provide full URL.
viewemp.jsp
Output:
On clicking Add Employee, you will see the following form.
Fill the form and click Save to add the entry into the database.
Now, click Edit to make some changes in the provided data.
Now, click Edit Save to add the entry with changes into the database.
Now, click Delete to delete the entry from the database.
Download SQL File
Download SQL File
Download MYSQL-connector.jar file
If you are not using maven, download MYSQL-connector.jar.