The method created in the following steps is used to delete employee records by ID:
DataHandler.java
in the Java Source Editor.public String deleteEmployeeById(int id) throws SQLException { }
getDBConnection();
Statement
object, define a ResultSet
type as before, and formulate the SQL statement. Add a trace message to assist with debugging.
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); sqlString = "DELETE FROM Employees WHERE employee_id = " + id; System.out.println("\nExecuting: " + sqlString);
stmt.execute(sqlString);
Success
.
return "success";
The following code example shows the code for the deleteEmployeeById()
method.
Method for Deleting an Employee Record
public String deleteEmployeeById(int id) throws SQLException { getDBConnection(); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); sqlString = "DELETE FROM Employees WHERE employee_id = " + id; System.out.println("\nExecuting: " + sqlString); stmt.execute(sqlString); return "success"; }