Contents
• Applet Fundamentals
• Applet Tag
Unit 5 • Applet life-cycle
• Passing parameters to applets
• Graphics Programming
• JDBC drivers
• JDBC connection
2 September 2021 1 2 September 2021 2
Applets Applets
• Small Java programs that can be downloaded and • Applet developed locally and stored in a local
executed in a Java enabled Web browser. system is known as local applet.
• Transported via internet.
• Does not require any internet connection.
• Perform arithmetic operations, display graphics,
play sounds, create animations, play interactive • Remote applet is developed by someone else
games etc. and stored on a remote computer.
• Two types of applets • If our computer is connected to internet, we
1. Local applet can download a remote applet via internet
2. Remote applet and runs it.
2 September 2021 3 2 September 2021 4
Building applet code Applet Tag
• Two classes Applet and Graphics needs to be • <applet> tag is used to start an applet from an HTML
imported. document and from an appletviewer.
<Applet>
• The Applet class is contained in the java.applet [CODE BASE=codebase URL]
package. CODE = appletfile
Example Program [ALT=alternate Text]
WIDTH=pixels HEIGHT=pixels
[ALIGN=alignment]
[<PARAM NAME=attributename1 VALUE=value1>]
[<PARAM NAME=attributename2 VALUE=value2>]
</Applet>
2 September 2021 5 2 September 2021 6
Applet Tag Applet Tag
[CODE BASE=codebase URL] – This is an optional [ALT=alternate Text] – This is an optional
attribute which specifies the directory that attribute which specifies a short text message
will be searched for the applet’s executable that should be displayed if the browser
class file. understands the applet tag, but cannot run
CODE = appletfile – This is a required attribute Java applets.
that gives the name of the file that contains WIDTH=pixels HEIGHT=pixels – These are
the applet compiled subclass. required attributes that give the initial size in
pixels of the applet display area.
2 September 2021 7 2 September 2021 8
Applet Tag Passing Parameters to Applets
[ALIGN=alignment] - This is an optional [<PARAM NAME=attributename1 VALUE=value1>]
attribute that specifies the alignment of the • Each <PARAM> tab has a NAME attribute and a
attribute. VALUE attribute.
[<PARAM NAME=attributename1 • Example
VALUE=value1>] – This attribute is used to <Applet>
pass parameter to applets. <PARAM NAME=COLOR VALUE=“Red”>
</Applet>
Example Program
2 September 2021 9 2 September 2021 10
Life Cycle of an Applet Life Cycle of an Applet
• init() – this method is called first. This is where
we should initialize variables.
• start() – this method is called after init(). This
is also called after an applet has been
stopped. While init() is called once(first time
an applet is loaded), start() is called each time
an applet’s HTML document is displayed on
screen. For example, a user leaves a Web page
and comes back.
2 September 2021 11 2 September 2021 12
Life Cycle of an Applet Life Cycle of an Applet
• paint() – This method is called each time the • stop() – This method is called when a Web
applet is damaged. If any other window covers browser leaves the HTML document containing
the applet, the window system calls paint() the applet. When the user returns to the page
method. applets are restarted using start() method.
• update() – The default update method in class • destroy() – This method is called when the
Applet first fills an applet with default runtime environment determines that the applet
needs to be removed completely from memory.
background color and then calls paint().
• repaint() – Calling repaint() in turn calls update()
method.
2 September 2021 13 2 September 2021 14
Life Cycle of an Applet Graphics Programming
• Getting Input from the user – Example • Every applet has its own area of the screen
Program called canvas.
• Java’s co-ordinate system has the origin(0,0) in
the upper left corner.
• Positive x-values are to the right and positive
y-values are to the bottom.
• The value of co-ordinates x and y are in pixels.
2 September 2021 15 2 September 2021 16
Graphics Class - Methods Graphics Class - Methods
1. drawArc() – draws an arc. 7. drawString() – displays a string.
2. drawLine() – draws a straight line. 8. fillArc() – draws a filled arc.
9. fillOval() – draws a filled oval.
3. drawOval() – draws an oval.
10. fillPolygon() – draws a filled polygon.
4. drawPolygon() – draws a polygon. 11.fillRect() – draws a filled rectangle.
5. drawRect() – draws a hollow rectangle. 12.fillRoundRect() – draws a filled rectangle with
6. drawRoundRect() – draws a hollow rectangle rounded corners.
with rounded corners. 13.getColor() – retrieves the current drawing color.
14.setColor() – sets the drawing color.
2 September 2021 17 2 September 2021 18
Graphics Class - Methods Graphics Class - Methods
Example program for drawLine(), drawRect(), • fillRect() method is similar to drawRect()
fillRect(),drawRoundRect(), fillRoundRect() except that it draws a solid rectangle.
• drawLine() method takes 2 pairs of co- • fillRoundRect() and drawRoundRect() method
ordinates and draws a line between them. takes 6 arguments, the two extra arguments
• drawRect() method takes 4 arguments, first representing the width and height of the
two represents the x and y co-ordinates of angle of corners.
the top left corner of the rectangle and
remaining 2 represents the width and height
of the rectangle.
2 September 2021 19 2 September 2021 20
Graphics Class - Methods Graphics Class - Methods
Example Program for drawOval() Example Program for drawArc() and fillArc()
• drawOval() method can be used to draw a • drawArc() and fillArc() method takes 6
circle. arguments. The first 4 is same as that of
• Takes 4 arguments. The first 2 represent the drawOval(), and the last 2 represent the start
top left corner of the imaginary rectangle and angle and sweep angle (no: of degrees)
the other 2 represents the width and height around the arc.
of the oval itself.
• If the width and height are same, the oval • Zero degrees is at 3’O Clock and degrees
becomes a circle. increase in a counter-clockwise direction.
2 September 2021 21 2 September 2021 22
Graphics Class - Methods Color Class
Example Program for drawPolygon() and • We can use static variables in Color to specify
fillPolygon() a number of common colors. (Example:
• Both have the form drawPolygon(int[], Color.blue)
int[],int) and fillPolygon(int[], int[], int). • Color(int,int,int) – takes red green and blue
integers between 0 and 255.
• Color(float,float,float) – takes 3 float values
between 0.0 and 1.0 for red, green and blue.
2 September 2021 23 2 September 2021 24
JDBC-ODBC bridge driver
Java Data Base Connectivity-JDBC
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)
2 September 2021 25 2 September 2021 26
Network Protocol driver (fully Java
Native-API driver (partially Java driver)
driver)
2 September 2021 27 2 September 2021 28
Thin driver (fully Java driver)
Steps to connect to a Database
1. Register the driver class.
2. Create the connection object.
3. Create the Statement object.
4. Execute the query.
5. Close the connection object
2 September 2021 29 2 September 2021 30
1. Register the driver class 2. Create the connection object
• The forName() method of Class is used to • getConnection() method of DriverManager
register the driver class. class is used to establish connection with the
• This method is used to dynamically load the database.
driver class. • Syntax of getConnection() method.
• Syntax of forName() method Connection conn = DriverManager.getConnection(dbURL,
username, password);
Class.forName(“com.mysql.jdbc.Driver") Example
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost3306/mysql",“root","");
2 September 2021 31 2 September 2021 32
3. Create the Statement object
4. Execute the query
• createStatement() method of Connection • The executeQuery() method of Statement interface is
used to execute queries to the database.
interface is used to create statement.
• This method returns the object of ResultSet that can
• Example of createStatement() method. be used to get all the records of a table.
Statement stmt=con.createStatement(); Example
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
2 September 2021 33 2 September 2021 34
5 Close the connection object DriverManager Class
• By closing connection object statement and • DriverManager class acts as an interface
ResultSet will be closed automatically. between user and drivers.
• The close() method of Connection interface is • It keeps track of the drivers that are available
used to close the connection. and handles establishing a connection
Example between a database and the appropriate
con.close(); driver.
2 September 2021 35 2 September 2021
DriverManager Class - Methods Connection Interface
• public static void registerDriver(Driver driver): To • Provides many methods for transaction
register the given driver with DriverManager.
management like commit(), rollback() etc.
• public static void deregisterDriver(Driver driver):To
deregister the given driver with DriverManager. • By default, connection commits the changes
• public static Connection getConnection(String url):To after executing queries.
establish the connection with the specified url.
• public static Connection getConnection(String url,String
userName,String password): To establish the
connection with the specified url, username and
password.
2 September 2021 2 September 2021
Connection Interface - Methods Statement Interface - Methods
• public Statement createStatement(): creates a • Provides methods to execute queries with the
statement object that can be used to execute SQL
queries. database.
• public void setAutoCommit(boolean status): is used to • public ResultSet executeQuery(String sql): is
set the commit status. By default it is true. used to execute SELECT query. It returns the
• public void commit(): saves the changes made since object of ResultSet.
the previous commit/rollback permanent.
• public void rollback(): Drops all changes made since the • public int executeUpdate(String sql): is used to
previous commit/rollback. execute specified query. It may be create,
• public void close(): closes the connection and releases drop, insert, update, delete etc.
a JDBC resource immediately.
2 September 2021 2 September 2021
Programs
• Insert Records
• Update Records
• Delete Records
End of Unit 5
2 September 2021 41 2 September 2021 42