使用jsp,servlets,java

时间:2018-06-18 08:55:29

标签: java jsp servlets

您好我更新了我的数据库中的元素。似乎我在数据库中的列中的值不会出现。我需要它,以便我可以更新我的数据库,但这是我得到的this is the what it displays in my jsp page     {

这是我的Servlet

empServices empserv= new empServices();

if (request.getParameter("process") !=null && request.getParameter("process").equals("updemps")) 

    {


empgs.addAll(empserv.updAll(request.getParameter("serialS")));
            request.setAttribute("empdetails", empgs);
            RequestDispatcher updrd = request.getRequestDispatcher("updDetails.jsp");
            updrd.forward(request, response);


    }

else if (request.getParameter("process") != null && request.getParameter("process").equals("edit"))
        {
            //some process here

        }

这是我的DAO,这部分将把数据库中的内容带到jsp页面希望

public ArrayList<empGetSet> getupdemp()
{

    ArrayList<empGetSet> empdtls = new ArrayList<empGetSet>();

    try {

        Connection conn = getConnection();
        String updsql = "UPDATE csemp SET Fname=?,Lname=?,NameResource=?,JRSS=?,Band=?,Acct=?,PMPSeat=?,SeatJRSS=?,OpenSeatDesc=?,ReqSkills=?,ReqBand=?,DReject=?,RReject=?,DetActionPlan=?,DataCompletion=?,Status=? WHERE SerialNumber=?";
        PreparedStatement psupd= conn.prepareStatement(updsql);
        ResultSet rsupd = psupd.executeQuery();

        while (rsupd.next()) 
        {

            empGetSet readgetset = new empGetSet();

            readgetset.setfName(rsupd.getString(1));
            readgetset.setlName(rsupd.getString(2));
            readgetset.setNameRes(rsupd.getString(3));
            readgetset.setSerialS(rsupd.getString(4));
            readgetset.setjRss(rsupd.getString(5));
            readgetset.setBanD(rsupd.getString(6));
            readgetset.setAccT(rsupd.getString(7));
            readgetset.setpMPS(rsupd.getString(8));
            readgetset.setSjRss(rsupd.getString(9));
            readgetset.setOpenSeatDesc(rsupd.getString(10));
            readgetset.setReqSkills(rsupd.getString(11));
            readgetset.setReqBand(rsupd.getString(12));
            readgetset.setdReject(rsupd.getString(13));
            readgetset.setrReject(rsupd.getString(14));
            readgetset.setDetActPlan(rsupd.getString(15));
            readgetset.setDataComplete(rsupd.getString(16));
            readgetset.setStaT(rsupd.getString(17));


            empdtls.add(readgetset);

        }
        psupd.close();
        conn.close();

    }


    catch (Exception e)
    {

        e.printStackTrace();
    }

    return empdtls;

}

这是我在DAO编辑DB内容的另一种方法

public void updEmp (String fName, String lName, String nameRes, String serialS, String jRss, String banD, String accT, String pMPS, String sjRss, String openSeatDesc, String reqSkills, String reqBand, String dReject, String rReject, String detActionPlan, String dataComplete, String staT) 
{
    try {

        Connection conn = getConnection();
        String updsql = "UPDATE csemp SET Fname=?,Lname=?,NameResource=?,JRSS=?,Band=?,Acct=?,PMPSeat=?,SeatJRSS=?,OpenSeatDesc=?,ReqSkills=?,ReqBand=?,DReject=?,RReject=?,DetActionPlan=?,DataCompletion=?,Status=? WHERE SerialNumber=?";
        PreparedStatement psupd = conn.prepareStatement(updsql);

        psupd.setString(1, fName);
        psupd.setString(2, lName);
        psupd.setString(3, nameRes);
        psupd.setString(4, serialS);
        psupd.setString(5, jRss);
        psupd.setString(6, banD);
        psupd.setString(7, accT);
        psupd.setString(8, pMPS);
        psupd.setString(9, sjRss);
        psupd.setString(10, openSeatDesc);
        psupd.setString(11, reqSkills);
        psupd.setString(12, reqBand);
        psupd.setString(13, dReject);
        psupd.setString(14, rReject);
        psupd.setString(15, detActionPlan);
        psupd.setString(16, dataComplete);
        psupd.setString(17, staT);

        psupd.executeUpdate();


            System.out.println("Details Updated!");


        psupd.close();
        conn.close();
    }

    catch (Exception e) 
    {

        e.printStackTrace();
    }


}

这是我的jsp页面

<title>Update Details</title>
</head>
<body>
<div align="right"><a href="loggedin.jsp"><button>Back</button></a></div>

<form action = "empServlet" method ="get">
<c:forEach items ="${empdetails}" var="empdet">

First name:
  <input type="text" name="eFname"  value ="${empdet.fName}" required /> 
  <br>
  <br>
Last name:
  <input type="text" name="eLname" value ="${empdet.lName}" required/>
  <br>
  <br>
Name of Resource (LN ID Format):
  <input type="text" name="eNameRes"  value="${empdet.nameRes}" required/>
  <br>
  <br>
Serial Number:
  <input type="text" name="eSerials"  value ="${empdet.serialS}" required/>
  <br>
  <br>
JRSS:
  <input type="text" name="eJrss" value ="${empdet.jRss}" required/>
  <br>
  <br>
Band:
  <input type="text" name="eBand"  value ="${empdet.banD}" required />
  <br>
  <br>
Account(Proposed):
  <input type="text" name="eAcct" value ="${empdet.accT}" required/>">
  <br>
  <br>
PMP Seat:
  <input type="text" name="ePMPs" value ="${empdet.pMPS}" required/>
  <br>
  <br>
Seat JRSS:
  <input type="text" name="esJRSS"  value ="${empdet.sjRss}" required/>
  <br>
  <br>
Open Seat Description:
  <br>
  <textarea rows="4" cols="50" name ="eOpenSeatDesc" placeholder = "Enter descriptions here" required>${empdet.openSeatDesc}</textarea>
  <br>
   <br>
Required Skills:
  <input type="text" name="eReqSkills" value ="${empdet.reqSkills}" required/>
  <br>
  <br>
Requested Band (low/high):
  <input type="text" name="eReqBand" value ="${empdet.reqBand}" required/>
  <br>
  <br>
Date of Rejection:
  <input type="text" name="eDreject" value ="${empdet.dReject}" required/>
  <br>
  <br>
Reason for Rejection:
  <br>
  <textarea rows="4" cols="50" placeholder = "Enter reasons here" name= "eRreject" required>${empdet.rReject}</textarea>
  <br>
   <br>
Detailed Action Plan:
  <br>
  <textarea rows="4" cols="50" placeholder = "Enter details here" name= "eDetActPlan" required>${empdet.detActPlan}</textarea>
  <br>
  <br>
Target Date of Completion:
  <input type="text" name="eDataComplete"  value ="${empdet.dataComplete}" required/>
  <br>
  <br>
Status (Ongoing,Closed):
  <input type="text" name="eStat"  value ="${empdet.staT}" required/>
  <br><br>
<input type = "submit" value = "Submit" >

</c:forEach>
</form>
</body>
</html>

更新更新~~~ *我终于显示了内容:)(我很高兴)我可以问另一个问题的朋友吗?我怎样才能真正更新这个。我的意思是当我点击更新jsp上的提交按钮时,它会将新值存储到数据库中。顺便说一句,谢谢你们!

