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

More Related Content

Similar to Unit 5 Java Programming with Linux-converted.pdf

Char word counter in Python with simple gui - PROJECT
Char word counter in Python with simple gui - PROJECTChar word counter in Python with simple gui - PROJECT
Char word counter in Python with simple gui - PROJECTMahmutKAMALAK
 
React Native One Day
React Native One DayReact Native One Day
React Native One DayTroy Miles
 
Python SQite3 database Tutorial | SQlite Database
Python SQite3 database Tutorial | SQlite DatabasePython SQite3 database Tutorial | SQlite Database
Python SQite3 database Tutorial | SQlite DatabaseElangovanTechNotesET
 
Intro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & ArraysIntro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & ArraysBlue Elephant Consulting
 
Name _______________________________ Class time __________.docx
Name _______________________________    Class time __________.docxName _______________________________    Class time __________.docx
Name _______________________________ Class time __________.docxrosemarybdodson23141
 
College for Women Department of Information Science
College for Women  Department of Information Science  College for Women  Department of Information Science
College for Women Department of Information Science WilheminaRossi174
 
College for women department of information science
College for women  department of information science  College for women  department of information science
College for women department of information science AMMY30
 
Introduction to Coding
Introduction to CodingIntroduction to Coding
Introduction to CodingFabio506452
 
SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...
SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...
SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...Sencha
 
Scene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game EnginesScene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game EnginesBryan Duggan
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN StackTroy Miles
 
Learning to Spot and Refactor Inconsistent Method Names
Learning to Spot and Refactor Inconsistent Method NamesLearning to Spot and Refactor Inconsistent Method Names
Learning to Spot and Refactor Inconsistent Method NamesDongsun Kim
 

Similar to Unit 5 Java Programming with Linux-converted.pdf (20)

Char word counter in Python with simple gui - PROJECT
Char word counter in Python with simple gui - PROJECTChar word counter in Python with simple gui - PROJECT
Char word counter in Python with simple gui - PROJECT
 
React Native One Day
React Native One DayReact Native One Day
React Native One Day
 
Python SQite3 database Tutorial | SQlite Database
Python SQite3 database Tutorial | SQlite DatabasePython SQite3 database Tutorial | SQlite Database
Python SQite3 database Tutorial | SQlite Database
 
Intro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & ArraysIntro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & Arrays
 
Name _______________________________ Class time __________.docx
Name _______________________________    Class time __________.docxName _______________________________    Class time __________.docx
Name _______________________________ Class time __________.docx
 
cb streams - gavin pickin
cb streams - gavin pickincb streams - gavin pickin
cb streams - gavin pickin
 
College for Women Department of Information Science
College for Women  Department of Information Science  College for Women  Department of Information Science
College for Women Department of Information Science
 
College for women department of information science
College for women  department of information science  College for women  department of information science
College for women department of information science
 
Introduction to Coding
Introduction to CodingIntroduction to Coding
Introduction to Coding
 
Go Faster With Native Compilation
Go Faster With Native CompilationGo Faster With Native Compilation
Go Faster With Native Compilation
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
 
SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...
SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...
SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...
 
Scene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game EnginesScene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game Engines
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN Stack
 
Learning to Spot and Refactor Inconsistent Method Names
Learning to Spot and Refactor Inconsistent Method NamesLearning to Spot and Refactor Inconsistent Method Names
Learning to Spot and Refactor Inconsistent Method Names
 
Vectors Intro.ppt
Vectors Intro.pptVectors Intro.ppt
Vectors Intro.ppt
 
Data Exploration in R.pptx
Data Exploration in R.pptxData Exploration in R.pptx
Data Exploration in R.pptx
 
Java applets
Java appletsJava applets
Java applets
 
Chapter-3.pdf
Chapter-3.pdfChapter-3.pdf
Chapter-3.pdf
 
MATLAB Programming
MATLAB Programming MATLAB Programming
MATLAB Programming
 

Recently uploaded

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 

Recently uploaded (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 

Unit 5 Java Programming with Linux-converted.pdf

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