0% found this document useful (0 votes)
58 views13 pages

Experiment:-1 Objective:-Introduction To Component. What Is Components?

The document discusses Java Database Connectivity (JDBC) which is an API used to connect Java applications to databases. It allows Java programs to execute SQL statements and retrieve results from a database. The key aspects covered are: - JDBC provides interfaces for connecting to a database, executing queries, and processing results. - The JDBC architecture includes the JDBC API layer and JDBC driver layer. Driver managers ensure the correct database-specific driver is used. - There are 4 types of JDBC drivers: JDBC-ODBC bridge, native API, network-based pure Java, and 100% pure Java drivers. - JDBC is commonly used

Uploaded by

Pradyumna Mittal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views13 pages

Experiment:-1 Objective:-Introduction To Component. What Is Components?

The document discusses Java Database Connectivity (JDBC) which is an API used to connect Java applications to databases. It allows Java programs to execute SQL statements and retrieve results from a database. The key aspects covered are: - JDBC provides interfaces for connecting to a database, executing queries, and processing results. - The JDBC architecture includes the JDBC API layer and JDBC driver layer. Driver managers ensure the correct database-specific driver is used. - There are 4 types of JDBC drivers: JDBC-ODBC bridge, native API, network-based pure Java, and 100% pure Java drivers. - JDBC is commonly used

Uploaded by

Pradyumna Mittal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Pradyumna Mittal 16010BTIT00609

Experiment:-1
Objective:-Introduction to Component.

What is Components?
A component is a modular, portable, replaceable, and reusable set of well-defined
functionality that encapsulates its implementation and exporting it as a higher-level interface.

A component is a software object, intended to interact with other components, encapsulating


certain functionality or a set of functionalities. It has an obviously defined interface and
conforms to a recommended behaviour common to all components within an architecture.

A software component can be defined as a unit of composition with a contractually specified


interface and explicit context dependencies only. That is, a software component can be
deployed independently and is subject to composition by third parties.

What is Component Technology?

Component technology refers to the use of software components for software development.
Software components usually confirm to a component model, and they are often hosted and
managed by a component framework, which provides a controlled environment where
components can be composed together to form larger applications or systems.

Component is a functionality independent path of system. It performs some function and they
required some input or produce output.

Need of Components:-The primary objective of component-based architecture is to


ensure component reusability. A component encapsulates functionality and behaviours of a
software element into a reusable and self-deployable binary unit. There are many standard
component frameworks such as COM/DCOM, JavaBean, EJB, CORBA, .NET, web services,
and grid services. These technologies are widely used in local desktop GUI application design
such as graphic JavaBean components, MS ActiveX components, and COM components
which can be reused by simply drag and drop operation.

Reusability − Components are usually designed to be reused in different situations in


different applications. However, some components may be designed for a specific task.

Replaceable − Components may be freely substituted with other similar components.

Not context specific − Components are designed to operate in different environments


and contexts.

Extensible − A component can be extended from existing components to provide new


behaviour.
Pradyumna Mittal 16010BTIT00609

Encapsulated − A component depicts the interfaces, which allow the caller to use its
functionality, and do not expose details of the internal processes or any internal
variables or state.

Independent − Components are designed to have minimal dependencies on other


components.

Features of Components:-

Information Hiding:-The most important characteristics is that components


completely hide their implementation where every module hides an important design
decision as well as defined interface which doesn’t change when the design or
implementation changes.

Context Independence:-Components are easily transforms into different


application context hence component should be self-contain software element.

Interoperability:-This facilitates a component to communicate with


components program in another programming language or located on a different post.

Reusability:- Components are usually designed to be reused in different situations


in different applications. However, some components may be designed for a specific
task.

Flexibility:-The extensibility of software system is improved because one


component and can be flexibly substituted by another component that satisfies the
user requirement.

Benefits of Components:-
A component is more generalized and application-independent.
Components can be reused in a variety of contexts.
Individual components can be custom-made to suit specific requirements, whereas
existing ones can be customized to meet those requirements.
Several components can be assembled to form a functioning system.
Upgrades of individual components end the problem of massive upgrades as done in
monolithic systems.
Components can live anywhere on a network - in computers best suited for running
them - based on the functions the components provide.
Using distributed computing system standards such as CORBA, Java RMI and EJB,
and Microsoft's COM/DCOM, components can be distributed among the computers
in an enterprise network.
Pradyumna Mittal 16010BTIT00609

