Java_UNIT_4
Java_UNIT_4
BCA IV SEM
Syllabus
Swings Fundamentals: Components (JLabel and ImageIcon, using
swing Buttons (JButton, JToggleButton, JCheckBox, JRadioButton),
JTextField, JScrollPane, JList, JComboBox) and Containers, Layout
managers, event delegation Model, event handling (event sources, event
listeners, event classes and interfaces, adapter classes).
JDBC: JDBC Architecture, JDBC Drivers, Connection, Statement,
Prepared Statement, Result set, Connecting to the Database using JDBC.
Applets
Applet is a special type of program that is embedded in the webpage to
generate the dynamic content. It runs inside the browser and works at
client side.
Advantage of Applet
There are many advantages of applet. They are as follows:
It works at client side so less response time.
Secured
It can be executed by browsers running under many plateforms, including
Linux, Windows, Mac Os etc.
Lifecycle of Java Applet
Lifecycle methods for Applet
public void init(): is used to initialized the Applet. It is invoked
only once.
public void start(): is invoked after the init() method or browser is
maximized. It is used to start the Applet.
public void stop(): is used to stop the Applet. It is invoked when
Applet is stop or browser is minimized.
public void destroy(): is used to destroy the Applet. It is invoked
only once.
How to run an Applet?
Java AWT is an API to develop GUI Swing is a part of Java Foundation Classes
1.
applications in Java and is used to create various applications.
The components of Java AWT are heavy The components of Java Swing are light
2.
weighted. weighted.
Java AWT has comparatively less functionality Java Swing has more functionality as
3.
as compared to Swing. compared to AWT.
The execution time of AWT is more than The execution time of Swing is less than
4.
Swing. AWT.
The components of Java AWT are platform The components of Java Swing are platform
5.
dependent. independent.
AWT is a thin layer of code on top of the Swing is much larger swing also has very
9
operating system. much richer functionality.
Program
Example of CardLayout Class: Using Parameterized
Constructor
ScrollPaneLayout
Events
Events Sources
Events Listeners
Events
The Events are the objects that define state change in a source.
An event can be generated as a reaction of a user while interacting with GUI
elements.
Some of the event generation activities are moving the mouse pointer,
clicking on a button, pressing the keyboard key, selecting an item from the
list, and so on.
We can also consider many other user operations as events.
The Events may also occur that may be not related to user interaction, such
as a timer expires, counter exceeded, system failures, or a task is completed,
etc.
We can define events for any of the applied actions.
Classification of Events
Classification of Events
Foreground Events
Foreground events are the events that require user interaction to generate,
i.e., foreground events are generated due to interaction by the user on
components in Graphic User Interface (GUI).
Interactions are nothing but clicking on a button, scrolling the scroll bar,
cursor moments, etc.
Background Events
Events that don’t require interactions of users to generate are known as
background events.
Examples of these events are operating system failures/interrupts,
operation completion, etc.
Event Sources
In few cases, the event notification will only be sent to listeners that
register to receive them.
Some listeners allow only one listener to register. Below is an example:
From the above syntax, the Type is the name of the event, and e2 is the
event listener's reference. When the specified event occurs, it will be
notified to the registered listener. This process is known
as unicasting events.
Event Sources
A source is an object that causes and generates an event.
It generates an event when the internal state of the object is changed.
The sources are allowed to generate several different types of events.
A source must register a listener to receive notifications for a specific event.
Each event contains its registration method.
From the above syntax, the Type is the name of the event, and e1 is a reference to
the event listener.For example, for a keyboard event listener, the method will be
called as addKeyListener().
For the mouse event listener, the method will be called
as addMouseMotionListener(). When an event is triggered using the
respected source, all the events will be notified to registered listeners and
receive the event object. This process is known as event multicasting.
Advantages of Using the Event Delegation Model in Java
Efficient event handling: The Event Delegation Model avoids the
overhead of sending events to every component in the GUI hierarchy by
delegating the event handling task to the appropriate component. This
makes event handling more efficient and reduces the computational load
on the system.
Flexibility: The Event Delegation Model allows for dynamic changes in
the GUI hierarchy, as components can be added or removed without
affecting the event handling process. This makes it easy to modify the GUI
without affecting the underlying coding.
Improved code organization: The Event Delegation Model allows
developers to separate the code that generates events from the code that
handles events. This improves the organization and readability of the
code, making it easier to maintain and modify in the future.
Advantages of Using the Event Delegation Model in Java
We can put the event handling code into one of the following places:
Within class
Other class
Anonymous class
Java event handling by implementing ActionListener
Java event handling by outer class
Java Adapter Classes
Java adapter classes provide the default implementation of
listener interfaces.
If you inherit the adapter class, you will not be forced to provide the
implementation of all the methods of listener interfaces. So it saves code.
The adapter classes are found in java.awt.event,
java.awt.dnd and javax.swing.event packages.
Pros of using Adapter classes
JDBC API: JDBC API provides various interfaces and methods to establish
easy connection with different databases.
JDBC Test Suite: JDBC Test suite facilitates the programmer to test the
various operations such as deletion, updation, insertion that are being
executed by the JDBC Drivers.
JDBC Components
JDBC Driver Manger: JDBC Driver manager loads the database-specific
driver into an application in order to establish the connection with the
database.
The JDBC Driver manager is also used to make the database-specific call
to the database in order to do the processing of a user request.
JDBC ODBC Bridge Driver: JDBC-ODBC Bridge Drivers are used to
connect the database drivers to the database.
The bridge does the translation of the JDBC method calls into the ODBC
method call. It makes the usage of the sun.jdbc.odbc package that
encompasses the native library in order to access the ODBC (Open
Database Connectivity) characteristics.
Architecture of JDBC
Application: It is the Java servlet or an applet that communicates with the data source.
The JDBC API: It allows the Java programs to perform the execution of the SQL
statements and then get the results.
A few of the crucial interfaces and classes defined in the JDBC API are the following:
Drivers
DriverManager
Statement
Connection
CallableStatement
PreparedStatement
ResultSet
SQL data
DriverManager: DriverManager plays a crucial role in the architecture of JDBC.
It uses database-specific drivers to connect the enterprise applications to various
databases.
JDBC drivers: To interact with a data source with the help of the JDBC, one needs a JDBC
driver which conveniently interacts with the respective data source.
Components of JDBC
Different Types of Architecture of JDBC
The architecture of the JDBC consists of two and three tiers model in
order to access the given database.
Two-tier model: In this model, the application interacts directly with the
source of data. The JDBC driver establishes the interaction between the
data source and the application. When a query is sent by the user to the
data source, the reply of those sent queries is sent directly to the user.
The source of data can be located on a different machine, and that
machine is connected to the user machine following a client-server
paradigm, where the machine which is sending the query is the client
machine, and the machine that is sending the result of those queries is
acting as the server.
Different Types of Architecture of JDBC
Three-tier model: In this model, the queries of the user are being sent to
the middle-tier services, from where the commands are sent again to the
source of data.
The answers to those queries are reverted to the middle tier, and from
there, it is again sent to the user.
JDBC Driver
There are 5 steps to connect any java application with the database using
JDBC. These steps are as follows:
Register the Driver class
Create connection
Create statement
Execute queries
Close connection
Java Database Connectivity with 5 Steps
Register the driver class
The forName() method of Class class is used to register the driver class.
This method is used to dynamically load the driver class.
Create the connection object
The getConnection()
method of
DriverManager class is
used to establish
connection with the
database.
Create the Statement object
The createStatement()
method of Connection
interface is used to
create statement. The
object of statement is
responsible to execute
queries with the
database.
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.
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.
Java Database Connectivity with MySQL
In this example we are using MySql as the database. So we need to know
following informations for the mysql database:
Driver class: The driver class for the mysql database
is com.mysql.jdbc.Driver.
Connection URL: The connection URL for the mysql database
is jdbc:mysql://localhost:3306/test where jdbc is the API, mysql is the
database, localhost is the server name on which mysql is running, we may
also use IP address, 3306 is the port number and test is the database name.
We may use any database, in such case, we need to replace the test with
our database name.
Username: The default username for the mysql database is root.
Password: It is the password given by the user at the time of installing the
mysql database. In this example, we are going to use root as the password.
Example to Connect Java Application with mysql database
Example to Connect Java Application with mysql database
with User Input
Example to update data in mysql database
with User Input
Example to Delete data in mysql database
Example to Delete data in mysql database
with User Input