JSP CRUD Application
JSP CRUD Application
Open
This tutorial explains you how you can write CRUD application in JSP. CRUD means create, read,
update and delete. This example explains you about all the steps in creating JSP CRUD application.
CRUD is basically used in the context of database driven application where it Creates, Reads, Updates and
Deletes the data of database. CRUD is also know as create, read, update and delete. The example given
here will teach you how easily you can create application that insert, query, update and delete the data.
Create means to insert a record into the database table, Update means edit/make changes into the existing
record, and the Delete means deleting of record from the table. We can also create a CRUD application
using JSP, Servlet, And the JDBC.
Example
https://www.roseindia.net/jsp/crud-application.shtml 2/17
4/1/2020 JSP CRUD Application
Here we will create a CRUD application using Java Servlet, JSP, and the JDBC. We will understand here
the basic purpose of creating a CRUD application by a registration form where the user will registered by
providing the id, first name, and last name. Update/Edit their records, and can delete their record also. For
this application we will required some front end interfaces for the registering a user and updating the
record. For creating the interfaces I have created the JSP pages such as user.jsp page for adding new user
this page contains a form and the respective input text fields for providing their information and the submit
button as well as created edit.jsp page where the selected record's ID will be showed in the readonly
textfield and the other textfields for updating the respective fields. Then I have created a Java Beans
named UserBean.java which contains some data members (id, fName, lName) and their setter getter
methods. These setter getter methods are used for setting and getting the corresponding value
respectively. Then I have created a ConnectionProvider class to connect with the existing database (in my
case I am using MySQL). Then I have created a DAO class where I have written the code for adding the
user, editing the user record, deleting the user record, getting the all user record, and for getting user
record by ID. Then I have created a Servlet where written the code to handle insert, edit, and delete
records. Finally I have created a listUser.jsp page for displaying all records and the link for updating and
deleting the records. In this application you can insert, edit, or delete application one by one.
Source Code
UserBean.java
https://www.roseindia.net/jsp/crud-application.shtml 3/17
4/1/2020 JSP CRUD Application
package net.roseindia.bean;
ConnectionProvider.java
https://www.roseindia.net/jsp/crud-application.shtml 4/17
4/1/2020 JSP CRUD Application
package net.roseindia.dbconnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
}
}
UserDao.java
https://www.roseindia.net/jsp/crud-application.shtml 5/17
4/1/2020 JSP CRUD Application
package net.roseindia.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import net.roseindia.bean.UserBean;
import net.roseindia.dbconnection.ConnectionProvider;
public UserDao() {
conn = ConnectionProvider.getConnection();
}
ps.setInt(1, userBean.getId());
ps.setString(2, userBean.getfName());
ps.setString(3, userBean.getlName());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
https://www.roseindia.net/jsp/crud-application.shtml 6/17
4/1/2020 JSP CRUD Application
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
return users;
}
if (rs.next()) {
userBean.setId(rs.getInt("userid"));
userBean.setfName(rs.getString("firstname"));
userBean.setlName(rs.getString("lastname"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return userBean;
}
}
UserHandler.java
https://www.roseindia.net/jsp/crud-application.shtml 7/17
4/1/2020 JSP CRUD Application
package net.roseindia.handler;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.roseindia.dao.UserDao;
import net.roseindia.bean.UserBean;
public UserHandler() {
super();
dao = new UserDao();
}
https://www.roseindia.net/jsp/crud-application.shtml 8/17
4/1/2020 JSP CRUD Application
dao.editUser(user);
request.setAttribute("user", user);
redirect = UserRecord;
System.out.println("Record updated Successfully");
} else if (action.equalsIgnoreCase("listUser")){
redirect = UserRecord;
request.setAttribute("users", dao.getAllUsers());
} else {
redirect = INSERT;
}
RequestDispatcher rd = request.getRequestDispatcher(redirect);
rd.forward(request, response);
}
user.jsp
https://www.roseindia.net/jsp/crud-application.shtml 9/17
4/1/2020 JSP CRUD Application
edit.jsp
listUser.jsp
https://www.roseindia.net/jsp/crud-application.shtml 10/17
4/1/2020 JSP CRUD Application
</tr>
<%
}
//}
%>
</table>
<p><a href="UserHandler?action=insert">Add User</a></p>
</body>
</html>
web.xml
https://www.roseindia.net/jsp/crud-application.shtml 11/17
4/1/2020 JSP CRUD Application
Output
When you will execute the above example you will get the output as follows :
2. After executing this example the user.jsp page will be opened from here you can insert the new record
and view the record's available in the table.
3. After providing the fields value ( I have inserted two records) when you will click on the submit button, in
the above image, the page will be forwarded to the listUser.jsp as follows :
https://www.roseindia.net/jsp/crud-application.shtml 12/17
4/1/2020 JSP CRUD Application
In the above image you can see a hyperlink "Add User", this is for adding more new user as well as there
are two more hyperlinks "Update" and "Delete" update is used for editing the existing record (discussed
later) and the delete is used for deleting the existing record (discussed later).
And when you will see the database table you will see these two records are also added into the table as
follows :
4. Updating record : When you will click on the hyperlink Update shown at the listUser.jsp page an interface
for updating the corresponding record, such as I want to edit the record with user ID 1, will be opened as
follows :
5. Here You can alter the record of user ID 1, such as I have changed the Last Name from Singh to Roy
and keep the Deepak as it is then the output will be as follows :
https://www.roseindia.net/jsp/crud-application.shtml 13/17
4/1/2020 JSP CRUD Application
And the database table after update will be look like as follows :
6. Now I want to delete the record with of User ID = 2 So, I have click on the hyperlink delete in the second
row whose id is 2 the record is deleted and then after the remaining list is as follows :
After downloading this war file simply rename it to the jspServletJdbcExample and import it into your
eclipse and run this example.
Ads
https://www.roseindia.net/jsp/crud-application.shtml 14/17