Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Ejb EJB Insert data

Related Tutorials


 
 

Share on Google+Share on Google+

EJB Insert data

Advertisement
The program given below describes you the way to insert data into the database using EJB.

EJB Insert data

     

The program given below describes you the way  to insert data into the database using EJB. The steps involved in inserting data 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(); Integer getInsert();
:-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();

 Integer getInsert();
}

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 .

@SuppressWarnings:-SuppressWarnings are the warning  which is to fix the cause of the warning.

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 int insert;

  @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 Integer getInsert() {
  Connection con = null;
  String url = "jdbc:mysql://192.168.10.75:3306/";
  String dbName = "komal";
  String driver = "com.mysql.jdbc.Driver";
  int val = 0;
  String userName = "root";
  String password = "root";
  try {
  Class.forName(driver).newInstance();
  con = DriverManager.getConnection
   (
url + dbName, userName, password);
  Statement st = con.createStatement();
  val = st.executeUpdate("INSERT INTO employees VALUES
    ('ABC','CDE','Rohini','indian')"
);
  st.close();
  catch (Exception e) {
  System.out.println(e);
  }
  return val;
  }
}

3)Create a client application  named Main.java

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 .

Main.java

package secure;

import bean.AccountStatusRemote;
import javax.ejb.EJB;
import java.util.*;

public class Main {

  @EJB
  private static AccountStatusRemote accountStatusBean;

  public static void main(String[] args) {
  System.out.println(accountStatusBean.getStatus());
  System.out.println(accountStatusBean.getAddress());
  System.err.println("=====================================");
  System.err.println("Data inserted successfully");
  System.err.println(accountStatusBean.getInsert()
"  rows affected")
 
}
}

Output of the program

Name of the company is: Roseindia.net
Address of the company is: sec-3,D-16/116,Rohini
=====================================
Data inserted successfully
1 rows affected

Download Source code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 12, 2009

Related Tutorials

Discuss: EJB Insert data  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
sidhesh
December 30, 2012
accessing db using session beans

sir i have implemented the same type of application in netbeans 7.2 but it gives error "no suitable driver found" even though i have explicitly loaded the driver.. what should i do to make it run ....
mamuka
November 24, 2012
downoad link not working

downoad link not working
DMCA.com