Unit 4 Complete Notes
Unit 4 Complete Notes
In simple terms, JavaBeans are classes which encapsulate several objects into a single object. It
helps in accessing these object from multiple places. JavaBeans contains several elements like
Constructors, Getter/Setter Methods and much more.
Beans should implement java.io.serializable, as it allows to save, store and restore the
state of a JavaBean you are working on
Now that you are familiar with basics, let’s learn in detail about the properties of JavaBeans.
JavaBean Properties
A JavaBean property can be accessed by the user of the object. The feature can be of any Java
data type, containing the classes that you define. It may be of the following mode: read, write,
read-only, or write-only. JavaBean features are accessed through two methods:
1. getEmployeeName ()
For example, if the employee name is firstName, the method name would be getFirstName() to
read that employee name. This method is known as an accessor. Properties of getter methods are
as follows:
Your Bean will be named SimpleBean. Here are the steps to create it and view it in the BeanBox:
1. Write the SimpleBean code. Put it in a file named SimpleBean.java, in the directory
of your choice. Here's the code:
import java.awt.*;
import java.io.Serializable;
javac SimpleBean.java
This produces the class file SimpleBean.class
3.Create a manifest file. Use your favorite text editor to create a file, we'll call
it manifest.tmp, that contains the following text:
Name: SimpleBean.class
Java-Bean: True
4.Create the JAR file. The JAR file will contain the manifest and the SimpleBean class
file:
5.Load the JAR file into the ToolBox. Select the File|LoadJar... menu item. This will bring
up a file browser. Navigate to the SimpleBean.jar location and select it. SimpleBean will
appear at the bottom of the ToolBox. (Note that when the BeanBox is started, all Beans in
JAR files in the beans/jars directory are automatically loaded into the ToolBox).
6.Drop a SimpleBean instance into the BeanBox. Click on the word SimpleBean in the
ToolBox. The cursor will change to a crosshair. Move the cursor to a spot within the BeanBox
and click. SimpleBean will appear as a painted rectangle with a hatched border. This border
means that SimpleBean is selected. The SimpleBean properties will appear in the Properties
sheet.
Advantages of JavaBeans
The following list enumerates some of the benefits of JavaBeans:
Portable
JavaBeans components are built purely in Java, hence are fully portable to any platform that
supports the Java Run-Time Environment. All platform specifics, as well as support for
JavaBeans, are implemented by the Java Virtual Machine.
Compact and Easy
JavaBeans components are simple to create and easy to use. This is an important focus sector of
the JavaBeans architecture. It doesn’t take much effort to write a simple Bean. Also, a bean is
lightweight, so, it doesn’t have to carry around a lot of inherited baggage to support the Beans
environment.
Carries the Strengths of the Java Platform
JavaBeans is pretty compatible, there isn’t any new complicated mechanism for registering
components with the run-time system.
Disadvantages of JavaBeans
1. JavaBeans are mutable, hence lack the advantages offered by immutable objects.
Manifest File
The manifest is a special file named `MANIFEST. MF` and resides within the `META-INF`
directory of a JAR file. This file provides a multitude of capabilities that influence the
behavior, discoverability, and structure of the encapsulated JavaBeans components.
JAR files
JAR files can be used for packaging related class files, serialized Beans, and other resources.
This scheme allows multiple Beans to be packaged in a single JAR file, providing a convenient
way to share common class files and resources. Optionally, a JAR file may contain a manifest
describing its contents.
2. Message Driven Bean: Like Session Bean, it contains the business logic but it is invoked
by passing message.
3. Entity Bean: It summarizes the state that can be remained in the database. It is deprecated.
Now, it is replaced with JPA (Java Persistent API). There are two types of entity bean:
EJB Architecture
The EJB architecture has two main layers, i.e., Application Server and EJB Container, based
on which the EJB architecture exist. The graphical representation of the EJB architecture is given
below.
In the above diagram, the logical representation of how EJBs are invoked and deployed by using
RMI(Remote Method Invocation) is defined. The containers of the EJB cannot be self-deployed.
In order to deploy the containers, it requires the Application server.
Java JDBC
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the
query with the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC
drivers to connect with the database. There are four types of JDBC drivers:
o JDBC-ODBC Bridge Driver,
o Native Driver,
o Network Protocol Driver, and
o Thin Driver
.
We can use JDBC API to access tabular data stored in any relational database. By the help of
JDBC API, we can save, update, delete and fetch data from the database. It is like Open
Database Connectivity (ODBC) provided by Microsoft.
The current version of JDBC is 4.3. It is the stable release since 21st September, 2017. It is based
on the X/Open SQL Call Level Interface. The java.sql package contains classes and interfaces
for JDBC API. A list of popular interfaces of JDBC API are given below:
o Driver interface
o Connection interface
o Statement interface
o PreparedStatement interface
o CallableStatement interface
o ResultSet interface
JDBC Driver
JDBC Driver is a software component that enables java application to interact with the
database. There are 4 types of JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)
1) JDBC-ODBC bridge driver
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-
ODBC bridge driver converts JDBC method calls into the ODBC function calls. This is now
discouraged because of thin driver.
Oracle does not support the JDBC-ODBC Bridge from Java 8. Oracle recommends that you use
JDBC drivers provided by the vendor of your database instead of the JDBC-ODBC Bridge.
Advantages:
o easy to use.
o can be easily connected to any database.
Disadvantages:
o Performance degraded because JDBC method call is converted into the ODBC function
calls.
o The ODBC driver needs to be installed on the client machine.
2) Native-API driver
The Native API driver uses the client-side libraries of the database. The driver converts JDBC
method calls into native calls of the database API. It is not written entirely in java.
Advantage:
o performance upgraded than JDBC-ODBC bridge driver.
Disadvantage:
o The Native driver needs to be installed on the each client machine.
o The Vendor client library needs to be installed on client machine.
Disadvantages:
o Network support is required on client machine.
o Requires database-specific coding to be done in the middle tier.
o Maintenance of Network Protocol driver becomes costly because it requires database-
specific coding to be done in the middle tier.
4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is
why it is known as thin driver. It is fully written in Java language.
Advantage:
o Better performance than all other drivers.
o No software is required at client side or server side.
Disadvantage:
o Drivers depend on the Database.
JDBC API is available in two packages java.sql, core API and javax.sql JDBC optional
packages. Following are the important classes and interfaces of JDBC.
To connect Java application with the MySQL database, we need to follow 5 following steps.
In this example we are using MySql as the database. So we need to know following
informations for the mysql database:
1. Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
2. Connection URL: The connection URL for the mysql database
is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the database,
localhost is the server name on which mysql is running, we may also use IP address,
3306 is the port number and sonoo is the database name. We may use any database, in
such case, we need to replace the sonoo with our database name.
3. Username: The default username for the mysql database is root.
4. Password: It is the password given by the user at the time of installing the mysql
database. In this example, we are going to use root as the password.
Let's first create a table in the mysql database, but before creating table, we need to create
database first.
In this example, sonoo is the database name, root is the username and password both.
1. import java.sql.*;
2. class MysqlCon{
3. public static void main(String args[]){
4. try{
5. Class.forName("com.mysql.jdbc.Driver");
6. Connection con=DriverManager.getConnection(
7. "jdbc:mysql://localhost:3306/sonoo","root","root");
8. //here sonoo is database name, root is username and password
9. Statement stmt=con.createStatement();
10. ResultSet rs=stmt.executeQuery("select * from emp");
11. while(rs.next())
12. System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
13. con.close();
14. }catch(Exception e){ System.out.println(e);}
15. }
16. }
The above example will fetch all the records of emp table.
To connect java application with the mysql database, mysqlconnector.jar file is required to be
loaded.
2) Set classpath:
There are two ways to set the classpath:
o temporary
o permanent
1. C:>set classpath=c:\folder\mysql-connector-java-5.0.8-bin.jar;.;
Go to environment variable then click on new tab. In variable name write classpath and in
variable value paste the path to the mysqlconnector.jar file by appending mysqlconnector.jar;.;
as C:\folder\mysql-connector-java-5.0.8-bin.jar;.;
There are 5 steps to connect any java application with the database using
JDBC
Class.forName("oracle.jdbc.driver.OracleDriver");
Syntax of createStatement()
methodpublic Statement createStatement()throws SQLException
Example to create the statement object
Statement stmt=con.createStatement();
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
Create a Table
Before establishing connection, let's first create a table in oracle database. Following is the
SQL query to create a table.
In this example, we are connecting to an Oracle database and getting data from emp table.
Here, system and oracle are the username and password of the Oracle database.
import java.sql.*;
class OracleCon{
public static void main(String args[]){
try{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
}
}