3 个答案:

答案 0 :(得分:0)

尝试放

<c:out value='${status.value}' /> 

而不是

<c:out value ="${empdet.fName}"/>

和其他人一样。

关于弹簧绑定和状态变量的澄清......

status是spring bind使用的变量。 https://docs.spring.io/spring/docs/1.2.x/taglib/tag/BindTag.html

此外,您还需要使用spring bind绑定字段值。

更改此

First name:
  <input type="text" name="eFname" required value ="<c:out value ="${empdet.fName}"/>">
  <br>
  <br>

<spring:bind path="empdet.fName">
        First Name: <input 
            type="text"
            value="<c:out value="${status.value}"/>"
            name="<c:out value="${status.expression}"/>">
            <c:if test="${status.error}">
                Error codes:
               <c:forEach items="${status.errorMessages}" var="error">
                    <c:out value="${error}"/>
                </c:forEach>
            </c:if>
    </spring:bind>

答案 1 :(得分:0)

  1. ResultSet用于选择查询。
  2. 如果您正在运行update。请使用executeUpdate()或执行。
  3. 如果有列表,请使用c:for

    String query =“Query”;       PreparedStatement preparedStmt = conn.prepareStatement(query);       preparedStmt.setInt(1,6000);       preparedStmt.setString(2,“Fred”);

      // execute the java preparedstatement
      preparedStmt.executeUpdate();
    

答案 2 :(得分:0)

问题似乎是您无法在JSP中导入JSTL核心标记。还要确保获取jar文件。在JSP的顶部添加以下行。

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

还要确保为JSP启用了EL(表达式语言)。如何测试EL是否被禁用/忽略?尝试一个简单的表达式,看它是否产生结果(启用)或按原样打印(忽略)。例如${3 + 4}必须打印结果7

最后一个,为了打印你真的不需要使用<c:out value="${empdet.fName}"/>只需使用${empdet.fName}仍然会给你相同的结果,所以试试这个而不必导入JSTL,{{1 }}。但请确保在范围(页面/请求/会话/应用程序)中设置<input value="${empdet.fName}" type="text"/>