Home » Jquery » jquery – Mysql table record is not getting updated using Update query

jquery – Mysql table record is not getting updated using Update query

Posted by: admin February 22, 2020 Leave a comment

Questions:

I have two tables in my database(Database Name:dmsqms) i.e. dmsmembers and dmsfiles. For dmsmembers “zid” is primary key and for dmsfiles “document_name” is primary key. “zid” column is same for both tables. When user do registration then record stores into dmsmembers. When user save form (index.jsp) then this record saves into dmsfiles table. I also have one filelist.jsp where all records are fetched from database using “select * from dmsfiles” query. In filelist.jsp, for each record/row there is one Edit button. When user clicks on “Edit” then respective jsp form opens then user do changes as per need and clicks on “Update” button. Now problem is when user clicks on Update button then it redirects to next page but record is not getting updated. From my point, there may be problem of primary key.

filelistJSP.jsp

<body>
<br>
<center><h3><i>Note: Here you can see all files uploaded by you only.</i></h3></center>
<br>
<br>
<center>
<%
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
%>
<table border="2">
  <tr>
   <th style="width: 20%">Z ID</th>
   <th style="width: 20%">First Name</th>
   <th style="width: 20%">Last Name</th>
   <th style="width: 20%">Mail ID</th>
   ...
   <th class="text-center">Edit Form</th>
   </tr>
<%
  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dmsqms","root","");
   String zid = request.getParameter("zid");
   String sql = "select * from dmsfiles where zid= '"+zid+"'";
   ps = con.prepareStatement(sql);
   rs = ps.executeQuery();
   while(rs.next()){
    %>
   <tr>
   <td><%=rs.getString(1)%></td>              
   <td><%=rs.getString(2)%></td>           
   <td><%=rs.getString(3)%></td>   
   <td><%=rs.getString(4)%></td> 
   <td><%=rs.getString(5)%></td> 
   <td><%=rs.getString(6)%></td> 
   <td><%=rs.getString(7)%></td>
   <td><%=rs.getString(8)%></td>              
   ...
   <td><%=rs.getString(24)%></td>
   <td><a href="DownloadServletClass?fileName=<%=rs.getString(22)%>" class="btn btn-primary">Download</a></td>
   <td><a href="editnew.jsp?zid=<%=rs.getString("zid")%>&documentname=<%=rs.getString(9)%>" class="btn btn-warning">Edit</a></td>
 </tr>
<%
   }
%>    
</table><br>
<a href="home.jsp" class="btn btn-info">Home</a>
</center>
</body>

editnew.jsp

<body>
<br>
<div id="Registercontainer">
<div class="RegForm">    
<div id="back_glob">    
<div id="back_form">
<%
  try{
    String host = "jdbc:mysql://localhost:3306/dmsqms";
    Statement stat = null;
    ResultSet res = null;
    Connection conn = null;
    PreparedStatement stmt = null;
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection(host,"root","");
 %>
 <form action="editprocess.jsp" method="post" enctype="multipart/form-data">
    <%
      stat = conn.createStatement();
      String docName = request.getParameter("documentname");
      String zid = request.getParameter("zid");
      String data = "select * from dmsfiles where zid = '"+zid+"' AND document_name = '"+docName+"'";
      res = stat.executeQuery(data);
      while(res.next()){
     %>
     
     <center><h2>Edit form</h2></center>
     <input type="hidden" name="id" value="<%=res.getString("zid") %>">
      <table width="400px" align="center" border="2">
       <tr>
         <td align="center" colspan="2">Form Details</td>
       </tr>
       <tr>
       <td>Z id</td>
       <td>
       <input type="text" name="zid" value='<%=res.getString(1)%>'/>
       </td>
       </tr>
       <tr>
       <td>First Name</td>
       <td>
       <input type="text" required="" name="firstname" value='<%=res.getString(2)%>'/>
        </td>
        </tr>
        <tr>
        <td>Last Name</td>
        <td>
        <input type="text" required="" name="lastname" value='<%=res.getString(3)%>'/>
        </td>
        </tr>
        
        ...
        
        <%
          }
        %>    
        <tr>
        <td></td>
        <td>
        <input type="hidden" value="update" name="update"/>
        <input type="submit" value="Update" class="btn btn-primary"/>
        </td>
        </tr>
        </table>
        </form>
        <br>
        <center><a href="home.jsp" class="btn btn-info">Home</a></center>
    </div>
    </div>  
    </div>
    </div>
    <%
        }  //try block close
        catch(Exception e){
           System.out.println(e);
        }   
%>
</body>

editprocess.jsp

<body>
<%
String z_id = request.getParameter("zid");
String first_Name = request.getParameter("firstname");
String last_Name = request.getParameter("lastname");
String mail_i_d = request.getParameter("mailid");
String d_iv = request.getParameter("division");

...

