0

Gretings This is for a project for my university. The idea is to offer a functionality where we can delete an employee using .JSP.

I tried to implement this using two .jsp's as well as a little bit of clientside Javascript

The first jsp is the actual display of the data, on this jsp I have a dynamic generated table as a result from a query. Here is the creation of the table as well as the select. :

        <%
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=HR;user=sa;password=sa;");
        Statement stmt = conn.createStatement();
        String sqlStr = "SELECT EmpID, EmpFirstName, EmpLastName, EmpJobTitle, EmpSalary FROM Employees order by EmpID";
        ResultSet rset = stmt.executeQuery(sqlStr);
        %>
        <table id ="tabEmployees" border=1>
            <tr>
                <th>ID</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Job Title</th>
                <th>Salary</th>
            </tr>
            <% while (rset.next()) { %>
            <tr>
                <td><%=rset.getInt("EmpID")%></td>
                <td><%=rset.getString("EmpFirstName")%></td>
                <td><%=rset.getString("EmpLastName")%></td>
                <td><%=rset.getString("EmpJobTitle")%></td>
                <td><%=rset.getDouble("EmpSalary")%></td>
                <td><input type="submit" value="Delete"  onclick="deleteRow(this)"> </td>
            </tr>                
        <% } %>
        </table>

As you can observe I have a delete button on each row which its purpose is to delete that row (obviously) . This delete button has behind a javascript function (deleteRow). The code of javascript is on a .javascript file. Here it is :

 function deleteRow(r)
     {
        var confirmation = confirm("Delete the row ?");
        if (confirmation == true) 
        {    
            var i = r.parentNode.parentNode.rowIndex; 
            var tabEmployee = document.getElementById("tabEmployees");
            var cellID = tabEmployee.rows[i].cells[0];
            var empID = cellID.firstChild.data;
            window.alert("1)The Emp id is = "+ empID);
            var hidElement = document.getElementById("hidInput");
            hidElement.value = empID;

            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() 
            {
                if (xhttp.readyState == 4 && xhttp.status == 200) 
                {
                    document.getElementById("showDeleteResult").innerHTML = xhttp.responseText;
                }
            };
            window.alert("2)The Emp id is = "+ empID);
            xhttp.open("get", "deleteEmployees.jsp", true);
            xhttp.send();
            document.getElementById("tabEmployees").deleteRow(i);

        } 
    }

And finally I have the actual deletion of data on another jsp named deleteEmployees.jsp which has the following code :

   <body>
        <%@ page import = "java.sql.*" %>

        <%Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        String empID = request.getParameter("empID");
        SQLException ex1=null;
        response.setContentType("text/html");
        out.println("Employee ID " + empID);
        try 
            {
            Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=HR;user=sa;password=sa;");
            Statement stmt = conn.createStatement();
            String delEmployee = "delete from dbo.Employees "
                                + "where empID = " + empID;
            stmt.executeUpdate (delEmployee);
            stmt.close();
            conn.close();
            }
        catch(SQLException ex)
            {
                ex1=ex;
            }
        if (ex1==null)
            {
                out.println("Employee Deleted");
            }
            else
            {
               out.println("Problem during the deletion of the employee ");  
            }

        %>
    </body>

While the EmpID while on the first page has a legitimate value when it reaches the deleteEmployees.jsp is null. How this empID can retain its value ?

Any constructive answer is welcomed

Thank you

1 Answer 1

0

You need to pass the empID to the other jsp as a request parameter:

xhttp.open("get", "deleteEmployees.jsp?empID=" + empID, true);
1
  • If it answered your question can you mark it as the selected answer please
    – jtsnr
    Dec 8, 2015 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.