Experiment:-2

Objective:-Java Database Connectivity

What is jdbc?
Java Database Connectivity(JDBC) is an Application Programming Interface(API) used to
connect Java application with Database. JDBC is used to interact with various type of
Database such as Oracle, MS Access, My SQL and SQL Server. JDBC can also be defined as
the platform-independent interface between a relational database and Java programming.
JDBC is alsoused for database-independent connectivity between the Java programming
language and a wide range of databases.

The JDBC library includes APIs for each of the tasks mentioned below that are commonly
associated with database usage.

Making a connection to a database.


Creating SQL or MySQL statements.
Executing SQL or MySQL queries in the database.

Viewing & Modifying the resulting records.

Fundamentally, JDBC is a specification that provides a complete set of interfaces that allows
for portable access to an underlying database. Java can be used to write different types of
executables, such as −

Java Applications
Java Applets
Java Servlets
Java ServerPages (JSPs)
Enterprise JavaBeans (EJBs).

All of these different executables are able to use a JDBC driver to access a database, and take
advantage of the stored data.

JDBC Architecture:-The JDBC API supports both two-tier and three-tier processing
models for database access but in general, JDBC Architecture consists of two layers −

JDBC API: This provides the application-to-JDBC Manager connection.


JDBC Driver API: This supports the JDBC Manager-to-Driver Connection.

The JDBC API uses a driver manager and database-specific drivers to provide transparent
connectivity to heterogeneous databases.
Pradyumna Mittal 16010BTIT00609

The JDBC driver manager ensures that the correct driver is used to access each data source.
The driver manager is capable of supporting multiple concurrent drivers connected to
multiple heterogeneous databases.

Following is the architectural diagram, which shows the location of the driver manager with
respect to the JDBC drivers and the Java application –

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:- 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.
Pradyumna Mittal 16010BTIT00609

2. JDBC API Driver:-In a Type 2 driver, JDBC API calls are converted into
native C/C++ API calls, which are unique to the database. These drivers are typically
provided by the database vendors and used in the same manner as the JDBC-ODBC
Bridge. The vendor-specific driver must be installed on each client machine.
If we change the Database, we have to change the native API, as it is specific to a
database and they are mostly obsolete now, but you may realize some speed increase
with a Type 2 driver, because it eliminates ODBC's overhead.

The Oracle Call Interface (OCI) driver is an example of a Type 2 driver.

3. JDBC-Net pure Java:-In a Type 3 driver, a three-tier approach is used to


access databases. The JDBC clients use standard network sockets to communicate
with a middleware application server. The socket information is then translated by
the middleware application server into the call format required by the DBMS, and
forwarded to the database server. This kind of driver is extremely flexible, since it
requires no code installed on the client and a single driver can actually provide access
to multiple databases. You can think of the application server as a JDBC "proxy,"
meaning that it makes calls for the client application. As a result, you need some
knowledge of the application server's configuration in order to effectively use this
driver type.

Your application server might use a Type 1, 2, or 4 driver to communicate with the
database, understanding the nuances will prove helpful.
Pradyumna Mittal 16010BTIT00609

4. 100% Pure Java:-In a Type 4 driver, a pure Java-based driver communicates


directly with the vendor's database through socket connection. This is the highest
performance driver available for the database and is usually provided by the vendor
itself.

This kind of driver is extremely flexible, you don't need to install special software on
the client or server. Further, these drivers can be downloaded dynamically.

Uses of JDBC:-
If you are accessing one type of database, such as Oracle, Sybase, or IBM,
the preferred driver type is 4.
If your Java application is accessing multiple types of databases at the same time,
type 3 is the preferred driver.
Type 2 drivers are useful in situations, where a type 3 or type 4 driver is not
available yet for your database.
The type 1 driver is not considered a deployment-level driver, and is typically
used for development and testing purposes only.

Java Database Connectivity with MySQL:-


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/db1 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 db1 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.
Pradyumna Mittal 16010BTIT00609

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.

5. Program for connectivity.

PROGRAM FOR JDBC:-


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Check
{
public static void main(String[] args)
{
Connection con;
ResultSet rs;
Statement st;
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","root");
st=con.createStatement();
rs=st.executeQuery("select * from emp");
System.out.println("id\t\tname\t\tsalary\t\tcity ");
while(rs.next())
{

System.out.println(rs.getString(1)+"\t\t"+rs.getString(2)+"\t\t"+rs.getString(3)
+"\t\t"+rs.getString(4));
}
}
catch(Exception e)
{
Pradyumna Mittal 16010BTIT00609

System.out.println(e);
}
}
}

Output:-
Pradyumna Mittal 16010BTIT00609

Experiment:-3
Objective:- Introduction to RMI and its implementation.
Introduction:-
The RMI (Remote Method Invocation) is an API that provides a mechanism to create
distributed application in java. The RMI allows an object to invoke methods on an object
running in another JVM.
The RMI provides remote communication between the applications using two objects stub
and skeleton.

Goals of RMI:-
Following are the goals of RMI −

• To minimize the complexity of the application.


• To preserve type safety.
• Distributed garbage collection.
• Minimize the difference between working with local and remote objects.

RMI Architecture:-
In an RMI application, we write two programs, a server program (resides on the server) and
a client program (resides on the client).
• Inside the server program, a remote object is created and reference of that object is
made available for the client (using the registry).

• The client program requests the remote objects on the server and tries to invoke its
methods.
The following diagram shows the architecture of an RMI application.
Pradyumna Mittal 16010BTIT00609

Stub:-
The stub is an object, acts as a gateway for the client side. All the outgoing requests are
routed through it. It resides at the client side and represents the remote object. When the
caller invokes method on the stub object, it does the following tasks:
1. It initiates a connection with remote Virtual Machine (JVM).
2. It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM).
3. It waits for the result.
4. It reads (unmarshalls) the return value or exception.
5. It finally, returns the value to the caller.

Skeleton:-
The skeleton is an object, acts as a gateway for the server side object. All the incoming
requests are routed through it. When the skeleton receives the incoming request, it does the
following tasks:
1. It reads the parameter for the remote method.
2. It invokes the method on the actual remote object.
3. It writes and transmits (marshals) the result to the caller.

Steps of implementations:-
1. create the remote interface:- For creating the remote interface, extend the
Remote interface and declare the RemoteException with all the methods of the remote
interface. Here, we are creating a remote interface that extends the Remote interface.
There is only one method named add() and it declares RemoteException.
import java.rmi.*;
public interface Adder extends Remote
{
public int add(int x,int y)throws RemoteException;
}

2. Provide the implementation of the remote interface:- Now provide the


implementation of the remote interface. For providing the implementation of the
Remote interface, we need to

• Either extend the UnicastRemoteObject class,


• or use the exportObject() method of the UnicastRemoteObject class

In case, you extend the UnicastRemoteObject class, you must define a constructor that
declares RemoteException.
import java.rmi.*;
import java.rmi.server.*;
Pradyumna Mittal 16010BTIT00609

public class AdderRemote extends UnicastRemoteObject implements


Adder {
AdderRemote()throws RemoteException
{
super();
}
public int add(int x,int y)
{
return x+y;
}
}

3. create the stub and skeleton objects using the rmic tool:-Next step is
to create stub and skeleton objects using the rmi compiler. The rmic tool invokes the
RMI compiler and creates stub and skeleton objects.
mic AdderRemote

4. Start the registry service by the rmiregistry tool:-Now start the registry
service by using the rmiregistry tool. If you don't specify the port number, it uses a
default port number. In this example, we are using the port number 5000.
rmiregistry 5000

Output:-

5. Create and run Server Application:- In this example, we are binding the
remote object by the name sonoo.
Pradyumna Mittal 16010BTIT00609

import java.rmi.*;
import java.rmi.registry.*;
public class MyServer
{
public static void main(String args[])
{
try
{

Adder stub=new AdderRemote();


Naming.rebind("rmi://localhost:5000/sonoo",stub);
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output:-

6. Create and run Client Application:- At the client we are getting the stub
object by the lookup() method of the Naming class and invoking the method on this
object. In this example, we are running the server and client applications, in the same
machine so we are using localhost. If you want to access the remote object from
another machine, change the localhost to the host name (or IP address) where the
remote object is located.
import java.rmi.*;
Pradyumna Mittal 16010BTIT00609

public class MyClient


{
public static void main(String args[])
{
try
{
Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");
System.out.println(stub.add(34,4));
}
catch(Exception e)
{
}
}
}

Output:-

You might also like