❮ Previous Next ❯

JSP CRUD Example

This is a simple CRUD(Create Read Update Delete) application which is a en employee management web application using JSP, JSTL, and Oracle. Here we are using Eclipse IDE.

index.jsp

<html>  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
<title>JSP CRUD Example</title>  
</head>  
<body>  
<h1>JSP CRUD Example</h1>  
<a href="addemployeeform.jsp">Add Employee</a>  
<a href="viewemployees.jsp">View Employee</a>  
</body>  
</html>

addemployeeform.jsp

<html>  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
<title>Add Employee Form</title>  
</head>  
<body>  
<jsp:include page="employeeform.html"></jsp:include>  
</body>  
</html>

employeeform.html

<html>
<body>
<a href="viewemployees.jsp">View All Records</a><br/>  
<h1>Add New Emplyee</h1>  
<form action="addemployee.jsp" method="post">  
<table>  
<tr><td>Id:</td><td><input type="text" name="id"/></td></tr>  
<tr><td>Name:</td><td>  
<input type="text" name="name"/></td></tr>  
<tr><td>Designation:</td><td>  
<input type="text" name="designation"/></td></tr>
<tr><td>Email:</td><td><input type="email" name="email"/></td></tr>  
<tr><td>Sex:</td><td>  
<input type="radio" name="sex" value="male"/>Male   
<input type="radio" name="sex" value="female"/>Female </td></tr>  
<tr><td>Country:</td><td>  
<select name="country" style="width:155px">  
<option>India</option>  
<option>Pakistan</option>  
<option>USA</option>  
<option>Srilanka</option>  
<option>Other</option>  
</select>  
</td></tr>  
<tr><td colspan="2"><input type="submit" value="Add Employee"/></td></tr>  
</table>  
</form>

</body>

addemployee.jsp

<%@page import="com.java8s.dao.EmployeeDao"%>  
<jsp:useBean id="e" class="com.java8s.bean.Emplyee"></jsp:useBean>  
<jsp:setProperty property="*" name="e"/>  
<%  
int i=EmployeeDao.save(e);  
if(i>0){  
response.sendRedirect("addemployee-success.jsp");  
}else{  
response.sendRedirect("addemployee-error.jsp");  
}  

%>

Employee.java

package com.java8s.bean;  
public class Employee {  
private int id;  
private String name,designation,email,sex,country;  
//generate getters and setters  
}  

EmployeeDao.java

package com.java8s.dao;  
import java.sql.*;  
import java.util.*;    
import com.java8s.bean.Employee;  
public class EmployeeDao {  
  
public static Connection getConnection(){  

Connection con=null;

try{  
        Class.forName("oracle.jdbc.driver.OracleDriver");  
        con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
    }catch(Exception e){System.out.println(e);}  
    return con;  
}  

public static int save(Employee e){  
    int status=0;  
    try{  
        Connection con=getConnection();  
        PreparedStatement ps=con.prepareStatement(  
"insert into register(id,name,designation,email,sex,country) values(?,?,?,?,?,?)");  
        ps.setString(1,e.getId());  
        ps.setString(2,e.getName());  
ps.setString(3,e.getDesignation());  
        ps.setString(4,e.getEmail());  
        ps.setString(5,e.getSex());  
        ps.setString(6,e.getCountry());  
        status=ps.executeUpdate();  
    }catch(Exception e){System.out.println(e);}  
    return status;  
}  
public static int update(Employee e){  
    int status=0; 
    try{  
        Connection con=getConnection();  
        PreparedStatement ps=con.prepareStatement(  
"update register set name=?,designation=?,email=?,sex=?,country=? where id=?");  
ps.setInt(1,e.getId());          
ps.setString(2,e.getName());  
        ps.setString(3,e.getDesignation());  
        ps.setString(4,e.getEmail());

ps.setString(5,e.getSex());  
        ps.setString(6,e.getCountry());  
        
        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,e.getId());  
        status=ps.executeUpdate();  
    }catch(Exception e){System.out.println(e);}  
  
