hi everyone ..i have 2 jsp page(atm.jsp,delete.jsp) in atm.jsp: i have a table containing values, on click on id value, that number should paste/copy in delete.jsp textbox..
Here is a jsp application that allow the user to click particular record to delete. As the user clicks the particular button, that record will get displayed into another page and allow the user to delete that record and move back to the main page.
1)application.jsp:
<%@ page import="java.sql.*" %> <html> <head> <script language="javascript"> function deleteRecord(id){ var f=document.form; f.method="post"; f.action='delete.jsp?id='+id; f.submit(); } </script> </head> <body> <br><br> <form method="post" name="form"> <table border="1"> <tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr> <% 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 employee"; st = con.createStatement(); ResultSet rs = st.executeQuery(query); %> <% while(rs.next()){ %> <tr><td><%=rs.getString(2)%></td> <td><%=rs.getString(3)%></td> <td><%=rs.getString(4)%></td> <td><%=rs.getString(5)%></td> <td><input type="button" name="delete" value="Delete" style="background-color:red;font-weight:bold;color:white;" onclick="deleteRecord(<%=rs.getString(1)%>);" ></td> </tr> <% } %> <% } catch(Exception e){ e.printStackTrace(); } %> </table> </form> </body> </html>
continue..
2)delete.jsp:
<%@page language="java"%> <%@page import="java.sql.*"%> <form method="post" action="delete.jsp"> <table border="1"> <% String id=request.getParameter("id"); int no=Integer.parseInt(id); int sumcount=0; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); String query = "select * from employee where id='"+no+"'"; Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(query); if(rs.next()){ %> <tr><td>ID: </td><td><input type="text" name="id" value="<%=rs.getString("id")%>"></td></tr> <tr><td>Name:</td><td><input type="text" name="name" value="<%=rs.getString("name")%>"></td></tr> <tr><td>Address:</td><td><input type="text" name="address" value="<%=rs.getString("address")%>"></td></tr> <tr><td>Contact:</td><td><input type="text" name="contact" value="<%=rs.getInt("contactNo")%>"></td></tr> <tr><td>Email:</td><td><input type="text" name="email" value="<%=rs.getString("email")%>"></td></tr> </tr> <tr> <td><input type="submit" name="Submit" value="Delete" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td> </tr> <% } } catch(Exception e){} %> </table> </form> <% int id = Integer.parseInt(request.getParameter("id")); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); Statement st=con.createStatement(); int i=st.executeUpdate("delete from employee where id="+id+""); response.sendRedirect("application.jsp"); } catch (Exception e){ System.out.println(e); } %>
Ads