Unit 4
Unit 4
JAVA BEANS
Syllabus: -
A bean obtains all the benefits of Java’s “write once, run-anywhere” paradigm.
A bean's properties, events, and methods are exposed to an application builder tool and can be controlled.
A bean may be designed to operate correctly in different locales, which makes it useful in global markets.
Auxiliary software can be provided to help a person configure a bean.
The configuration settings of a bean can be saved in persistent storage and restored later.
A bean may register to receive events from other objects and generate events sent to other objects.
Features of a Bean: -
Support for introspection: allows a builder tool to analyze how a bean works.
Support for customization: allows a user to alter the appearance and behavior of a bean.
Support for events: allows beans to fire events and informs builder tools about both the events they can fire
and the events they can handle.
Support for properties allowing beans to be manipulated programatically, as well as to support the
customization mentioned above.
Support for persistence: allow beans customized in an application builder to save and restore their state.
Creating a JavaBean
Steps: -
Example: -
import java.awt.*;
import java.io.Serializable;
public class SimpleBean extends Canvas implements Serializable {
public SimpleBean() {
setSize(50,30);
setBackground(Color.blue);
}
}
What is BDK?
The JavaBeans Development Kit (BDK) is a set of technologies (BDK 1.1, J2SE SDK, and Java Plug-in
software) to support the development of JavaBeans components and to act as a standard reference base for both
component developers and tool vendors. The BDK provides a reference Bean container, the "BeanBox," and a
variety of reusable example source code for use by both tool and beans developers.
JAR stands for the Java Archive. This file format is used to distribute a set of java classes. This file helps you to
reduce the file size
A developer must provide a manifest file to indicate which of the Components in the JAR file are Java Beans. A
Manifest file may refer to several class files. If a class file is a Java Bean, the line “java-Bean: True” must
immediately follow its entry.
Use notepad to create a manifest file that contains the following text: -
Name: SimpleBean.class
Java_bean:True
BDK Introspection
Introspection is the process of analyzing a bean to determine its capabilities. It is an important feature of Java
Bean API because it allows an application builder tool to present information about a component to a software
designer. Without introspection, the Java beans technology could not operate. One way to expose Bean's
properties, events, and methods to the application builder tool is by using simple naming conventions.
A Java bean property is a named attribute that can affect its behavior or appearance.
It includes color, label, font, font size, and display size.
A JavaBean property may be read, write, read only, or write only.
These are accessed through two methods in the JavaBean's implementation class: –
getPropertyName(
For example, if property name is firstName, your method
)
1 name would be getFirstName() to read that property.
This method is called accessor.
Types of Properties: -
Simple - Property with a single value whose changes are independent of changes in any other property.
Indexed - Property that supports a range of values instead of a single value.
Bound - Property for which a change to the property results in a notification sent to some other bean.
Constrained - Property for which a change to the property results in validation by another bean. The other
bean may reject the change if it is not appropriate.
Types of Beans
Interface Description
Class Description
collection of Java classes and an XML file bundled into a single unit.
Specifies certain rules and specific callback methods (with J2EE Environment) to be followed by Java
classes.
standard server-side component model for CTM.
The EJB architecture is a component architecture for developing and deploying component-based distributed
business applications.
EJB component: A reusable, application-independent piece of code. It is a Java class written to implement
business logic.
EJB Container: Represents an environment in which the component operates. An EJB component lives
inside an EJB container.
EJBObject: Method by which the client programs execute remote EJB components.
EJBServer: Application server that is EJB compliant.
EJB repository yields system-level services to the enterprise. Rather than the bean developer, the EJB
repository is responsible for system-level services such as transaction
Management and security authorization.
Establishes roles for application development.
Provides distributed transaction support.
Are portable elements, the application assembler can build new applications from the beans that already
exist.
Provides vendor-specific enhancements.
Types of EJB: -
1. Entity Bean
− type of EJB that represents persistent data manipulated in a database.
− Does not contain business logic
For example, a business application might use a database to store business entity objects such as
accounts, customers, orders, etc.
− Entity beans are persistent, allow shared access, and have primary keys.
− Bean managed Persistence
− Container Managed Persistence
− Can be shared among multiple clients
− Each entity Bean has a unique object identifier called as Primary key
− A session bean is a client of Entity bean.
− Entity beans work within a transaction. EJB container provides transaction management
− Its interface is implemented by every entity enterprise bean class.
− It is of two types: -
i) Bean-Managed Persistence (BMP): Enables you to manage database queries and connectivity
issues. For this, you need to write code to establish connectivity and query database tables in the
bean class file.
ii) Container-Managed Persistence (CMP): Enables EJB container to manage database queries
and connectivity issues.
1. ejbCreate()
2. ejbPostCreate()
Entity Bean life cycle: -
2. Session Bean
− Encapsulates business logic
− can be invoked by local, remote, and web service clients.
− when a client or user requests a session bean, a separate instance of the bean is created and provided to
the caller.
− A client does not directly access an instance of the session bean. Instead, it accesses a session object
through the session beans' Home (Implement the Home interface) and Component (Implement the
Component interface) interfaces.
− A session bean is not persistent, i.e., data is stored temporarily, and data will be destroyed if some crash
occurs.
− Life is restricted by the life of the client session
− further classified into two types: -
i) Stateful:
− Represents that session EJB created to maintain a specific client's state between subsequent calls for
some maximum time.
− It is specific to a client.
− Maintains the state or some information of a client across multiple requests.
− Are mapped to a single client for that client's life.
ii) Stateless:
− Represents those session EJBs that have no regard for maintaining any state between subsequent calls
by the client.
− does not maintain the state of a client between multiple methods calls.
− Can be accessed by one client at a time.
− In case of concurrent access, EJB container routes each request to a different instance.
− Because stateless session beans support multiple clients, they can offer better scalability for
applications that require a large number of clients.
Life Cycle of a Stateless Session Bean: - Life Cycle of a Stateful Session Bean: -
3. Message-driven
− enterprise bean that allows J2EE (Java to Enterprise Edition) applications to process messages
asynchronously.
− Invoked by EJB container when it receives a message from the queue.
− Used for communication purposes only.
− acts as a JMS (Java Message Service) message listener, which is similar to an event listener
except that it receives messages instead of events.
− The messages may be sent by any J2EE component, such as an application client, another
enterprise bean, a web component, or a JMS application or system that does not use J2EE
technology.
− Message-driven beans currently process only JMS messages, but in the future, they may be
used to process other kinds of messages.
Before JDBC, the ODBC API was used to connect to and execute queries to the database. However, ODBC
drivers were platform-dependent and unsecured, so JDBC was used.
Functions of JDBC: -
JDBC Basics
JDBC program starts out by loading a JDBC driver class (in this case, Sun's JDBC-ODBC bridge).
It uses the driver to create a database connection, represented by a Connection object.
With the database connection, it creates a Statement object to represent a SQL statement.
Executing an SQL statement produces a ResultSet containing a query's results.
The program displays the results and then cleans up the resources it has used.
If an error occurs, a SQLException is thrown, so our program traps that exception and displays some of the
information it encapsulates.
Before using a driver, it must be registered with the JDBC DriverManager. This is typically done by loading
the driver class using the Class.forName( ) method: -
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName("com.oracle.jdbc.OracleDriver");
}
catch (ClassNotFoundException e) {
/* Handle Exception */
}
JDBC drivers are available for most database platforms from several vendors.
JDBC Drivers: -
Used to translate API calls into operation for specific data sources.
Enables Java applications to interact with the database.
These are client-side adapters that convert Java program requests to a protocol that DBMS can understand.
JDBC URLs
A JDBC driver uses a JDBC URL to identify and connect to a particular database.
These URLs are generally of the form: -
jdbc:driver:databasename
The Oracle JDBC-Thin driver uses a URL of the form: jdbc:oracle:thin:@site:port:database
3. JDBC-ODBC Bridge: -
Allows ODBC drivers to be used as JDBC drivers.
It was implemented as a way to get JDBC off the ground quickly and, as a result, provide access to some of the
less popular DBMS.
If JDBC drivers are not implemented for them, it connects database drivers to the database.