updating selected record

Monday, March 8, 2010 Posted by Unknown
in this post we will create method for uploading record
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);


}
}
}
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";
}

}

creating updatedetails 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();


}
}

run insertupdate.jsp page and insert record.then it will show all records


after selecting one record




after updating record


Labels:
  1. Unable to update the record. after clicking on update it's not calling edit action.
    URL modifies to shows "http://localhost:8080/ProjName/ControllerServlet?action=edit&name=suprb1" and shows blank page

  2. means those values are going to servlet.
    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);
    }
    here put comment for rd.forward and insert this code after getrequest dispatcher
    out.println(ubean.getphoneno() or someother method) then it has to show that value as output.
    let me know what output you got

  3. I printed message on very first line of Edit action but even that is not printing... same thing for delete action.

    here is code snippet:

    if (action.equals("edit"))
    {
    System.out.println("We reached in edit action");
    UserBean ubean = dbObject.getDetails(name);
    request.setAttribute("updateuser",ubean);
    RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp");
    rd.forward(request,response);
    }

  4. put comments for rd.forward or remove rd.forward then only you can identify the mistake. here it dont show anything because you are forwarding to next page.

  5. update and delete functions not working for me.

    i am able to output the correct username, phone, etc to the console but the insertupdate.jsp is pre-populating with the record.

    here is the code
    if(action.equals("edit"))
    {
    //get userdetails of particular name
    UserBean ubean = dbobject.getDetails(userName);
    request.setAttribute("updateuser",ubean);
    RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp");
    System.out.println(ubean.getPhoneNo());
    // rd.forward(request, response);

    }

  6. correction *is NOT pre-populating with the record.

  7. update and delete functions not working for me.

    i am able to output the correct username, phone, etc to the console but the insertupdate.jsp does not pre-populate with the record.

    here is the code
    if(action.equals("edit"))
    {
    //get userdetails of particular name
    UserBean ubean = dbobject.getDetails(userName);
    request.setAttribute("updateuser",ubean);
    RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp");
    System.out.println(ubean.getPhoneNo());
    // rd.forward(request, response);

    }

  8. Did not work:

    Delete
    http://localhost:8080/MVCExample/ControllerServlet?action=delete&userName=DanO
    Blank

    Update:
    http://localhost:8080/MVCExample/ControllerServlet?action=edit&userName=DanOO

    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'user_name' in 'where clause'

  9. Check your database column name "user_name" that should be same in sql query

Post a Comment