Kunal Java Unit-4&5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

INDEX:

1.Explain applet lifecycle method.

2.What is JDBC? Explain its types.

3.What is RMI? Write different steps to create RMI

application.

4.Explain creating and running executable JAR files.


1. Explain applet lifecycle method.
ANS- The lifecycle of a Java applet is managed through specific methods that are
called at different stages of the applet's execution. These methods define how an
applet is initialized, started, stopped, and destroyed.

init(): Called once when the applet is first loaded. Used for initialization.
public void init() {
// Initialization code
setBackground(Color.white);
}
start(): Called each time the applet becomes active (e.g., when the user revisits
the web page).
public void start() {
// Code to start or resume execution
thread = new Thread(this);
thread.start();
}
paint(Graphics g): Called whenever the applet needs to be redrawn. This is
where the applet’s UI is defined.
public void paint(Graphics g) {
// Drawing code
g.drawString("Hello, Applet!", 50, 25);
}

stop(): Called when the applet is no longer active (e.g., when the user leaves the
web page).
public void stop() {
// Code to suspend execution
thread = null;
}
destroy(): Called once when the applet is being unloaded from memory.
public void destroy() {
// Cleanup code
// No need to nullify thread since it's already stopped
}
2. What is JDBC? Explain its types.
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:

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.

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.
3) Network Protocol driver
The Network Protocol driver uses middleware (application server) that converts
JDBC calls directly or indirectly into the vendor-specific database protocol. It is fully
written in java.

Advantage:
o No client side library is required because of application server that can
perform many tasks like auditing, load balancing, logging etc.
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. Th
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.
3. What is RMI? Write different steps to create RMI application.
ANS- Remote Method Invocation (RMI) is a Java API that enables an object running
in one Java Virtual Machine (JVM) to invoke methods on an object running in
another JVM. This allows for distributed computing, where different parts of an
application can run on different machines.
Steps to Create an RMI Application
Create a Remote Interface:
import java.rmi.*;

public interface Hello extends Remote {


String sayHello() throws RemoteException;
}
Implement the Remote Interface:
import java.rmi.*;
import java.rmi.server.*;

public class HelloImpl extends UnicastRemoteObject implements Hello {


public HelloImpl() throws RemoteException {
super();
}
public String sayHello() throws RemoteException {
return "Hello, world!";
}
}
Create the Server:
import java.rmi.*;
import java.rmi.registry.*;

public class Server {


public static void main(String[] args) {
try {
HelloImpl obj = new HelloImpl();
Registry registry = LocateRegistry.createRegistry(1099);
registry.bind("Hello", obj);
System.out.println("Server ready");

} catch (Exception e) {
System.out.println("Server exception: " + e);
}
}
}
Create the Client:
import java.rmi.*;
import java.rmi.registry.*;

public class Client {


public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost", 1099);

Hello stub = (Hello) registry.lookup("Hello");


String response = stub.sayHello();
System.out.println("Server says: " + response);
} catch (Exception e) {
System.out.println("Client exception: " + e);
}
}
}
4. Explain creating and running executable JAR files.
ANS- A JAR (Java Archive) file is a package format for Java libraries, programs, and
other resources. An executable JAR is a special type of JAR that can be run directly
without explicitly calling the Java command.
Steps to Create an Executable JAR
1. Create a Java project with a main class:
o This class will be the entry point for your application.
o Ensure it has a public static void main(String[] args) method.
2. Compile your Java code:
o Use the javac compiler to compile your Java source files into .class files.
3. Create a manifest file:
o This file specifies the main class of your application.
o Create a text file named manifest.mf with the following content:
o Manifest-Version: 1.0
o Main-Class: your.package.MainClass
Replace your.package.MainClass with the fully qualified name of your main class.
4. Create the JAR file:
o Use the jar tool to create the JAR file:
jar cfm myapp.jar manifest.mf *.class

Running the Executable JAR


java -jar myapp.jar

You might also like