Deleting a Row from SQL Table Using EJB

In the given example of Enterprise Java Bean, we are going to delete a row from the SQL Table.

Deleting a Row from SQL Table Using EJB

Deleting a Row from SQL Table Using EJB

     

In the given example of Enterprise Java Bean, we are going to delete a row from the SQL Table. Find out the steps given below that describes how to delete a particular row from the database table using EJB. The steps involved in this process are as :-

1)Create an interface named AccountStatusRemote.java

AccountStatusRemote.java :-This is the Remote Interface for the Bean. Here we have used  @Remote annotation to declare the class as a Remote Interface. The use of annotation here is that through it we can create a java source file which contain the bean implementation logic.

String getStatus(); String getAddress(); String getUpdate();:-
These are the method which is to be defined in the Bean and is called in the client application.

AccountStatusRemote.java

package bean;

import javax.ejb.Remote;
import java.util.*;

@Remote
public interface AccountStatusRemote {

  String getStatus();

  String getAddress();
 
  String getUpdate();
}

2)Create a Bean named AccountStatusBean.java

AccountStatusBean.java:-This is the session bean we have created. By session bean we mean the bean which act as an agents to the client. it is generally used in controlling the business process and filling the gaps between the data of the entity beans. Here

@Stateless
is the session type.

@RolesAllowed(value = {"USERS"}):-This is also the annotation which means that only users in the security role USERS can access the  method declared in the Bean .

Main.java:-This is the client application through which we can access the methods which are defined in the bean.

@EJB:-This is the annotation that configure the EJB values for a field or a method. Normally this annotation is a Resource annotation where it is known that the resultant is an EJB interface.

private static AccountStatusRemote accountStatusBean:-By this we have created an instance of the interface AccountStatusRemote .

AccountStatusBean.java 

package bean;

import java.sql.*;
import java.util.*;
import javax.annotation.security.RolesAllowed;
import javax.ejb.Stateless;

@Stateless
public class AccountStatusBean implements AccountStatusRemote {

  private String name = "Roseindia.net";
  private String address = "sec-3,D-16/116,Rohini";
  private String Update;

  @RolesAllowed(value = {"USERS"})
  public String getStatus() {
  return "Name of the company is: " + name;
  }

  public String getAddress() {
  return "Address of the company is: " + address;
  }

  @SuppressWarnings(value = "unchecked")

  public String getUpdate() {
 Connection con = null;
  String url = "jdbc:mysql://192.168.10.75:3306/";
  String dbName = "komal";
  String driver = "com.mysql.jdbc.Driver";
  String updateString=null;
  String userName = "root";
  String password = "root";
  try {
  Class.forName(driver).newInstance();
  con = DriverManager
   .getConnection
(url + dbName, userName, password);
  Statement st = con.createStatement()
  updateString="delete from employees where
    First_Name='A'"
;
  st.executeUpdate(updateString);
  st.close();

  catch (Exception e) {
  }
  return updateString ;
  }
 }

Output of the program

Name of the company is: Roseindia.net
Address of the company is: sec-3,D-16/116,Rohini
=====================================
Data updated successfully : See sql table to verify
delete from employees where First_Name='A'

Download Source code