Databese Connectivity concept with Servlet
CURD-Operation
=================================================================================
//index.html
//ConnectivityUtility .java
/EmpBean.java
// EmpDao.java
//SaveServlet.java
//ViewServlet.java
//EditServlet.java
//EditServlet1.java
//DeleteServlet
================================================================================
//index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="#E6E6FA" text="whilte">
<h1><center>Add Employee Information</center></h1>
<form action="SaveServlet" method="post">
<table border="1" align="center">
<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="text" name="email"></td></tr>
<tr><td>Country:</td><td>
<select name="country">
<option>India</option>
<option>Bangladesh</option>
<option>Pakistan</option>
<option>Srilanka</option>
</select>
<tr><td><input type="submit" value="SaveData"></td></tr>
</table>
</form>
<h1><center><a href="ViewServlet">View Employee Information</a></center></h1>
</body>
</html>
================================================================================
//ConnectivityUtility .java
package connecivityutil;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectivityUtility {
private ConnectivityUtility()
{
}
static Connection con=null;
static
{
try
{
Class.forName("org.postgresql.Driver");
con=DriverManager.getConnection("jdbc:postgresql://localhost:5433/govind", "postgres", "manager");
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static Connection getConnection()
{
return con;
}
}
===============================================================================
//EmpBean.java
package empbean;
public class EmpBean {
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;
}
}
================================================================================
// EmpDao.java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import connecivityutil.ConnectivityUtility;
import empbean.EmpBean;
public class EmpDao {
public static int save(EmpBean e)
{
int x=0;
try
{
Connection con = ConnectivityUtility.getConnection();
PreparedStatement ps=con.prepareStatement("insert into servcurd (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());
x=ps.executeUpdate();
}
catch (Exception e1) {
e1.printStackTrace();
}
return x;
}
=================================================================================
public static List<EmpBean> viewAllData()
{
List<EmpBean> list=new ArrayList<EmpBean>();
try
{
Connection con=ConnectivityUtility.getConnection();
PreparedStatement ps=con.prepareStatement("select * from servcurd");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
EmpBean e=new EmpBean();
e.setId(rs.getInt(1));
e.setName(rs.getString(2));
e.setPassword(rs.getString(3));
e.setCountry(rs.getString(4));
e.setEmail(rs.getString(5));
list.add(e);
}
}
catch (Exception e1) {
e1.printStackTrace();
}
return list;
}
================================================================================
public static int update(EmpBean e)
{
int x=0;
try
{
Connection con=ConnectivityUtility.getConnection();
PreparedStatement ps=con.prepareStatement("update servcurd 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());
x=ps.executeUpdate();
}
catch (Exception e1) {
e1.printStackTrace();
}
return x;
}
================================================================================
public static EmpBean getDataById(int id)
{
EmpBean e=new EmpBean();
try
{
Connection con=ConnectivityUtility.getConnection();
PreparedStatement ps=con.prepareStatement("select * from servcurd 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));
}
}
catch (Exception e1) {
e1.printStackTrace();
}
return e;
}
================================================================================
public static int delete(int id)
{
int x=0;
try
{
Connection con = ConnectivityUtility.getConnection();
PreparedStatement ps=con.prepareStatement("delete from servcurd where id=?");
ps.setInt(1, id);
x=ps.executeUpdate();
}
catch (Exception e) {
e.printStackTrace();
}
return x;
}
}
================================================================================
//SaveServlet.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.EmpDao;
import empbean.EmpBean;
public class SaveServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
String name=request.getParameter("name");
String password=request.getParameter("password");
String email=request.getParameter("email");
String country=request.getParameter("country");
EmpBean e=new EmpBean();
e.setName(name);
e.setPassword(password);
e.setEmail(email);
e.setCountry(country);
int x = EmpDao.save(e);
if(x>0)
{
out.println("<h1><center>Data Save Successfully</center></h1>");
RequestDispatcher rd = request.getRequestDispatcher("index.html");
rd.include(request, response);
}
else
{
out.println("<h1><center>Data Not Saved Successfully</center></h1>");
}
}
}
=================================================================================
//ViewServlet .java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.EmpDao;
import empbean.EmpBean;
public class ViewServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<body bgcolor='#E6E6FA'>");
out.print("<h1><center>Employee List</center></h1>");
List<EmpBean> list = EmpDao.viewAllData();
out.println("<table border='1' width='100%' align='center'>");
out.print("<tr><th>EmpId</th><th>EmpName</th><th>Password</th><th>Email</th><th>Country</th><th>Edit</th><th>Delete</th></tr>");
for(EmpBean e:list)
{
out.print("<tr><td>"+e.getId()+"</td><td>"+e.getName()+"</td><td>"+e.getPassword()+"</td><td>"+e.getEmail()+"</td><td>"+e.getCountry()+"</td><td><a href='EditServlet?id="+e.getId()+"'>edit</a></td><td><a href='DeleteServlet?id="+e.getId()+"'>delete</a></td></tr>");
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
================================================================================
//EditServlet.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.EmpDao;
import empbean.EmpBean;
public class EditServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
out.println("<h1><center>Update Employee</center></h1>");
String eid=request.getParameter("id");
int id = Integer.parseInt(eid);
EmpBean e = EmpDao.getDataById(id);
out.print("<html>");
out.print("<body bgcolor='#E6E6FA'>");
out.print("<form action='EditServlet1' method='post'>");
out.print("<table align='center'>");
out.print("<tr><td></td><tr><input type='hidden' name='id' value='"+e.getId()+"'/></td></tr>");
out.print("<tr><td>Name:</td><td><input type='text' name='name' value='"+e.getName()+"'/><td><tr>");
out.print("<tr><td>Password:</td><td><input type='text' name='password' value='"+e.getPassword()+"'/><td><tr>");
out.print("<tr><td>Email:</td><td><input type='text' name='email' value='"+e.getEmail()+"'/><td><tr>");
out.print("<tr><td>Country:</td><td>");
out.print("<select name='country'>");
out.print("<option>India</option>");
out.print("<option>Bangladesh</option>");
out.print("<option>Pakistan</option>");
out.print("<option>Srilanka</option>");
out.print("</select>");
out.print("</td></tr>");
out.print("<tr><td><input type='submit' value='Edit & Save'/></td></tr>");
out.print("</table>");
out.print("</form>");
out.print("</body>");
out.print("</html>");
out.close();
}
}
================================================================================
//EditServlet1.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.EmpDao;
import empbean.EmpBean;
public class EditServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
String eid=request.getParameter("id");
int id=Integer.parseInt(eid);
String name=request.getParameter("name");
String password=request.getParameter("password");
String email=request.getParameter("email");
String country=request.getParameter("country");
EmpBean e=new EmpBean();
e.setId(id);
e.setName(name);
e.setPassword(password);
e.setEmail(email);
e.setCountry(country);
int x = EmpDao.update(e);
if(x>0)
{
response.sendRedirect("ViewServlet");
}
else
{
out.println("! sorry unable to update Record");
}
out.close();
}
}
=================================================================================
//DeleteServlet .java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.EmpDao;
public class DeleteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
String eid=request.getParameter("id");
int id=Integer.parseInt(eid);
out.println("Id="+id);
EmpDao.delete(id);
response.sendRedirect("ViewServlet");
}
}
===============================================================================
1.click on SaveData(Button)
2.click on link View Employee Information
3. click on edit button
Database
Directory-Structure:-
No comments:
Post a Comment