A crud application is the most important application for any project growth. We can easily create a Crud Application in Servlet.
Create a “user905” table in the oracle database with auto incrementing id using sequence. Id, name, password, email and country are the five fields in the table.
File:index.html
DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Add New Employee</h1>
<form action="SaveServlet" method="post">
<table>
<tr><td>Name:</td><td><input type="text" name="name"/></td></tr>
<tr><td>Password:</td><td><input type="password" name="password"/></td></tr>
<tr><td>Email:</td><td><input type="email" name="email"/></td></tr>
<tr><td>Country:</td><td>
<select name="country" style="width:150px">
<option>India</option>
<option>USA</option>
<option>UK</option>
<option>Other</option>
</select>
</td></tr>
<tr><td colspan="2"><input type="submit" value="Save Employee"/></td></tr>
</table>
</form>
<br/>
<a href="ViewServlet">view employees</a>
</body>
</html>
File:Emp.java
public class Emp { private int id; private String name,password,email,country; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } }
File:EmpDao.java
import java.util.*; import java.sql.*; public class EmpDao { 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(Emp e){ int status=0; try{ Connection con=EmpDao.getConnection(); PreparedStatement ps=con.prepareStatement( "insert into user905(name,password,email,country) values (?,?,?,?)"); ps.setString(1,e.getName()); ps.setString(2,e.getPassword()); ps.setString(3,e.getEmail()); ps.setString(4,e.getCountry()); status=ps.executeUpdate(); con.close(); }catch(Exception ex){ex.printStackTrace();} return status; } public static int update(Emp e){ int status=0; try{ Connection con=EmpDao.getConnection(); PreparedStatement ps=con.prepareStatement( "update user905 set name=?,password=?,email=?,country=? where id=?"); ps.setString(1,e.getName()); ps.setString(2,e.getPassword()); ps.setString(3,e.getEmail()); ps.setString(4,e.getCountry()); ps.setInt(5,e.getId()); status=ps.executeUpdate(); con.close(); }catch(Exception ex){ex.printStackTrace();} return status; } public static int delete(int id){ int status=0; try{ Connection con=EmpDao.getConnection(); PreparedStatement ps=con.prepareStatement("delete from user905 where id=?"); ps.setInt(1,id); status=ps.executeUpdate(); con.close(); }catch(Exception e){e.printStackTrace();} return status; } public static Emp getEmployeeById(int id){ Emp e=new Emp(); try{ Connection con=EmpDao.getConnection(); PreparedStatement ps=con.prepareStatement("select * from user905 where id=?"); ps.setInt(1,id); ResultSet rs=ps.executeQuery(); if(rs.next()){ e.setId(rs.getInt(1)); e.setName(rs.getString(2)); e.setPassword(rs.getString(3)); e.setEmail(rs.getString(4)); e.setCountry(rs.getString(5)); } con.close(); }catch(Exception ex){ex.printStackTrace();} return e; } public static List<Emp> getAllEmployees(){ List<Emp> list=new ArrayList<Emp>(); try{ Connection con=EmpDao.getConnection(); PreparedStatement ps=con.prepareStatement("select * from user905"); ResultSet rs=ps.executeQuery(); while(rs.next()){ Emp e=new Emp(); e.setId(rs.getInt(1)); e.setName(rs.getString(2)); e.setPassword(rs.getString(3)); e.setEmail(rs.getString(4)); e.setCountry(rs.getString(5)); list.add(e); } con.close(); }catch(Exception e){e.printStackTrace();} return list; } }
File:SaveServlet.java
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("/SaveServlet") public class SaveServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); String name=request.getParameter("name"); String password=request.getParameter("password"); String email=request.getParameter("email"); String country=request.getParameter("country"); Emp e=new Emp(); e.setName(name); e.setPassword(password); e.setEmail(email); e.setCountry(country); int status=EmpDao.save(e); if(status>0){ out.print("
Record saved successfully!
"); request.getRequestDispatcher("index.html").include(request, response); }else{ out.println("Sorry! unable to save record"); } out.close(); } }
File:EditServlet.java
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 EditServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); out.println("
"); String sid=request.getParameter("id"); int id=Integer.parseInt(sid); Emp e=EmpDao.getEmployeeById(id); out.print("
); out.print("
"
); out.print("
"); out.print("Name:"); out.print("Password:/> </td></tr>"); out.print("Email:"); out.print("Country:"); out.print("); out.print("India"); out.print("USA"); out.print("UK"); out.print("Other"