Applets: Unit - V
Applets: Unit - V
UNIT – V
Applet: Basics, Architecture, Applet Skeleton, requesting repainting, using the status
window, passing parameters to applets
GUI Programming with Swings – The origin and design philosophy of swing, components
and containers, layout managers, event handling, using a push button, jtextfield, jlabel
and image icon, the swing buttons, jtext field, jscrollpane, jlist, jcombobox, trees, jtable,
An overview of jmenubar, jmenu and jmenuitem, creating a main menu,
showmessagedialog, showconfirmdialog, showinputdialog, showoptiondialog, jdialog,
create a modeless dialog.
Accessing Databases with JDBC: Types of Drivers, JDBC Architecture, JDBC classes and
Interfaces, Basic steps in developing JDBC applications, Creating a new database and table
with JDBC.
--------------------------------------------------------------------------------------------------------
APPLETS
5.11.Introduction:
Applet – Definition:
main()
Present Not present
metho
Page 1
JAVA UNIT-5
d
Requires a java enables web
browser like Chrome, Netscape
Execution Requires JRE
Navigator, Opera, HotJava and so
on.
Called as stand-alone
Requires some third party tool help
Nature application as application
like a browser to execute
can be executed from
command prompt.
Can access any data or cannot access anything on the
Restrictions
software available on the system except browser’s services.
system
Page 2
JAVA UNIT-5
Four methods in the Applet class of java.applet package gives us the framework
to build an applet.
1. init( )
2. start( )
3. stop( )
4. destroy( )
1. New Born State: (Initialization State)
Applet enters to this state when it is first loaded by calling init() method.
In this stage, we will do the following:
1. Create objects needed by applet
2. Setup initial values
3. Load images and fonts
4. Setup colors.
We should override init( ) method as follows:
publi void init( ) {...................}
This method is called only once during the lifetime of the applet.
2. Runnning State
Applet enters this state when the system calls start( ) method.
It calls automatically, once the applet is initialized.
It also moves an idle applet to running state.
If required, this method can be overrided as
follows: public void start( )
{
----------// @override@ ------------
}
3. Idle or Stopped State
An applet becomes idle when it is stopped from running.
Stopping occurs automatically when we leave the page containing the
currently running applet then stop( ) method is invoked.
If required, this method can be overrided as
follows: public void stop( )
{
----------// @override@ ------------
}
4. Dead State
An applet is said to be dead, when it is removed from memory.
It occurs automatically by invoking the destroy( ) method when we
quit the browser.
If required, this method can be overrided as
follows: public void destroy( )
Page 3
JAVA UNIT-5
{
----------// @override@ ------------
}
5. Display State
Applet moves to this state whenever it has to perform some output
operations on the screen.
The paint( ) is called to display.
It is called after the applet enter into the running state.
Every applet will have a paint( ) method and it must be overrided as
follows: public void paint(Graphics g)
{
----------// @override@ ------------
}
The paint( ) method takes one parameter of type Graphics and it
contains graphics context.
Page 4
JAVA UNIT-5
import java.awt.Graphics;
{
----------------------------------------
----------------------------------------
}
}
Executing an Applet
Two ways: 1. Using Web Browser
2. Using appletviewer command
1. Using Web Browser
-- Build applet code (.java file)
-- Compile applet code (.class file)
-- create HTML file (.html file)
<HTML>
<applet code = ".class file" height = 100 width = 150>
</applet>
</HTML>
2. Using appletviewer command
To view and test an applet more conveniently during development
stage, we can use appletviewer command at command prompt.
Simply include a comment at the head of the java sourcefile that contains
the
<APPLET> tag.
/* <HTML>
</applet>
</HTML> */
import java.awt.Graphics;
Page 5
JAVA UNIT-5
/* <HTML>
<Applet code="Message.class" height=100 width=150>
</Applet>
</HTML> */
{
g.drawString("welcome to applets",10,100);
}
}
Output:
At command prompt
An applet writes to its window only when its update( ) or paint( ) method is
called by the AWT.
How can the applet itself cause its window to be updated when its information
Page 6
JAVA UNIT-5
import java.awt.*;
import java.applet.Applet;
import java.awt.Graphics;
Page 7
JAVA UNIT-5
/*
<applet code=" RepaintDemo " width=300 height=50>
</applet>
*/
// class extending
public class RepaintDemo extends Applet
{
int test=2;
public void paint(Graphics graphics)
{
super.paint(graphics);
setBackground(Color.cyan); // set backgroung color of window
graphics.setColor(Color.black); // set color of Text appearing on window
graphics.drawString("Value of Variable test = "+test, 80, 80);
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex){}
// increasing value of variable by 1 and update its value of GUI
test++;
repaint();
}
}
Output:
At command prompt
Page 8
JAVA UNIT-5
In the above example an applet and a variable called test is declared inside it.
We are continuously incrementing value of variable test and we want to ensure that
updated value of variable is visible on user interface. Therefore we are making use of
repaint method that ensures to call update method before paint method. Output of the
above program. In this window value of test variable is always incrementing and
updated value is visible on GUI.
import java.applet.*;
/*
<applet code="StatusWindow" width=300 height=50>
</applet>
*/
public class StatusWindow extends Applet{
public void init() {
setBackground(Color.cyan);
}
// Display msg in applet window. public void paint(Graphics g) {
g.drawString("This is in the applet window.",10, 20);
showStatus("This is shown in the status window.");
}
}
Page 9
JAVA UNIT-5
Syntax:
public String getParameter(String parameterName)
Page 10
JAVA UNIT-5
Output:
JDBC is used to interact with various type of Database such as Oracle, MS Access,
My SQL and SQL Server.
The JDBC API consists of classes and methods that are used to perform various
operations like: connect, read, write and store data in the database.
How JDBC connect Java Application to the database by following image.
JDBC Driver
JDBC Driver is required to establish connection between application and
database. It also helps to process SQL requests and generating result.
Types of driver
Page 11
JAVA UNIT-5
JDBC-ODBC Bridge:
Type-1 Driver acts as a bridge between JDBC and other database connectivity
mechanism (ODBC).
This driver converts JDBC calls into ODBC calls and redirects the request to the
ODBC driver.
Note: In Java 8, the JDBC-ODBC Bridge has been removed.
Advantage
Easy to use
Allow easy connectivity to all database supported by the ODBC Driver.
Disadvantage
Slow execution time
Dependent on ODBC Driver.
Uses Java Native Interface(JNI) to make ODBC call.
Page 12
JAVA UNIT-5
Advantage
faster as compared to Type-1 Driver
Contains additional features.
Disadvantage
Requires native library
Increased cost of Application
Advantage
Does not require any native library to be installed.
Database Independency.
Provide facility to switch over from one database to another database.
Disadvantage
Slow due to increase number of network call.
Page 13
JAVA UNIT-5
Thin Driver
This is Driver called Pure Java Driver because. This driver interact directly with
database.
It does not require any native database library, that is why it is also known as
Thin Driver.
Advantage
Does not require any native library.
Does not require any Middleware server.
Better Performance than other driver.
Disadvantage
Slow due to increase number of network call.
JDBC Architecture
Page 14
JAVA UNIT-5
This JDBC driver translates the requests of a Java application such a database
query like database access, update or storing procedures from Java language to
Structured Query Language(SQL), which is then forwarded to a specific database of
choice of this Java application.
These SQL statements are executed by the preferred database and the result is
sent back to JDBC driver. The JDBC driver sends the result back to Java
application(through JDBC API) in the form which it can easily understand. Please refer
to Figure - 1.
JDBC classes and interface required to connect a Java application to a database are -
Driver interface - A Java application can access a database through its driver. Driver
interface represents the database driver that is used to establish the connectivity of a
Java application with the database.
Page 15
JAVA UNIT-5
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
2. Create a Connection
After registering and loading the driver in step1, now we will create a
connection using getConnection() method of DriverManager class.
This method has several overloaded methods that can be used based on the
requirement.
Basically it require the database name, username and password to establish
connection. Syntax of this method is given below.
Syntax
Page 16
JAVA UNIT-5
getConnection(String url)
getConnection(String url, String username, String password)
getConnection(String url, Properties info)
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE",
"username","password"
);
Page 17
JAVA UNIT-5
Now lets combine all these steps into a single example and create a complete
example of JDBC connectivity.
Example: All Steps into one place
import java.sql.*;
class Test {
public static void main(String[] args) {
try {
//Loading driver
Class.forName("oracle.jdbc.driver.OracleDriver");
//creating connection
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE",
"username", "password");
Statement s = con.createStatement(); //creating statement
ResultSet rs = s.executeQuery("select * from Student"); //executing statement
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
con.close(); //closing connection
} catch (Exception e) {
e.printStacktrace();
}
}
}
CREATE a table with JDBC
Page 18
JAVA UNIT-5
try
{
//SQL CREATE TABLE query
String query = "CREATE TABLE MyTable (" + "ID int," + "FirstName
varchar(30)," + "LastName varchar(30)," + "Age int )";
Output:
Page 19