Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

CURD Example


CURD stand for Create, Read, Update, Delete. Below is an example of how to perform add, update, delete and view operation in servlet.

Table:Student

For creating a this example below is the directory structure of the program:

table-student

student.html

	
<form action="SaveServlet1" method="post">    
<table>   
<tr><td>Id:</td><td><input type="text" name="id1"/></td></tr>     
<tr><td>Name:</td><td><input type="text" name="name1"/></td></tr>     
<tr><td>Age:</td><td><input type="text" name="age1"/></td></tr>   
<tr><td>Course:</td><td><input type="text" name="course1"/></td></tr>     
<tr><td>City:</td><td>      
<select name="city1" style="width:150px">     
<option>Delhi</option>      
<option>Noida</option>      
<option>Raipur</option>     
<option>Bhopal</option>     
</select>     
</td></tr>      
<tr><td colspan="2"><input type="submit" value="Save"/></td></tr>   
</table>      
</form>   
<br/>     
<a href="ViewServlet1">View Student</a>   
</body>   
</html>  
	

stu.java

	
package com.app.studytonight;

public class stu {      
private String id1, name1, age1, course1, city1;    
public String getId1() {    
    return id1; 
}   
public void setId1(String id1) {    
    this.id1 = id1; 
}   
public String getName1() {  
    return name1;   
}   
public void setName1(String name1) {    
    this.name1 = name1; 
}   
public String getAge1() {   
    return age1;    
}   
public void setAge1(String age1) {  
    this.age1 = age1;   
}   
public String getCourse1() {    
    return course1; 
}   
public void setCourse1(String course1) {    
    this.course1 = course1; 
}   
public String getCity1() {  
    return city1;   
}   
public void setCity1(String city1) {    
    this.city1 = city1; 
}   
}
	

stuDao.java

	
package com.app.studytonight;   
import java.util.*;     
import java.sql.*;      
    
public class stuDao {   
    
    public static Connection getConnection(){   
        Connection con1=null;   
        try{    
            Class.forName("oracle.jdbc.driver.OracleDriver");   
            con1=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");      
        }   
        catch(Exception e1) 
        {   
            System.out.println(e1); 
        }   
        return con1;    
    }   
    public static int save(stu e1){     
        int status=0;   
        try{    
            Connection con1=stuDao.getConnection();     
            PreparedStatement ps1=con1.prepareStatement(    
                         "insert into student(id,name,age,course,city) values (?,?,?,?,?)");    
            ps1.setString(1,e1.getId1());   
            ps1.setString(2,e1.getName1());     
            ps1.setString(3,e1.getAge1());      
            ps1.setString(4,e1.getCourse1());   
            ps1.setString(5,e1.getCity1());     
                
            status=ps1.executeUpdate();     
                
            con1.close();   
        }   
        catch(Exception ex1)    
        {   
            ex1.printStackTrace();  
        }   
            
        return status;      
    }   
    public static int update1(stu e1){      
        int status=0;   
        try{    
            Connection con1=stuDao.getConnection();     
            PreparedStatement ps1=con1.prepareStatement(    
                         "update student set id=?, name=?,age=?,course=?,city=? where id=?");   
            ps1.setString(1,e1.getId1());   
            ps1.setString(2,e1.getName1());     
            ps1.setString(3,e1.getAge1());      
            ps1.setString(4,e1.getCourse1());   
            ps1.setString(5,e1.getCity1());     
                
                
            status=ps1.executeUpdate();     
                
            con1.close();   
        }   
        catch(Exception ex1)    
        {ex1.printStackTrace();}    
            
        return status;      
    }   
    public static int delete1(int id1){     
        int status=0;   
        try{    
            Connection con1=stuDao.getConnection();     
            PreparedStatement ps1=con1.prepareStatement("delete from student where id=?");      
            ps1.setInt(1,id1);      
            status=ps1.executeUpdate();     
                
            con1.close();   
        }catch(Exception e1){e1.printStackTrace();}     
            
        return status;      
    }   
    public static stu getStudentById(int id1){      
        stu e1=new stu();   
            
        try{    
            Connection con1=stuDao.getConnection();     
            PreparedStatement ps1=con1.prepareStatement("select * from student where id=?");    
            ps1.setInt(1,id1);      
            ResultSet rs1=ps1.executeQuery();   
            if(rs1.next()){     
                e1.setId1(rs1.getString(1));    
                e1.setName1(rs1.getString(2));      
                e1.setAge1(rs1.getString(3));   
                e1.setCourse1(rs1.getString(4));    
                e1.setCity1(rs1.getString(5));      
            }   
            con1.close();   
        }catch(Exception ex1){ex1.printStackTrace();}   
            
        return e1;      
    }   
    public static List<stu> getAllStudent(){    
        List<stu> list=new ArrayList<stu>();    
            
        try{    
            Connection con1=stuDao.getConnection();     
            PreparedStatement ps1=con1.prepareStatement("select * from student");   
            ResultSet rs1=ps1.executeQuery();   
            while(rs1.next()){      
                stu e1=new stu();   
                e1.setId1(rs1.getString(1));    
                e1.setName1(rs1.getString(2));      
                e1.setAge1(rs1.getString(3));   
                e1.setCourse1(rs1.getString(4));    
                e1.setCity1(rs1.getString(5));      
                list.add(e1);   
            }   
            con1.close();   
        }catch(Exception e1){e1.printStackTrace();}     
            
        return list;    
    }   
} 
	

SaveServlet.java

	
    package com.app.studytonight;   
import java.io.IOException;     
import java.io.PrintWriter;     
    
import javax.servlet.ServletException;      
import javax.servlet.annotation.WebServlet;     
import javax.servlet.http.HttpServlet;      
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse;      
@WebServlet("/SaveServlet1")    
public class SaveServlet1 extends HttpServlet {     
    protected void doPost(HttpServletRequest request, HttpServletResponse response)     
         throws ServletException, IOException {     
        response.setContentType("text/html");   
        PrintWriter out=response.getWriter();   
        String id1=request.getParameter("id");      
        String name1=request.getParameter("name");      
        String age1=request.getParameter("age");    
        String course1=request.getParameter("course");      
        String city1=request.getParameter("city");      
            
        stu e1=new stu();   
        e1.setId1(id1); 
        e1.setName1(name1);     
        e1.setAge1(age1);   
        e1.setCourse1(course1);     
        e1.setCity1(city1);     
            
        int status=stuDao.save(e1);     
        if(status>0){   
            out.print("<p>Record saved successfully!</p>");     
            request.getRequestDispatcher("student.html").include(request, response);    
        }else{      
            out.println("Sorry! unable to save record");    
        }   
            
        out.close();    
    }   
    
}  
	

EditServlet.java

	
package com.app.studytonight;   
import java.io.IOException;     
import java.io.PrintWriter;     
    
import javax.servlet.ServletException;      
import javax.servlet.annotation.WebServlet;     
import javax.servlet.http.HttpServlet;      
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse;      
@WebServlet("/EditServlet")   
public class EditServlet1 extends HttpServlet {     
    protected void doGet(HttpServletRequest request, HttpServletResponse response)      
           throws ServletException, IOException {   
        response.setContentType("text/html");     
        PrintWriter out=response.getWriter();   
        out.println("<h1>Update Student</h1>");   
        String sid=request.getParameter("id");    
        int id1=Integer.parseInt(sid);      
            
        stu e1=stuDao.getStudentById(id1);      
            
        out.print("<form action='EditServlet2' method='post'>");    
        out.print("<table>");   
        out.print("<tr><td></td><td><input type='hidden' name='id' value='"+e1.getId1()+"'/></td></tr>");     
        out.print("<tr><td>Name:</td><td><input type='text' name='name' value='"+e1.getName1()+"'/></td></tr>");      
        out.print("<tr><td>Age:</td><td><input type='age' name='age' value='"+e1.getAge1()+"'/></td></tr>");      
        out.print("<tr><td>Course:</td><td><input type='course' name='course' value='"+e1.getCourse1()+"'/></td></tr>");      
        out.print("<tr><td>City:</td><td>");      
        out.print("<select name='city' style='width:150px'>");      
        out.print("<option>Delhi</option>");      
        out.print("<option>Noida</option>");      
        out.print("<option>Raipur</option>");     
        out.print("<option>Bhopal</option>");     
        out.print("</select>");     
        out.print("</td></tr>");      
        out.print("<tr><td colspan='2'><input type='submit' value='Edit & Save '/></td></tr>");     
        out.print("</table>");      
        out.print("</form>");   
            
        out.close();    
    }   
}   
	

EditServlet2.java

	
    package com.app.studytonight;   
import java.io.IOException;     
import java.io.PrintWriter;     
    
import javax.servlet.ServletException;      
import javax.servlet.annotation.WebServlet;     
import javax.servlet.http.HttpServlet;      
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse;      
@WebServlet("/EditServlet2")    
public class EditServlet2 extends HttpServlet {     
    protected void doPost(HttpServletRequest request, HttpServletResponse response)     
          throws ServletException, IOException {    
        response.setContentType("text/html");   
        PrintWriter out=response.getWriter();   
            
        String sid=request.getParameter("id");      
        int id1=Integer.parseInt(sid);      
        String name1=request.getParameter("name");      
        String age1=request.getParameter("age");    
        String course1=request.getParameter("course");      
        String city1=request.getParameter("city");      
            
        stu e1=new stu();   
        e1.setId1(id1);     
        e1.setName1(name1);     
        e1.setAge1(age1);   
        e1.setCourse1(course1);     
        e1.setCity1(city1);     
            
        int status=stuDao.update(e1);   
        if(status>0){   
            response.sendRedirect("ViewServlet");   
        }else{      
            out.println("Sorry! unable to update record");      
        }   
            
        out.close();    
    }   
    
}  
	

DeleteServlet.java

	
package com.app.studytonight;
import java.io.IOException;  
import javax.servlet.ServletException;  
import javax.servlet.annotation.WebServlet;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
@WebServlet("/DeleteServlet")  
public class DeleteServlet extends HttpServlet {  
    protected void doGet(HttpServletRequest request, HttpServletResponse response)   
             throws ServletException, IOException {  
        String sid=request.getParameter("id");  
        int id1=Integer.parseInt(sid);   
        stuDao.delete(id1);  
        response.sendRedirect("ViewServlet");  
    }  
} 
	

ViewServlet.java


	
package com.app.studytonight;
import java.io.IOException;  
import java.io.PrintWriter;  
import java.util.List;  
  
import javax.servlet.ServletException;  
import javax.servlet.annotation.WebServlet;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
@WebServlet("/ViewServlet")  
public class ViewServlet extends HttpServlet {  
    protected void doGet(HttpServletRequest request, HttpServletResponse response)   
               throws ServletException, IOException {  
        response.setContentType("text/html");  
        PrintWriter out=response.getWriter();  
        out.println("<a href='student.html'>Add New Student</a>");  
        out.println("<h1>Student List</h1>");  
          
        List<stu> list=stuDao.getAllStudent();  
          
        out.print("<table border='1' width='100%'");  
        out.print("<tr><th>Id</th><th>Name</th><th>Age</th><th>Course</th><th>City</th><th>Edit</th><th>Delete</th></tr>");  
         for(stu e1:list){      
         out.print("<tr><td>"+e1.getId1()+"</td><td>"+e1.getName1()+"</td><td>"+e1.getAge1()+"</td><td>"+e1.getCourse1()+"</td><td>"+e1.getCity1()+"</td><td><a href='EditServlet?id="+e1.getId1()+"'>edit</a></td><td><a href='DeleteServlet?id="+e1.getId1()+"'>delete</a></td></tr>"); 
        out.print("</table>");  
          
        out.close();  
    }  
}
	

Run the Program.

To add a record of fill the fields and click on the save button.

crud-output

Record save successfully. You can add more records.

crud-output

To view the records click on the link View Student. After clicking on the link, below are the records.

crud-output

To view the records click on the link View Student. After clicking on the link, below are the records.

crud-output

To delete any record click on the delete link of that record. The record will be deleted from the database.

crud-output

Data of Kiran has been deleted.

crud-output