Results 1 to 15 of 15
  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,474
    Rep Power
    21

    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,474
    Rep Power
    21

    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,474
    Rep Power
    21

    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,474
    Rep Power
    21

    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 offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,063
    Rep Power
    8

    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

  9. #9
    sujay.123 is offline Member
    Join Date
    May 2015
    Posts
    4
    Rep Power
    0

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

    hello gimbal2,
    Please post the code if you have it for updating the table rows.It is required for me to complete the project

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,063
    Rep Power
    8

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

    You want me to post the code you should be creating as part of your job as a programmer?

    If you want snippets you can copy/paste, google for them. I suggest you think about it and do your job.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    sujay.123 is offline Member
    Join Date
    May 2015
    Posts
    4
    Rep Power
    0

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

    I am not a programmer and i am doing the project myself at home.I am developing my jsp knowledge.
    I want some help from you and i do have a code which is giving me errors

  12. #12
    sujay.123 is offline Member
    Join Date
    May 2015
    Posts
    4
    Rep Power
    0

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

    Please help me THANKS in advance

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,063
    Rep Power
    8

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

    So now you want me to help you with code you don't even show? In a forum thread which you did not create?

    I am going to do not more than point you to this valuable resource:

    Free Online Version of Core Servlets and JavaServer Pages (Second Edition) in PDF

    and wish you good luck.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    sujay.123 is offline Member
    Join Date
    May 2015
    Posts
    4
    Rep Power
    0

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

    I am writing the code here.
    This is my display.jsp
    <%@ page import="java.io.*,java.util.*,java.sql.*"%>
    <%@ page import="javax.servlet.http.*,javax.servlet.*" %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Employees List</title>
    </head>
    <body>
    <form method="POST" action="insert.jsp">
    <table border="1" width="100%">
    <tr>
    <th>ID</th>
    <th>FNAME</th>
    <th>LNAME</th>
    <th>DOB</th>
    <th>QUALIFICATON</th>
    <th>SALARY</th>
    </tr>
    <%
    try
    {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    String dbURL = "jdbc:oracle:thin:@RSI-CHE-CEDB01:2229:ORA11G84";
    Properties properties = new Properties();
    properties.put("user", "sujay");
    properties.put("password", "sujay");
    properties.put("defaultRowPrefetch", "20");
    Connection conn = DriverManager.getConnection(dbURL, properties);
    if (conn != null) {
    System.out.println("Connected");
    }
    String query="select * from EMPLOYEES";
    Statement stmt=conn.createStatement();
    ResultSet rs=stmt.executeQuery(query);
    while(rs.next())
    {

    %>
    <tr>
    <td><%=rs.getInt("id")%></td>
    <td><%=rs.getString("fname")%></td>
    <td><%=rs.getString("lname")%></td>
    <td><%=rs.getDate("dob")%></td>
    <td><%=rs.getString("qual")%></td>
    <td><%=rs.getInt("salary")%></td>
    </tr>
    <%
    }
    %>
    </table>
    <%
    rs.close();
    stmt.close();
    conn.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    %>
    <input type="Submit" value="Insert" /></form>&nbsp&nbsp&nbsp&nbsp<form method="POST" action="delete.jsp"><input type="Submit" value="Delete" /></form>&nbsp&nbsp&nbsp&nbsp<form method="POST" action="update.jsp"><input type="Submit" value="Update" />
    </form>
    </body>
    </html>
    This is my update.jsp
    a<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
    <%@ page import="java.io.*,java.util.*,java.sql.*"%>
    <%@ page import="javax.servlet.http.*,javax.servlet.*" %>

    <!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>update</title>
    </head>
    <body>
    <h1>update</h1>
    <form name="updateEmployeeForm" action="up.jsp" method="POST">
    <table>
    <tr>
    <td>Id</td>
    <td>Fname</td>
    <td>Lname</td>
    <td>DOB</td>
    <td>Qual</td>
    <td>Salary</td>
    </tr>
    <tr>
    <td><input type="text" name="id" />
    <td><input type="text" name="fname" >
    <td><input type="text" name="lname" />
    <td><input type="text" name="dob" />
    <td><input type="text" name="qual" />
    <td><input type="text" name="salary" />
    </tr>
    <input type="submit" value="Submit"/>
    </table>
    </form>
    <br/><br/>
    <sql:setDataSource var="orlc" driver="oracle.jdbc.driver.OracleDriver"
    url="jdbc:oracle:thin:@RSI-CHE-CEDB01:2229:ORA11G84" user="sujay" password="sujay" />

    <c:choose>
    <c:when test="${not empty param.id
    && not empty param.fname
    && not empty param.lname
    && not empty param.dob
    && not empty param.qual
    && not empty param.salary}">
    <sql:update dataSource="${orlc}" var="newEmployee">
    UPDATE citizens SET fname=?,lname=?,dob=?,qual=?,salary=?, where id=?
    <sql:param value="${param.fname}" />
    <sql:param value="${param.lname}" />
    <sql:param value="${param.dob}" />
    <sql:param value="${param.qual}" />
    <sql:param value="${param.salary}" />
    <sql:param value="${param.id}" />
    </sql:update>
    </c:when>
    <c:otherwise>
    <font>Please enter mandatory information.</font>
    </c:otherwise>
    </c:choose>
    <br/><br/>
    <sql:query dataSource="${orlc}" var="EMPLOYEES">
    SELECT * from EMPLOYEES;
    </sql:query>
    <table border="1">
    <c:forEach var="row" items="${EMPLOYEES.rows}">
    <tr>
    <td><c:out value="${row.id}" /></td>
    <td><c:out value="${row.fname}" /></td>
    <td><c:out value="${row.lname}" /></td>
    <td><c:out value="${row.dob}" /></td>
    <td><c:out value="${row.qual}" /></td>
    <td><c:out value="${row.salary}" /></td>
    </tr>
    </c:forEach>
    </table>
    </body>
    </html>
    Please have a look at that codes and help me with it.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,936
    Rep Power
    20

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

    sujay.123, you need to go through two links:
    BB Code List - Java Programming Forum - Learn Java Programming
    How to ask questions the smart way

    Also, next time you have a question, start your own thread -- they're free.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Deleting from MySQL database
    By darrenstyles88 in forum New To Java
    Replies: 4
    Last Post: 09-07-2012, 10: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, 09: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
  •