DELETING RECORDS FROM DATABASE
DELETING RECORDS FROM DATABASE
<html>
<body>
<form action="Delete.jsp">
Enter Employee Number to Delete: <input type="text" name="txtEno"><br><br>
<input type="reset"> <input type="submit" value="Delete">
</form>
</body>
</html>
<%@ page contentType="text/html" import="java.sql.*" %>
<html>
<body>
<h1>Employee Record Deletion Page</h1>
<%
String eno = request.getParameter("txtEno");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/nehal", "root", "system123");
PreparedStatement stmt = con.prepareStatement(
"SELECT * FROM employee WHERE Employee_No=?");
stmt.setString(1, eno);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
PreparedStatement deleteStmt = con.prepareStatement(
"DELETE FROM employee WHERE Employee_No=?");
deleteStmt.setString(1, eno);
int rowsDeleted = deleteStmt.executeUpdate();
if (rowsDeleted > 0) {
out.println("<h1>Employee with Employee Number " + eno + " deleted successfully!</h1>");
}
} else {
out.println("<h1>Employee Record does not exist!</h1>");
}
con.close();
} catch (Exception e) {
out.println(e);
}
%>
</body>
</html>
Comments
Post a Comment