Results 1 to 9 of 9
  1. #1
    hassaanid2012 is offline Member
    Join Date
    Apr 2012
    Location
    Pakistan
    Posts
    20
    Rep Power
    0

    Default Delete Record using HTML Hyperlink JSP!!!

    Hello Guys!!!

    I want to delete the record from MS Access DB by using HTML Hyperlink. I have HTML Table that i used to display the contents of DB. DB contains about five records and HTML Table also displays the delete Hyperlink against each record. Now i want that Hyperlink to delete particular record from DB when clicked. I tried to accomplish this by using servlets but i don't understand how to do it. Anybody knows how can i do this? Any help is appreciated..... Thanks!!!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,432
    Rep Power
    18

    Default Re: Delete Record using HTML Hyperlink JSP!!!

    Does the link reference a servlet on your server?
    Does the link contain a parameter referencing the row to delete?
    What code have you got so far, and in what way is it not working?
    Please do not ask for code as refusal often offends.

  3. #3
    hassaanid2012 is offline Member
    Join Date
    Apr 2012
    Location
    Pakistan
    Posts
    20
    Rep Power
    0

    Default Re: Delete Record using HTML Hyperlink JSP!!!

    My hyperlink is linked with servlet here's the code:

    Hyperlink code:
    XML Code:
    <td align="center" colspan="1"><a href="http://localhost:8084/Project_Management_System/DeleteStudent">delete</a></td>
    Code in servlet doGet method:
    Java Code:
    try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url = "jdbc:odbc:studentDSN";
            Connection con = DriverManager.getConnection(url);
            String sql = "SELECT ID FROM students ORDER BY ID DESC";
            PreparedStatement pst = con.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                stdid = rs.getString("ID");
            }
            String sql1 = "DELETE FROM students WHERE ID = "+stdid+";";
            PreparedStatement pst1 = con.prepareStatement(sql1,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
            pst1.executeUpdate();
            JOptionPane.showMessageDialog(null, "Record Successfully Deleted");
            con.close();
            pst.close();
            pst1.close();
            response.sendRedirect("http://localhost:8084/Project_Management_System/faces/ManageStudents.jsp");
        }
        catch(ClassNotFoundException ex){
            Logger.getLogger(AddStudent.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch(SQLException SqlEx){
            System.out.println(SqlEx);
        }
    this code delete the record by asc order from database and also from html table but problem is if i click on delete hyperlink of 4th record in html table it delete the first record. I want to delete the 4th record instead of 1st record. How to delete record of selected row from html table and as well as database? how to reference html table row or table cell value in servlet?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,432
    Rep Power
    18

    Default Re: Delete Record using HTML Hyperlink JSP!!!

    How is the servlet supposed to know which student to delete?
    You have not sent any information in that link to tell the servlet what to delete.
    I would expect a parameter, eg 'id=1', representing the ID of the record to delete.
    Please do not ask for code as refusal often offends.

  5. #5
    hassaanid2012 is offline Member
    Join Date
    Apr 2012
    Location
    Pakistan
    Posts
    20
    Rep Power
    0

    Default Re: Delete Record using HTML Hyperlink JSP!!!

    No this servlet code delete the record in desc order in html table......first sql statement select the id of students in desc order and put the results in result set and next sql statement delete the records in desc order but that's not what i want.....how to get html table cell value without using javascript? because i don't wanna use javascript. if i able to get values of ID then its easy for me to delete record of selected row........help me plz!!!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,432
    Rep Power
    18

    Default Re: Delete Record using HTML Hyperlink JSP!!!

    Well, how do you populate that link?
    Because that's where you'll need to stick the '?id=<whatever student id>' in.
    Then you'll be able to change the servlet to read the id out of the request.
    Please do not ask for code as refusal often offends.

  7. #7
    hassaanid2012 is offline Member
    Join Date
    Apr 2012
    Location
    Pakistan
    Posts
    20
    Rep Power
    0

    Default Re: Delete Record using HTML Hyperlink JSP!!!

    Here's the HTML Table code in my JSP page:
    XML Code:
    <h2>Manage All Students</h2>
            <table border="1" width="800" cellspacing="1" cellpadding="5">
    <thead>
    <tr>
    <td colspan="8"><a href="http://localhost:8084/Project_Management_System/faces/AddStudent.jsp">Add New Student</a></td>
    </tr>
    </thead>
    <tbody>
    <tr>
    <th align="center">Sr #</th>
    <th align="center">Name</th>
    <th align="center">Roll Number</th>
    <th align="center">Ph. Number</th>
    <th align="center">Study Program</th>
    <th align="center">Status</th>
    <th align="center">Update Record</th>
    <th align="center">Delete Record</th>
    </tr>
    <%
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url = "jdbc:odbc:studentDSN";
            Connection con = DriverManager.getConnection(url);
            String sql = "SELECT * FROM students ORDER BY ID DESC";
            PreparedStatement pst = con.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            if(!rs.next()){
            %>
            <tr>
            <td colspan="8">Record not found</td>
            </tr>
            <%
            }
            else{
            do{
            %>
            <tr>
            <td align="center" colspan="1"><%=rs.getString("ID")%></td>
            <td align="center" colspan="1"><%=rs.getString("Name")%></td>
            <td align="center" colspan="1"><%=rs.getString("RollNumber")%></td>
            <td align="center" colspan="1"><%=rs.getString("PhoneNumber")%></td>
            <td align="center" colspan="1"><%=rs.getString("StudyProgram")%></td>
            <td align="center" colspan="1"><%=rs.getString("Status")%></td>
            <td align="center" colspan="1"><a href="http://localhost:8084/Project_Management_System/faces/UpdateStudent.jsp">update</a></td>
            <td align="center" colspan="1"><a href="http://localhost:8084/Project_Management_System/DeleteStudent">delete</a></td>
            </tr>
            <%
            }
            while(rs.next());
            }
            con.close();
            pst.close();
        }
        catch(SQLException SqlEx){
            System.out.println(SqlEx);
        }
    %>
    </tbody>
    </table>
    What modifications are required in this code to achieve what i want.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,432
    Rep Power
    18

    Default Re: Delete Record using HTML Hyperlink JSP!!!

    I've told you.
    You need to add the id to the URL.
    The Update will be a bit more problematic, but you could put multiple forms on the page I suppose, since you appear to be opposed ot using Javascript (which would be the proper solution).
    Please do not ask for code as refusal often offends.

  9. #9
    fadonic is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Default Re: Delete Record using HTML Hyperlink JSP!!!

    Quote Originally Posted by hassaanid2012 View Post
    No this servlet code delete the record in desc order in html table......first sql statement select the id of students in desc order and put the results in result set and next sql statement delete the records in desc order but that's not what i want.....how to get html table cell value without using javascript? because i don't wanna use javascript. if i able to get values of ID then its easy for me to delete record of selected row........help me plz!!!

    try geting the id from database record using the rs.get method in the url link to the same page ie <a href delete.jsp? id=<%=rs.getString%>
    then on the same page write the following code
    int id = request.getparameter("id");
    then write your sql query for delete

Similar Threads

  1. Replies: 9
    Last Post: 07-09-2011, 07:15 PM
  2. I want to add, search, edit, delete record of employee
    By jacob.pham in forum New To Java
    Replies: 2
    Last Post: 07-16-2010, 06:22 AM
  3. Replies: 3
    Last Post: 03-26-2009, 07:35 AM
  4. how to delete record from db using jsp
    By Manas Das in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-31-2009, 07:36 PM
  5. Replies: 0
    Last Post: 07-31-2008, 10:44 AM

Posting Permissions

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