Results 1 to 8 of 8
  1. #1
    suresh_ashok is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Deleting a row in database from JSP Delete Button or Check Box for multiple Row

    Hi,

    I am trying to delete a certain row from database using JSP. In jsp i had retreived all the records from the database and shown to the user and also included the delete button corresponding to each record but not sure how to capture the record of a single row and make it delete.


    Regards
    Suresh

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Deleting a row in database from JSP Delete Button or Check Box for multiple Row

    How are you showing the delete button?
    What is the html for the delete button?
    Does the href (or whatever) have an id representing the id (in the database) of the row to delete?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    suresh_ashok is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Deleting a row in database from JSP Delete Button or Check Box for multiple Row

    <html:submit value="Delete"/> i am using the form submit option for the Delete, such that only one record can be able to delete at the time..

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Deleting a row in database from JSP Delete Button or Check Box for multiple Row

    So there's a form per row?
    Is there an id field in the form then?
    If so then you use that so you know which row to delete.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    suresh_ashok is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Deleting a row in database from JSP Delete Button or Check Box for multiple Row

    Hi tolls,

    I had done with delete using this code
    1. View.jsp
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
    <%@page import="java.sql.*"%>
    <%@page import="com.tracker.database.SqlConnection;"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Delete Users</title>
    </head>
    <%
    SqlConnection connection = new SqlConnection();
    Connection con = connection.getDbConnection();
    String sql = "SELECT * FROM users";
    PreparedStatement ps = con.prepareStatement(sql);
    ResultSet rst = ps.executeQuery();
    int i=0;
    %>
    <body>



    <h1>Will be deleting an Existing User</h1>
    <h4> List of Users</h4>
    <form name=myname method=post
    action="deleteSuccess.jsp">
    <table border="1">
    <tr><td>Select</td>
    <td>EmployeeID</td><td>FirstName</td><td>LastName</td><td>RoleID</td>
    </tr>
    <%
    while(rst.next()){ %>
    <tr>
    <td><input type="checkbox" name="check<%=i%>"
    value=<%=rst.getInt(1)%>></td>
    <td><%=rst.getInt(1)%></td><td><%=rst.getString(2)%></td><td><%=rst.getString(3)%></td><td><%=rst.getInt(4)%></td>
    </tr>
    <%}%>
    </table>
    <input type="submit" value="Delete"></form>
    </body>
    </html>

    2. deleteSuccess.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>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>User Deleted</title>
    </head>
    <%
    String id[] = new String[10];
    for (int i = 0; i < 10; i++) {
    id[i] = request.getParameter("check" + i);
    }
    %>
    <body bgcolor="beige">
    <%
    try {
    Connection conn = null;
    Class.forName("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/tracker", "root", "root");
    ResultSet rs = null;
    Statement st = null;
    st = conn.createStatement();
    for (int a = 0; a < 10; a++) {
    st.executeUpdate("delete from users where empid='" + id[a]
    + "'");
    if(id[a] != null){%>
    <h3><font color="blue">Employee ID : <%= id[a]%> Deleted Successfully</font></h3>
    <% }}
    } catch (SQLException e) {
    System.out.println(e.getMessage());
    }

    %>

    <a href="/TestingMultiple/admin.jsp">Click here to go to Home</a>
    </body>

    </html>

    Here i am able to take EmpId and able to delete it, but now i need to update the entire row how to do ? Please some pointers for that

    Cheers
    Suresh

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Deleting a row in database from JSP Delete Button or Check Box for multiple Row

    When posting code please wrap it in [code] tags [/code].
    Unformatted code is hard to read.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Deleting a row in database from JSP Delete Button or Check Box for multiple Row

    So it deletes?
    OK, I don't understand what you mean by "update the entire row"?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,152
    Rep Power
    6

    Default Re: Deleting a row in database from JSP Delete Button or Check Box for multiple Row

    My interpretation: I now need to create a JSP with which I can update an existing row (in stead of create or delete one).
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Deleting from MySQL database
    By darrenstyles88 in forum New To Java
    Replies: 4
    Last Post: 09-07-2012, 11:19 AM
  2. Deleting multiple lines of text from a file
    By barman in forum New To Java
    Replies: 4
    Last Post: 06-29-2010, 10:12 AM
  3. Deleting rows from a database using JSP
    By Manas Das in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 02-17-2009, 03:31 PM
  4. check record dependency before deleting
    By dennisome in forum JDBC
    Replies: 9
    Last Post: 11-07-2008, 12:57 PM
  5. Delete Button
    By Zoroxeus in forum JavaServer Faces (JSF)
    Replies: 3
    Last Post: 10-26-2008, 03:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •