Searching and Deleting Records from DataBase using Servlets and Jsp's (MVC2).Here we are displaying records in the same page and deleting records from DataBase in the same page operation.
Everyone using the searching records displaying in the next page.It will take some processing time to load next page.To overcome time management we are executing all the actions in same page.
Developing a application Process:
step1: develop a s.jsp file
<form action="s" method="post">
Enter ID:<input type="text" name="id"/><br>
<input type="submit" value="search"
</form>
Step2: develop a Dao class DaoClass.java
Here we are creating a update method and search methods an dimplementaion of that methods and Data interacting with DataBase server.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package a.b;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
/**
*
* @author user
*/
public class DaoClass {
private int i;
public int update(String id,String name,String sal){
try{
int i=0;
Class.forName("com.mysql.jdbc.Driver");
Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/ashok","root","");
PreparedStatement pstmt=con.prepareStatement("update empTable set empName=?,empSalary=? where empId=?");
pstmt.setString(1,name);
pstmt.setString(2,sal);
pstmt.setString(3,id);
i=pstmt.executeUpdate();
}catch(Exception e){
}
return i;
}
public ArrayList get(String id) {
ArrayList list=null;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/ashok","root","");
String query="select * from empTable";
if(!id.equals("")){
query=query+" where empId="+id;
}
PreparedStatement pstmt=con.prepareStatement(query);
ResultSet rs=pstmt.executeQuery();
list=new ArrayList();
while(rs.next()){
JavaBeanOne javaBeanOne=new JavaBeanOne();
javaBeanOne.setEmpId(rs.getString(1));
javaBeanOne.setEmpName(rs.getString(2));
javaBeanOne.setEmpSalary(rs.getString(3));
list.add(javaBeanOne);
}
}catch(Exception e){
}
return list;
}
public int delete(String id){
int i=0;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/ashok","root","");
PreparedStatement pstmt=con.prepareStatement("delete from empTable where empId=?");
pstmt.setString(1,id);
i=pstmt.executeUpdate();
}catch(Exception e){
}
return i;
}
}
step3:develop a JavaBean class JavaBeanOne.java
package a.b;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author user
*/
public class JavaBeanOne {
String empId;
String empName;
String empSalary;
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpSalary() {
return empSalary;
}
public void setEmpSalary(String empSalary) {
this.empSalary = empSalary;
}
}
step4: Develop a action class(Controller) NewServlet.java
here we are creating a dao class object and call the methods .this is for searching records and forwarded to corresponding jsp
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import a.b.DaoClass;
import a.b.JavaBeanOne;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author user
*/
public class NewServlet extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response)
{
response.setContentType("text/html;charset=UTF-8");
try {
String id=request.getParameter("id");
PrintWriter out = response.getWriter();
DaoClass daoClass=new DaoClass();
HttpSession session=request.getSession();
ArrayList list=daoClass.get(id);
session.setAttribute("list",list);
RequestDispatcher rd=request.getRequestDispatcher("d.jsp");
rd.forward(request, response);
}catch (Exception ex) {
}
}
}
Step5:develop a servlet class for deleting records from that page using the Query String
DeleteServle.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import a.b.DaoClass;
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;
/**
*
* @author user
*/
public class DeleteServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
{
response.setContentType("text/html;charset=UTF-8");
String id=request.getParameter("id");
System.out.println("id = in DS" + id);
try {
PrintWriter out = response.getWriter();
DaoClass daoClass=new DaoClass();
int i=daoClass.delete(id);
request.setAttribute("id",id);
request.getRequestDispatcher("ss.jsp").forward(request, response);
} catch(Exception e){
}
}
}
step6:develop a d.jsp file for displaying records
<%@ page import="java.util.Iterator"%>
<%@ page import="a.b.JavaBeanOne"%>
<%@ page import="java.util.ArrayList"%>
<form action="s.jsp">
Enter ID:<input type="text" name="id"/><br>
<input type="submit" value="search">
</form>
<%
String id="";
String name="";
String sal="";
ArrayList list1=(ArrayList)session.getAttribute("list");
if(list1.size()>0){
Iterator i=list1.iterator();
while(i.hasNext()){
JavaBeanOne javaBeanOne=(JavaBeanOne)i.next();
id=javaBeanOne.getEmpId();
name=javaBeanOne.getEmpName();
sal=javaBeanOne.getEmpSalary();
%>
<table border="1" cellpadding="3" cellspacing="1">
<tr>
<td> <%=id%>
</td>
<td> <%=name%>
</td>
<td><%=sal%></td>
<%-- for values are stored in session scope and getting records
<%
session.setAttribute("id",id);
session.setAttribute("name",name);
session.setAttribute("sal",sal);
%>
--%>
<td><a href="d?id=<%=id%>">Delete</a></td>
<td><a href="updatepage.jsp?id=<%=id%>">Update</a></td>
</tr>
</table>
<%
}
} else{
out.println("No records Found Here");
}
%>
step7: develop a ss.jsp file for deleting records page
<%@page import="a.b.JavaBeanOne"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<form action="s.jsp">
Enter ID:<input type="text" name="id"/><br>
<input type="submit" value="search">
</form>
<%=request.getAttribute("id")%> <br><br>is
Deleted record from Db.<br>
<%
String id="";
String name="";
String sal="";
ArrayList list1=(ArrayList)session.getAttribute("list");
if(list1.size()>0){
Iterator i=list1.iterator();
while(i.hasNext()){
JavaBeanOne javaBeanOne=(JavaBeanOne)i.next();
id=javaBeanOne.getEmpId();
name=javaBeanOne.getEmpName();
sal=javaBeanOne.getEmpSalary();
%>
<table border="1" cellpadding="3" cellspacing="1">
<tr>
<td> <%=id%>
</td>
<td> <%=name%>
</td>
<td><%=sal%></td>
<%--
<%
session.setAttribute("id",id);
session.setAttribute("name",name);
session.setAttribute("sal",sal);
%>
--%>
<td><a href="d?id=<%=id%>">Delete</a></td>
<td><a href="updatepage.jsp?id=<%=id%>">Update</a></td>
</tr>
</table>
<%
}
} else{
out.println("No records Found Here");
}
%>
Outputs:
You are not entering any value then we are get a all records from DatBase
suppose yo are click on delete link.that record only deleted from that base.
Click Here For Download source code
Everyone using the searching records displaying in the next page.It will take some processing time to load next page.To overcome time management we are executing all the actions in same page.
Developing a application Process:
step1: develop a s.jsp file
<form action="s" method="post">
Enter ID:<input type="text" name="id"/><br>
<input type="submit" value="search"
</form>
Step2: develop a Dao class DaoClass.java
Here we are creating a update method and search methods an dimplementaion of that methods and Data interacting with DataBase server.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package a.b;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
/**
*
* @author user
*/
public class DaoClass {
private int i;
public int update(String id,String name,String sal){
try{
int i=0;
Class.forName("com.mysql.jdbc.Driver");
Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/ashok","root","");
PreparedStatement pstmt=con.prepareStatement("update empTable set empName=?,empSalary=? where empId=?");
pstmt.setString(1,name);
pstmt.setString(2,sal);
pstmt.setString(3,id);
i=pstmt.executeUpdate();
}catch(Exception e){
}
return i;
}
public ArrayList get(String id) {
ArrayList list=null;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/ashok","root","");
String query="select * from empTable";
if(!id.equals("")){
query=query+" where empId="+id;
}
PreparedStatement pstmt=con.prepareStatement(query);
ResultSet rs=pstmt.executeQuery();
list=new ArrayList();
while(rs.next()){
JavaBeanOne javaBeanOne=new JavaBeanOne();
javaBeanOne.setEmpId(rs.getString(1));
javaBeanOne.setEmpName(rs.getString(2));
javaBeanOne.setEmpSalary(rs.getString(3));
list.add(javaBeanOne);
}
}catch(Exception e){
}
return list;
}
public int delete(String id){
int i=0;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/ashok","root","");
PreparedStatement pstmt=con.prepareStatement("delete from empTable where empId=?");
pstmt.setString(1,id);
i=pstmt.executeUpdate();
}catch(Exception e){
}
return i;
}
}
step3:develop a JavaBean class JavaBeanOne.java
package a.b;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author user
*/
public class JavaBeanOne {
String empId;
String empName;
String empSalary;
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpSalary() {
return empSalary;
}
public void setEmpSalary(String empSalary) {
this.empSalary = empSalary;
}
}
step4: Develop a action class(Controller) NewServlet.java
here we are creating a dao class object and call the methods .this is for searching records and forwarded to corresponding jsp
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import a.b.DaoClass;
import a.b.JavaBeanOne;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author user
*/
public class NewServlet extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response)
{
response.setContentType("text/html;charset=UTF-8");
try {
String id=request.getParameter("id");
PrintWriter out = response.getWriter();
DaoClass daoClass=new DaoClass();
HttpSession session=request.getSession();
ArrayList list=daoClass.get(id);
session.setAttribute("list",list);
RequestDispatcher rd=request.getRequestDispatcher("d.jsp");
rd.forward(request, response);
}catch (Exception ex) {
}
}
}
Step5:develop a servlet class for deleting records from that page using the Query String
DeleteServle.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import a.b.DaoClass;
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;
/**
*
* @author user
*/
public class DeleteServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
{
response.setContentType("text/html;charset=UTF-8");
String id=request.getParameter("id");
System.out.println("id = in DS" + id);
try {
PrintWriter out = response.getWriter();
DaoClass daoClass=new DaoClass();
int i=daoClass.delete(id);
request.setAttribute("id",id);
request.getRequestDispatcher("ss.jsp").forward(request, response);
} catch(Exception e){
}
}
}
step6:develop a d.jsp file for displaying records
<%@ page import="java.util.Iterator"%>
<%@ page import="a.b.JavaBeanOne"%>
<%@ page import="java.util.ArrayList"%>
<form action="s.jsp">
Enter ID:<input type="text" name="id"/><br>
<input type="submit" value="search">
</form>
<%
String id="";
String name="";
String sal="";
ArrayList list1=(ArrayList)session.getAttribute("list");
if(list1.size()>0){
Iterator i=list1.iterator();
while(i.hasNext()){
JavaBeanOne javaBeanOne=(JavaBeanOne)i.next();
id=javaBeanOne.getEmpId();
name=javaBeanOne.getEmpName();
sal=javaBeanOne.getEmpSalary();
%>
<table border="1" cellpadding="3" cellspacing="1">
<tr>
<td> <%=id%>
</td>
<td> <%=name%>
</td>
<td><%=sal%></td>
<%-- for values are stored in session scope and getting records
<%
session.setAttribute("id",id);
session.setAttribute("name",name);
session.setAttribute("sal",sal);
%>
--%>
<td><a href="d?id=<%=id%>">Delete</a></td>
<td><a href="updatepage.jsp?id=<%=id%>">Update</a></td>
</tr>
</table>
<%
}
} else{
out.println("No records Found Here");
}
%>
step7: develop a ss.jsp file for deleting records page
<%@page import="a.b.JavaBeanOne"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<form action="s.jsp">
Enter ID:<input type="text" name="id"/><br>
<input type="submit" value="search">
</form>
<%=request.getAttribute("id")%> <br><br>is
Deleted record from Db.<br>
<%
String id="";
String name="";
String sal="";
ArrayList list1=(ArrayList)session.getAttribute("list");
if(list1.size()>0){
Iterator i=list1.iterator();
while(i.hasNext()){
JavaBeanOne javaBeanOne=(JavaBeanOne)i.next();
id=javaBeanOne.getEmpId();
name=javaBeanOne.getEmpName();
sal=javaBeanOne.getEmpSalary();
%>
<table border="1" cellpadding="3" cellspacing="1">
<tr>
<td> <%=id%>
</td>
<td> <%=name%>
</td>
<td><%=sal%></td>
<%--
<%
session.setAttribute("id",id);
session.setAttribute("name",name);
session.setAttribute("sal",sal);
%>
--%>
<td><a href="d?id=<%=id%>">Delete</a></td>
<td><a href="updatepage.jsp?id=<%=id%>">Update</a></td>
</tr>
</table>
<%
}
} else{
out.println("No records Found Here");
}
%>
Outputs:
You are not entering any value then we are get a all records from DatBase
suppose yo are click on delete link.that record only deleted from that base.
Click Here For Download source code
0 comments:
Post a Comment