Delete selected record

Monday, March 8, 2010 Posted by Jagadeesh Manne
in this post we will delete selected record from view.jsp page
for this we will create deleteDetails method in dbclass

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 model.UserBean;
import validation.ValidateForm;
import database.DBClass;
import java.util.List;
/**
*
* @author Jagadeesh
*/
public class ControllerServlet extends HttpServlet {

//declare values to get form values from jsp page
String userName;
String dateOfBirth;
String email;
String phoneNo;
String action;

UserBean bean = new UserBean();
ValidateForm validateform = new ValidateForm();
DBClass dbobject = new DBClass();
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
//get the values from jsp page
userName = request.getParameter("userName");
dateOfBirth = request.getParameter("dateOfBirth");
email = request.getParameter("email");
phoneNo = request.getParameter("phoneNo");
action = request.getParameter("action");
if(action.equals("submit"))
{
//set values to bean.For this call below method
setValuesToBean();

//check all form values are valid or not. send bean object
UserBean checkedbean = validateform.validateData(bean);
if(!checkedbean.getIsValid())
{

//if data is invalid.set bean object in request and pass that request to
//insertupdate.jsp using forward
checkedbean.setAction("submit");
request.setAttribute("error",checkedbean);

RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp");
rd.forward(request, response);
//now display errors in that jsp page
}
else
{
//using DBClass object call insertDetails method and pass bean object
dbobject.insertDetails(bean);
List list = dbobject.getAlldetails();
request.setAttribute("list", list);
//forward to insertupdate page using requestdispatcher
RequestDispatcher rd= request.getRequestDispatcher("view.jsp");
//display a message to client.store message in request object
//forwarding to jsp
rd.forward(request, response);


}
}
if(action.equals("edit"))
{
//get userdetails of particular name
UserBean ubean = dbobject.getDetails(userName);
request.setAttribute("updateuser",ubean);
RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp");
rd.forward(request, response);
}
if(action.equals("update"))
{
setValuesToBean();
UserBean checkedbean = validateform.validateData(bean);
if(!checkedbean.getIsValid())
{

//if data is invalid.set bean object in request and pass that request to
//insertupdate.jsp using forward
checkedbean.setAction("update");
request.setAttribute("error",checkedbean);
RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp");
rd.forward(request, response);
//now display errors in that jsp page
}
else
{
//using DBClass object call insertDetails method and pass bean object
dbobject.UpateDetails(bean,userName);
List list = dbobject.getAlldetails();
request.setAttribute("list", list);
//forward to insertupdate page using requestdispatcher
RequestDispatcher rd= request.getRequestDispatcher("view.jsp");
//display a message to client.store message in request object
//forwarding to jsp
rd.forward(request, response);


}

}
if(action.equals("delete"))
{
//delete userdetails of particular name
dbobject.deleteDetails(userName);
RequestDispatcher rd = request.getRequestDispatcher("view.jsp");
rd.forward(request, response);
}
}
catch(Exception e)
{
out.println(e);
}
finally {
out.close();
}
}
//this method is used to setvalues to bean
public void setValuesToBean()
{
bean.setUserName(userName);
bean.setDateOfBirth(dateOfBirth);
bean.setEmail(email);
bean.setPhoneNo(phoneNo);

}


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}


public String getServletInfo() {
return "Short description";
}

}

create method in DBClass
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.UserBean;

/**
*
* @author Jagadeesh
*/
public class DBClass {
public Connection createConnection() throws ClassNotFoundException,SQLException
{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdbase", "root", "root");
return connection;
}
//we get values from servlet by passing bean object to insertdetails method
public int insertDetails(UserBean bb) throws SQLException, ClassNotFoundException
{
Connection con = createConnection();
PreparedStatement pstmt = con.prepareStatement("insert into userdetails values(?,?,?,?)");
//set values to prepared statement object by getting values from bean object
pstmt.setString(1,bb.getUserName());
pstmt.setString(2,bb.getDateOfBirth());
pstmt.setString(3,bb.getEmail());
pstmt.setString(4,bb.getPhoneNo());
int i = pstmt.executeUpdate();
return i;

}
public List getAlldetails()throws SQLException, ClassNotFoundException
{

Connection con = createConnection();
PreparedStatement pstmt = con.prepareStatement("select * from userdetails");
ResultSet rs = pstmt.executeQuery();
List list = new ArrayList();
while(rs.next())
{
UserBean ubean = new UserBean();
ubean.setUserName(rs.getString(1));
ubean.setDateOfBirth(rs.getString(2));
ubean.setEmail(rs.getString(3));
ubean.setPhoneNo(rs.getString(4));
list.add(ubean);

}
return list;
}
public UserBean getDetails(String uname)throws SQLException, ClassNotFoundException
{
//here we will write code to get a single record from database
Connection con = createConnection();
PreparedStatement pstmt = con.prepareStatement("select * from userdetails where user_name=?");
pstmt.setString(1, uname);
ResultSet rs = pstmt.executeQuery();
List list = new ArrayList();
UserBean ubean = new UserBean();
while(rs.next())
{

ubean.setUserName(rs.getString(1));
ubean.setDateOfBirth(rs.getString(2));
ubean.setEmail(rs.getString(3));
ubean.setPhoneNo(rs.getString(4));
}
ubean.setAction("update");
return ubean;
}
public void UpateDetails(UserBean ubean, String name)throws SQLException, ClassNotFoundException
{

Connection con = createConnection();
PreparedStatement pstmt = con.prepareStatement("update userdetails set date_of_birth=?,e_mail=?,phone_no=? where user_name=? ");
//set values to prepared statement object by getting values from bean object
pstmt.setString(1,ubean.getDateOfBirth());
pstmt.setString(2,ubean.getEmail());
pstmt.setString(3,ubean.getPhoneNo());
pstmt.setString(4,name);
pstmt.executeUpdate();


}
public void deleteDetails(String uname)throws SQLException, ClassNotFoundException
{
//here we will write code to get a single record from database
Connection con = createConnection();
PreparedStatement pstmt = con.prepareStatement("delete * from userdetails where user_name=?");
pstmt.setString(1, uname);

}

}
in next post we will see all files we created
Labels:

Post a Comment