String iss_uer = request.getParameter("issuer");
String sta_tus = request.getParameter("status");
String file_Name = "";
String folder_Name = "allfiles";
String pa_th = folder_Name + File.separator + file_Name;
Timestamp add_ed_date = new Timestamp(System.currentTimeMillis()); 

if(request.getParameter("update") != null){
   // out.println("in");
   Connection con = null;
   PreparedStatement ps = null;
                
   try{
     Class.forName("com.mysql.jdbc.Driver");
     con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dmsqms","root","");
     String sql = "update dmsfiles set firstname=?,lastname=?,mailid=?,division=?,department=?,costcentercode=?,document_number=?,document_type=?,document_category=?,document_classification=?,authorised_by=?,fromdate=?,todate=?,document_level=?,document_general=?,serial_number=?,revision_number=?,issuer=?,status=?,filename=?,path=?,added_date=? where document_name='"+doc_Name+"'"; 
      ps = con.prepareStatement(sql);
      // ps.setString(1,z_id);
      ps.setString(1,first_Name);
      ps.setString(2,last_Name);
      ps.setString(3,mail_i_d);
      ps.setString(4,d_iv);
      ps.setString(5,de_pt);
      
      ...
       
      ps.setString(19,iss_uer);
      ps.setString(20,sta_tus);
      ps.setString(21,file_Name);
      ps.setString(22,pa_th);
      ps.setTimestamp(23, add_ed_date);
      int i = ps.executeUpdate();
      con.commit();
      if(i > 0)
      {
       out.println("alert('Record Updated Successfully');");
      }
      else
      {
       out.println("alert('There is a problem in updating Record.');");
      }
      }
      catch(SQLException sql)
      {
        sql.printStackTrace();
      }
      }
%>
<a href="home.jsp" class="btn btn-info">Home</a>
</body>

Exception Occurred:

20-Feb-2020 11:12:43.827 SEVERE [http-nio-8084-exec-1] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [jsp] in context with path [/DMS2] threw exception [An exception occurred processing JSP page /searchResult.jsp at line 97

94:<td align="center"><c:out value="${row.added_date}"/></td>
95:                            
96:<td align="center"><a href="DownloadServletClass?fileName=${row.fileName}" class="btn btn-primary">Download</a></td>
97:<td align="center"><a href="editnew.jsp?zid=<%=rs.getString("zid")%>&documentname=<%=rs.getString(9)%>" class="btn btn-warning">Edit</a></td>
98:                            
99: 
100: 


Stacktrace:] with root cause
 java.lang.NullPointerException
	at org.apache.jsp.searchResult_jsp._jspService(searchResult_jsp.java:371)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

searchResult.jsp

<form name="empForm" method="post">
<div>
<table border="2" style="width:100%" cellpadding="15" cellspacing="3">
 <tr>
  <th>Z ID</th>
  <th>First Name</th>
  <th>Last Name</th>
  <th>Mail ID</th>
  <th>Division</th>
  <th>Department</th>
  <th>Cost Center Code</th>
  <th>Document No.</th>
  <th>Document Name</th>
  <th>Document Type</th>
  <th>Document Category</th>
  ...                      
  <th>Status</th>
  <th>File Name</th>
  <th>Added Date</th>
  <th>Download</th>
  <th>Edit Form</th>
  </tr>
  <tbody>
<%
   Connection con = null;
   PreparedStatement ps = null;
   ResultSet rs = null;
 %>
 <sql:query var="empData" dataSource="${emp}">
   SELECT * FROM dmsfiles emp where 
   emp.document_number like '%<%=request.getParameter("documentnumber")%>%' and
   emp.document_name like '%<%=request.getParameter("documentname")%>%' and
   emp.document_type like '%<%=request.getParameter("Document_Type")%>%' and
   emp.document_category like '%<%=request.getParameter("Document_Category")%>%' and
   emp.document_classification like '%<%=request.getParameter("Document_Classification")%>%'
</sql:query>
<c:forEach var="row" items="${empData.rows}">
  <tr>
   <td align="center"><c:out value="${row.zid}"/></td>
   <td align="center"><c:out value="${row.firstname}"/></td>
   <td align="center"><c:out value="${row.lastname}"/></td>
   <td align="center"><c:out value="${row.mailid}"/></td>
   <td align="center"><c:out value="${row.division}"/></td>
   ...
   <td align="center"><c:out value="${row.revision_number}"/></td>
   <td align="center"><c:out value="${row.issuer}"/></td>
   <td align="center"><c:out value="${row.status}"/></td>
   <td align="center"><c:out value="${row.fileName}"/></td>
   <td align="center"><c:out value="${row.added_date}"/></td>
   <td align="center"><a href="DownloadServletClass?fileName=${row.fileName}" class="btn btn-primary">Download</a></td>
   <td align="center"><a href="editnew.jsp?zid=${row.zid}&document_name=${row.document_name}" class="btn btn-warning">Edit</a></td>
   </tr>
   </c:forEach>
   </tbody>
   </table>  
   </div>    
</form>
How to&Answer: