Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Accessing Database using EJB 
 

This is a simple EJB Application that access the database. Just go through the EJB example given below to find out the steps involved in accessing Database.

 

Accessing Database using EJB

                         

This is a simple EJB Application that access the database. Just go through the EJB example given below to find out the steps involved in accessing Database.

Creating a simple Database driven application in EJB

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(); List getData();:-
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();

    List getData();
}

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 .This is to be declared in the the sun-application.xml file.

<security-role-mapping>
<role-name>USERS</role-name>
<group-name>bank_users</group-name>
</security-role-mapping>

@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 List list = new ArrayList();

    @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 List getData() {
        Connection con = null;
        String url = "jdbc:mysql://192.168.10.75:3306/";
        String dbName = "komal";
        String driver = "com.mysql.jdbc.Driver";

        String userName = "root";
        String password = "root";
        try {
            Class.forName(driver).newInstance();
            con = DriverManager.getConnection(url + dbName, userName, password);
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select* from employees");

            HashMap row;
            while (rs.next()) {
                row = new HashMap();
                row.put("First_Name", rs.getString(1));
                row.put("Last_Name", rs.getString(2));
                row.put("Address",rs.getString(3));
                row.put("Nationality",rs.getString(4));
                list.add(row);
            }

            st.close();
        catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}

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());

        List list = accountStatusBean.getData();
        HashMap row;
        System.out.println("First_Name" "\t" "Last_Name" "\t" "Address"
                                                     
"\t\t" "Nationality");
        System.out.println("=============================================;  ");
        for (int i = 0; i < list.size(); i++) {
            row = (HashMaplist.get(i);
            String a = (Stringrow.get("First_Name");
            String s = (Stringrow.get("Last_Name");
            String b = (Stringrow.get("Address");
            String d = (Stringrow.get("Nationality");
            System.out.println(a + "\t\t" + s + "\t\t" + b + "\t\t" + d);
        }
        System.out.println("=============================================");
      
}
}

Output of the program

Name of the company is: Roseindia.net
Address of the company is: sec-3,D-16/116,Rohini
First_Name  Last_Name  Address     Nationality
==============================================================
Girish      Tewari     Rohini       Indian
Komal       Singh      Rohini       Indian
Sandeep     kumar      Rohini       Indian
Amit        Singh      Rohini       Indian
Girish      Tewari     Rohini       Indian
Darshan     Tewari     Rohini       Indian
==============================================================

Download Source code

                         
» View all related tutorials
Related Tags: c database data application io find ejb this app simple tab example to base exam ssi e eps li im

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.