Wa0064.
Wa0064.
Applet Fundamentals -applet tag, applet life cycle, passing parameters to applets. Working with
graphics - Line, Rectangle, Oval, Arc, color setting. JDBC architecture- JDBC connection,
JDBC statement object, JDBC drivers.
Applet Programming
Applets are small Java programs that are primarily used in Internet computing. They can be
transported over the Internet from one computer to another and run using the Applet Viewer
or any Web browser that supports Applet can perform arithmetic operations, display graphics,
play sounds, accept user input, create animation, and play interactive games.
Local and Remote Applets
We can embed applets into Web pages in two ways
One, we can write our own applets and embed them into Web pages. Second, we can
download an applet from a remote computer system and then embed it into a Web page.
An applet developed locally and stored a local system is known as a local applet. When a
Web page is trying to find a local applet, it does not need to use the internet and therefore the
local system does not require the Internet connection.
A remote applet is that which is developed someone else and stored on remote computer
connected to the Internet. If our system is connected to the Internet, wecan download the
remote applet on to our system and run on it.
In order to locate and load remote applet,we must know the applet's address on the Web. This
address is known as Uniform Reource Locator(URL) and must be specified the applet's
HTML document as the value of the CODEBASE attribute
Eg. CODEBASE = http : // www.netserve.com/applets
Applets Differ from Applications:
Both the applets and the stand alone applications are the java programs, there are significant
differences between them. Applets are not full featured application programs. They are
usually written to accomplish a small task or a component of a task. They are designed for
use on the internet and have certain limitations and restrictions in their design:
• Applets do not use the main( ) method for initiating the execution of the code. when
itloaded, automatically call certain method of applet class to start and execute the
applet code.
• Unlike stand-alone applications, Applets cannot be run independently. They are run
from inside a web page using a special feature known as HTML tag.
• Applets cannot read from or write to the files in the local computer.
• Applets cannot communicate with other serves on the network.
• Applets cannot run any program from the local computer.
• Applets area restricted from using libraries from other languages such as C or C++.
Preparing an Applet:
Before we try to write applets, we must make sure that java is installed properly and also
ensure that either the java appletviewer or a java-enabled browser is available.
The steps involved are:
1. Building an applet code (.java file)
2. Creating an executable applet (.class file)
3. Designing a web page using HTML tags.
4. Preparing <APPLET> tag .
5. Incorporating <APPLET> tag into the web page.
6. Creating HTML file.
7. Testing the applet code.
Building Applet code:
The applet class from java.applet package provides life and behavior to the applet through its
methods such as init( ), start( ), and paint( ). Java calls the main( ) method directly to initiate
the execution of the program, when an applet is loaded , java automatically calls a series of
applet class methods for starting , running, and stopping the applet code. The applet class
therefore maintains the lifecycle of an applet. The paint( ) method of the applet class displays
the result of the applet code when it is called. The output may be text, graphics, or sound. The
paint( ) method which requires a Graphics
object as an argument is defined as follows:
public void paint (Graphics g)
Syntax:
import java.awt.*;
import java.applet.*;
….
…..
public class appletclassname extends Applet
{
……
……
public void paint (Graphics g)
{
……
……
}
……
……
}
Example:
import java.awt.*;
import java.applet.*;
public class Hellojava extends Applet
{
public void paint (Graphics g)
{
g.drawString(“Hello java”, 10, 100);
}
}
Creating an Executable Applet:
Executable applet is nothing but the .class file of the applet, which is obtained by compiling
the source code of the applet. The steps required for compiling the Hellojava applet.
• Move to the directory containing the source code and type the following commands:
javac Hellojava.java
• The compiled output file called Hellojava.class is placed in the same directory as the
source.
• If any error message is received then we must check for errors, correct them and
compile the applet again.
One of the most important features of Java is its ability to draw graphics. We can write Java
applets that draw lines, figures of different shapes, Images, and text in different fonts and
styles. We can also incorporate different colors in display.
THE GRAPHICS CLASS
Java's Graphics class includes methods for drawing many different types of shapes, from
simple line to polygons to text in a variety of fonts. The most commonly used drawing
methods in the Graphics class.
Method Description
The simplest shape we can draw with the Graphics class is a line. The drawLine () method
takese pair of coordinates, (xl, yl) and (x2, y2) as arguments and draws a line between them.
we can draw a rectangle using the drawRect() method. This method takes four be remaining
two represent the width and the height of the rectangle.
Program- Drawing a line
import java.awt.*;
import java.applet.*;
public class Line extends Applet
{
public void paint(Graphics g)
{
g.drawLine(100,10,250, 150);
g.drawLine(100,150,150,10);
}
}
<html>
<head>
</head>
<body>
<applet code = "Line.class" width = "420" height = "320"></applet>
</body>
</html>
The Graphics class does not have any method for circles or ellipses. The drawOval() method
can be used to draw a circle or an ellipse. Ovals are just like rectangles with overly rounded
corners. The drawOval() method takes four arguments: the first two represent the top-left
comer of the imaginary rectangle and the other two represent the width and height of the oval
itself
import java.awt.*;
import java.applet.*;
public class circle extends Applet
{
public void paint(Graphics g)
{
g.drawOval(20,20,200,120);
g.setColor(Color.green);
g.fillOval(70,30,100,100);
}
}
<html>
<head>
</head>
<body>
<applet code = "circle.class" width = "480" height = "360"></applet>
</body>
</html>
DRAWING ARCS
An arc is a part of an oval. The drawArc( ) designed to draw arcs takes six arguments. The
first four are the same as the arguments for drawOval() method and the last two represent the
starting angle of the arc and the number of degrees (sweep angle) around the arc.
import java.awt.*;
import java.applet.*;
public class Mouth extends Applet
{
public void paint (Graphics g)
{
g.drawArc(60, 125, 80, 40, 180, 180); // Draw an Arc Shape
g.fillArc(60, 125, 80, 40, 180, 180); // Fill an Arc Shape
}
}
<html>
<head>
</head>
<body>
<applet code = "Mouth.class" width = "480" height = "360"></applet>
</body>
</html>
JDBC ARCHITECTURE
JDBC is a phrase that is used to specify Java Database Connectivity. It is an interface with the
J Application Program known as Application Programming Interface (API) on one end and the
data set defined as a database on another end. It is used to link an application written in Java
language in any platform with database as backend. JDBC helps to connect to a database, send
queries and updates to the database, and retrieve and process the results obtained from the
database for queries.
JDBC link with java application and database can be represented as
Type-1 driver or 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. Type-
1 driver is also called Universal driver because it can be used to connect to any of the databases.
As a common driver, it is used in order to interact with different databases, the data transferred
through this driver is not so secured.The ODBC bridge driver is needed to be installed in
individual client machines.Type-1 driver is not written in java, that’s why it isnot a portable
driver. This driver software is built-in with JDK so it doesnot need to install separately. It is a
database independent driver.
Native-API driver
The Native API driver uses the client -side libraries of the database.This driver converts JDBC
method calls into native calls of the database API. In order to interact with different database,
this driver needs their local API, that’s why data transfer is much more secure as compared to
type-1 driver.
This driver needs to be installed separately in individual client machines. The Vendor client
library needs to be installed on client machine. Type-2 driver isn’t written in java, that’s why
it isn’t a portable driver. It is a database dependent driver.
Network Protocol driver
The Network Protocol driver uses middleware (application server) that converts JDBC calls
directly or indirectly into the vendor-specific database protocol. Here all the database
connectivity drivers are present in a single server, hence no need of individual client-side
installation. Type-3 drivers are fully written in Java, hence they are portable drivers. No client
side library is required because of application server that can perform many tasks like auditing,
load balancing, logging etc.Network support is required on client machine. Maintenance of
Network Protocol driver becomes costly because it requires database-specific coding to be
done in the middle tier. Switch facility to switch over from one database to another database.
Thin driver
Type-4 driver is also called native protocol driver. This driver interact directly with database.
It does not require any native database library, that is why it is also known as Thin Driver. If
you are accessing one type of database, such as Oracle, Sybase, or IBM, the preferred driver
type is type-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.