Hi, I'm trying to enable/disable the textbox in the while loop. It works but when i want to update my data, the data are not updated correctly. When i remove the enable/disable function,the data are updated correctly. Is my javascript wrong? How do i correct it? Please help. Thank you. (only the update part is wrong)
JSP: editDepartment.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@page import="java.sql.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <style type="text/css"> </style> <script language="javascript" type="text/javascript"> function enable_text(status) { status=!status; deptName=document.updateDepartment.elements['updateDeptName']; HOD=document.updateDepartment.elements['updateHOD']; for (i=0;i<deptName.length;i++) { if ( document.updateDepartment.others[i].checked != status ) { deptName[i].disabled = status; HOD[i].disabled = status; } } } </script> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body onload='disable_text(false)'> <form name="addDepartment" action="AddDepartment">Add new department : <br> Department name : <input type="text" name="deptTxt" /> <br> HOD name : <input type="text" name="HODTxt" /> <br> <input type="submit" value="Add" /></form> <form name="deleteDepartment" action="DeleteDepartment"><br> <br> Delete department : <br> <% Connection con = null; try { int row=1; Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); con = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:XE", "tay", "tay"); Statement stmt = con.createStatement(); ResultSet rs = stmt .executeQuery("SELECT * FROM DEPARTMENT ORDER BY DEPARTMENT.DEPTNAME"); out.println("<table id=delDept>"); out.println("<tr>"); out.println("<td>Delete<td>deptID<td>deptName<td>HOD"); out.println("</tr>"); while (rs.next()) { out.println("<td><input type='checkbox' name='chk' value='"+rs.getString("DEPTID")+"'>"); out.println("<td>"+rs.getString("DEPTID")); out.println("<td>"+rs.getString("DEPTNAME")); out.println("<td>"+rs.getString("HOD")); out.println("</tr>"); row = row + 1; } out.println("</table>"); out.println("<input type=hidden name=counterloop value='" + (row-1) + "'>"); stmt.close(); rs.close(); //con.close(); } catch (Exception e) { System.out.println(e); } %> <br> <input type="submit" value="Delete" /> </form> <form name="updateDepartment" action="UpdateDepartment"> <br><br> Update department : <br> <% try { int row2=1; Statement stmt1 = con.createStatement(); ResultSet rs1 = stmt1 .executeQuery("SELECT * FROM DEPARTMENT ORDER BY DEPARTMENT.DEPTNAME"); out.println("<table id=updDept>"); out.println("<tr>"); out.println("<td>Select<td>deptID<td>deptName<td>HOD"); out.println("</tr>"); while (rs1.next()) { out.println("<tr>"); out.println("<td><input type='checkbox' name='others' onclick='enable_text(this.checked)'>"); //out.println("<td><input type='checkbox' name='others'>"); out.println("<td><input type=hidden name=updateDeptID value='"+rs1.getString("DEPTID")+"'><input type=text name=updateDeptID disabled='true' value='"+rs1.getString("DEPTID")+"'>"); out.println("<td><input type=text name=updateDeptName disabled='true' value='"+ rs1.getString("DEPTNAME")+"'>"); out.println("<td><input type=text name=updateHOD disabled='true' value='"+rs1.getString("HOD")+"'>"); out.println("</tr>"); row2=row2+1; } out.println("</table>"); out.println("<input type=hidden name=counterloop2 value='"+ (row2-1) + "'>"); stmt1.close(); rs1.close(); con.close(); } catch (Exception e) { System.out.println(e); } %> <br> <input type="submit" value="Update" /> </form> </body> </html>
servlet: UpdateDepartment.java
package testpackage;
import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
/** * Servlet implementation class UpdateDepartment */ public class UpdateDepartment extends HttpServlet { private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String deptID[] = request.getParameterValues("updateDeptID"); String deptName[] = request.getParameterValues("updateDeptName"); String HOD[] = request.getParameterValues("updateHOD"); Integer row = Integer.parseInt(request.getParameter("counterloop2")); System.out.println(row); Connection con = null; //if (deptID != null && deptID.length != 0) //{ for(int i=0;i<row;i++) { try { System.out.println(deptID[i]); System.out.println(deptName[i]); System.out.println(HOD[i]); Class.forName("oracle.jdbc.driver.OracleDriver") .newInstance(); con = DriverManager .getConnection( "jdbc:oracle:thin:@localhost:1521:XE", "tay", "tay"); Statement stmt = con.createStatement(); stmt.executeUpdate("UPDATE DEPARTMENT SET DEPTNAME='"+deptName[i] +"', HOD='"+HOD[i]+"' WHERE DEPTID='"+ deptID[i]+"'"); con.close(); } catch (Exception e) { System.out.println(e); } } //} response.sendRedirect("editDepartment.jsp"); }
}
JSP edit application:
1)editapplication.jsp:
<%@ page import="java.sql.*" %> <html> <script> function enable_text(){ var i=0; deptName=document.form.elements['name']; HOD=document.form.elements['address']; for (i=0;i<deptName.length;i++) { if(document.form.check[i].checked){ deptName[i].disabled = true; HOD[i].disabled = true; } } } </script> <form name="form" method="post" action="retcheck.jsp"> <table border="1"> <tr><th></th><th>Name</th><th>Address</th></tr> <% int i=0; Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String db = "test"; String driver = "com.mysql.jdbc.Driver"; String userName ="root"; String password="root"; int sumcount=0; Statement st; try{ Class.forName(driver).newInstance(); con = DriverManager.getConnection(url+db,userName,password); String query = "select * from data"; st = con.createStatement(); ResultSet rs = st.executeQuery(query); %> <% while(rs.next()){ %> <tr><td><input type="checkbox" value="<%= rs.getString("id")%>" onclick='enable_text()' name="check"> <td><input type="text" name="name" value="<%=rs.getString("name")%>"></td> <td><input type="text" name="address" value="<%=rs.getString("address")%>"></td> </tr> <% } %> <% } catch(Exception e){ e.printStackTrace(); } %> </table> <input type="submit" value="Edit"> </form> </html>
2)retcheck.jsp:
<%@ page import="java.sql.*" %> <% String id[]=request.getParameterValues("check"); %> <form name="form" method=post action="up.jsp"> <table border=1> <tr><th></th><th>Name</th><th>Address</th></tr> <% Connection conn = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root"); ResultSet rs = null; Statement st=null; st=conn.createStatement(); for(int a=0;a<id.length;a++){ rs = st.executeQuery("select * from data where id='"+id[a]+"'"); while(rs.next()){ %> <tr> <td><input type="hidden" name="id" value="<%=rs.getString("id")%>" > </td> <td><input type="text" name="name" value="<%=rs.getString("name")%>"></td> <td><input type="text" name="address" value="<%=rs.getString("address")%>"></td></tr> <% } } %> </table> <input type="submit" value="update"> </form>
3)up.jsp:
<%@ page import="java.sql.*" %> <% String id[]=request.getParameterValues("id"); String name[]=request.getParameterValues("name"); String add[]=request.getParameterValues("address"); Connection conn = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root"); Statement st=null; st=conn.createStatement(); for(int i=0;i<id.length;i++){ st.executeUpdate("update data set name='"+name[i]+"',address='"+add[i]+"' where id='"+id[i]+"'"); out.println("Data is updated successfully"); } response.sendRedirect("editapplication.jsp"); %>
Ads