    return status;  
}  
public static List<User> getAllRecords(){  
    List<User> list=new ArrayList<Employee>();  
      
    try{  
        Connection con=getConnection();  
        PreparedStatement ps=con.prepareStatement("select * from register");  
        ResultSet rs=ps.executeQuery();  
        while(rs.next()){  
            Employee e=new Employee();  
            e.setId(rs.getInt("id"));  
            e.setName(rs.getString("name"));  
            e.setDesignation(rs.getString("designation"));  
            e.setEmail(rs.getString("email"));  
            e.setSex(rs.getString("sex"));  
            e.setCountry(rs.getString("country"));

            list.add(e);  
        }  
    }catch(Exception e){System.out.println(e);}  
    return list;  
}  
public static Employee getRecordById(int id){  
    Employee e=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()){  
            e=new Employee();  
            e.setId(rs.getInt("id"));  
            e.setName(rs.getString("name"));  
            e.setPassword(rs.getString("password"));  
            e.setEmail(rs.getString("email"));  
            e.setSex(rs.getString("sex"));  
            e.setCountry(rs.getString("country"));  
        }  
    }catch(Exception e){System.out.println(e);}  
    return e;  
}  
}  

addemployee-success.jsp

<html>  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
<title>Add Employee Success</title>  
</head>  
<body>  
<p>Record successfully saved!</p>  
<jsp:include page="employeeform.html"></jsp:include>  
</body>  

</html>

addemployee-error.jsp

<html>  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
<title>Add Employee Error</title>  
</head>  
<body>  
 <p>Sorry!! error occurred!</p>  
<jsp:include page="employeeform.html"></jsp:include>  
</body>  
</html>

viewemployees.jsp

<html>  
<%@page import="com.java8s.dao.EmployeeDao,com.java8s.bean.*,java.util.*"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>View Users</title>  
</head>  
<body>  
<h1>Employees List</h1>  
<%  
List<Employee> list=EmployeeDao.getAllRecords();  
request.setAttribute("list",list);  
%>  
<table border="1" width="90%">  
<tr><th>Id</th><th>Name</th><th>Password</th><th>Email</th>  
<th>Sex</th><th>Country</th><th>Edit</th><th>Delete</th></tr>  
<c:forEach items="${list}" var="e">  
<tr><td>${e.getId()}</td><td>${e.getName()}</td><td>${e.getPassword()}</td>  
<td>${e.getEmail()}</td><td>${e.getSex()}</td><td>${e.getCountry()}</td>  
<td><a href="editform.jsp?id=${e.getId()}">Edit</a></td>  
<td><a href="deleteemployee.jsp?id=${e.getId()}">Delete</a></td></tr>  
</c:forEach>  
</table>  
<br/><a href="addemployeeform.jsp">Add New Employee</a>  
</body>  
</html>

editform.jsp

<html>  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
<title>Edit Form</title>  
</head>  
<body>  
<%@page import="com.java8s.dao.EmployeeDao,com.java8s.bean.Employee"%>

<%  
String id=request.getParameter("id");  
Employee e=EmployeeDao.getRecordById(Integer.parseInt(id));  
%>  
  
<h1>Edit Form</h1>  
<form action="editemployee.jsp" method="post">  
<input type="hidden" name="id" value="<%=e.getId() %>"/>  
<table>  
<tr><td>Name:</td><td>  
<input type="text" name="name" value="<%= e.getName()%>"/></td></tr>  
<tr><td>Password:</td><td>  
<input type="text" name="designation" value="<%= e.getDesignation()%>"/></td></tr>  
<tr><td>Email:</td><td>  
<input type="email" name="email" value="<%= e.getEmail()%>"/></td></tr>  
<tr><td>Sex:</td><td>  
<input type="radio" name="sex" value="male"/>Male   
<input type="radio" name="sex" value="female"/>Female </td></tr>  
<tr><td>Country:</td><td>  
<select name="country">  
<option>India</option>  
<option>Pakistan</option>  
<option>USA</option>  
<option>Srilanka</option>  
<option>Other</option>  
</select>  
</td></tr>  
<tr><td colspan="2"><input type="submit" value="Edit Employee"/></td></tr>  
</table>  
</form>  
</body>  

</html>

editemployee.jsp

<%@page import="com.java8s.dao.EmployeeDao"%>  
<jsp:useBean id="e" class="com.java8s.bean.Employee"></jsp:useBean>  
<jsp:setProperty property="*" name="e"/>  
<%  
int i=EmployeeDao.update(e);  
response.sendRedirect("viewemployees.jsp");  
%>

deleteemployee.jsp

<%@page import="com.java8s.dao.EmployeeDao"%>  
<jsp:useBean id="e" class="com.java8s.bean.Employee"></jsp:useBean>  
<jsp:setProperty property="*" name="e"/>  
<%  
EmployeeDao.delete(e);  
response.sendRedirect("viewemployees.jsp"); %> 


❮ Previous Next ❯


Follow Us




Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved