We can easily create CRUD Example in JSP. Here, we are using DAO files for database and JSTL for traversing records.

Download jstl.jar and mysql-connector.jar

Download jstl1.2.jar file
Download mysql-connector.jar


Download SQL File to Import in MySQL

Download SQL File


Download Project

download CRUD project in JSP


CRUD Example

Directory Structure in Eclipse

JSP CRUD Example
index.jsp

  
  
  
  
JSP CRUD Example

JSP CRUD Example

Add User
View Users

  

adduserform.jsp

  
  
  
  
Add User Form  
  
  
  
  
  
  

userform.html

View All Records

Add New User

 

 

Name:
Password:
Email:
Sex: Male Female
Country:

 


adduser.jsp

<%@page import="com.codebridgeplus.dao.UserDao"%>  
  
  
  
<% int i=UserDao.save(u); if(i>0){  
response.sendRedirect("adduser-success.jsp");  
}else{  
response.sendRedirect("adduser-error.jsp");  
}  
%>

User.java

package com.codebridgeplus.bean;  
public class User {  
private int id;  
private String name,password,email,sex,country;  
//generate getters and setters  
}

UserDao.java

package com.codebridgeplus.dao;  
import java.sql.*;  
import java.util.ArrayList;  
import java.util.List;  
import com.javatpoint.bean.User;  
public class UserDao {  
  
public static Connection getConnection(){  
    Connection con=null;  
    try{  
        Class.forName("com.mysql.jdbc.Driver");  
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","","");  
    }catch(Exception e){System.out.println(e);}  
    return con;  
}  
public static int save(User u){  
    int status=0;  
    try{  
        Connection con=getConnection();  
        PreparedStatement ps=con.prepareStatement(  
"insert into register(name,password,email,sex,country) values(?,?,?,?,?)");  
        ps.setString(1,u.getName());  
        ps.setString(2,u.getPassword());  
        ps.setString(3,u.getEmail());  
        ps.setString(4,u.getSex());  
        ps.setString(5,u.getCountry());  
        status=ps.executeUpdate();  
    }catch(Exception e){System.out.println(e);}  
    return status;  
}  
public static int update(User u){  
    int status=0;  
    try{  
        Connection con=getConnection();  
        PreparedStatement ps=con.prepareStatement(  
"update register set name=?,password=?,email=?,sex=?,country=? where id=?");  
        ps.setString(1,u.getName());  
        ps.setString(2,u.getPassword());  
        ps.setString(3,u.getEmail());  
        ps.setString(4,u.getSex());  
        ps.setString(5,u.getCountry());  
        ps.setInt(6,u.getId());  
        status=ps.executeUpdate();  
    }catch(Exception e){System.out.println(e);}  
    return status;  
}  
public static int delete(User u){  
    int status=0;  
    try{  
        Connection con=getConnection();  
        PreparedStatement ps=con.prepareStatement("delete from register where id=?");  
        ps.setInt(1,u.getId());  
        status=ps.executeUpdate();  
    }catch(Exception e){System.out.println(e);}  
  
    return status;  
}  
public static List getAllRecords(){  
    List list=new ArrayList();  
      
    try{  
        Connection con=getConnection();  
        PreparedStatement ps=con.prepareStatement("select * from register");  
        ResultSet rs=ps.executeQuery();  
        while(rs.next()){  
            User u=new User();  
            u.setId(rs.getInt("id"));  
            u.setName(rs.getString("name"));  
            u.setPassword(rs.getString("password"));  
            u.setEmail(rs.getString("email"));  
            u.setSex(rs.getString("sex"));  
            u.setCountry(rs.getString("country"));  
            list.add(u);  
        }  
    }catch(Exception e){System.out.println(e);}  
    return list;  
}  
public static User getRecordById(int id){  
    User u=null;  
    try{  
        Connection con=getConnection();  
        PreparedStatement ps=con.prepareStatement("select * from register where id=?");  
        ps.setInt(1,id);  
        ResultSet rs=ps.executeQuery();  
        while(rs.next()){  
            u=new User();  
            u.setId(rs.getInt("id"));  
            u.setName(rs.getString("name"));  
            u.setPassword(rs.getString("password"));  
            u.setEmail(rs.getString("email"));  
            u.setSex(rs.getString("sex"));  
            u.setCountry(rs.getString("country"));  
        }  
    }catch(Exception e){System.out.println(e);}  
    return u;  
}  
}

adduser-success.jsp

  
  
  
  
Add User Success

Record successfully saved!

 

  

adduser-error.jsp

  
  
  
  
Add User Error

Sorry, an error occurred!

 

  

viewusers.jsp

  
  
  
  
  
View Users  
  
  
  
<%@page import="com.codebridgeplus.dao.UserDao,com.codebridgeplus.bean.*,java.util.*"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

Users List

<%
List list=UserDao.getAllRecords();
request.setAttribute(“list”,list);
%>


Id Name Password Email Sex Country Edit Delete
${u.getId()} ${u.getName()} ${u.getPassword()} ${u.getEmail()} ${u.getSex()} ${u.getCountry()} Edit Delete

Add New User

  

editform.jsp

  
  
  
  
Edit Form  
  
  
<%@page import="com.codebridgeplus.dao.UserDao,com.codebridgeplus.bean.User"%>  
  
<% String id=request.getParameter("id"); User u=UserDao.getRecordById(Integer.parseInt(id)); %>

Edit Form

“/>

Name: “/>
Password: “/>
Email: “/>
Sex: Male
Female
Country:
  

edituser.jsp

<%@page import="com.codebridgeplus.dao.UserDao"%>  
  
  
<% int i=UserDao.update(u); response.sendRedirect("viewusers.jsp"); %>

deleteuser.jsp

<%@page import="com.codebridgeplus.dao.UserDao"%>  
  
  
<% UserDao.delete(u); response.sendRedirect("viewusers.jsp"); %>

Download Project

download CRUD project in JSP


Output

JSP CRUD Example 1 JSP CRUD Example 2 JSP CRUD Example 3 JSP CRUD Example 4 JSP CRUD Example 5 JSP CRUD Example 6 JSP CRUD Example 7