UNIT - 4 Java
UNIT - 4 Java
❖ Swing :-
Swing is a part of Java's standard library used for building Graphical User Interfaces (GUIs). It is part of the
Java Foundation Classes (JFC) and provides a rich set of GUI components for creating desktop applications.
AWT is part of the Java Foundation Classes (JFC), but Swing (which came later) is built on top of AWT
and is more advanced.
COMPONENTS in Java:
A Component is an object having a graphical representation that can be displayed on the screen and can
interact with the user.
Common Characteristics:
Component Description
JLabel Displays a short text or image
ImageIcon It allows you to load and display an image in a JLabel.
JButton Clickable button
JTextField Allows single-line text input
JTextArea Multi-line editable text area
JCheckBox A checkbox to toggle true/false
JRadioButton Used in groups for selecting one option
JComboBox Drop-down menu
JList A list of selectable items
JTable Displays tabular data
JScrollBar Scrollbar used with scrollable content
Layout managers are used to arrange components (like buttons, labels, text fields) in a container (like
JFrame or JPanel) in a specific pattern or layout.
1. FlowLayout
2. BorderLayout
3. GridLayout
Java uses the Event Delegation Model to handle user interactions such as mouse clicks, key presses, or
button actions.
Key Concepts:
1. Event Source – The GUI component that generates the event (e.g., JButton)
2. Event Object – Carries information about the event (e.g., ActionEvent)
3. Event Listener – Interface that receives the event and performs some action (e.g., ActionListener)
What Is Event Handling in Java?
Event Handling in Java refers to the mechanism that allows your GUI program to respond to user
interactions such as:
• Clicking a button
• Pressing a key
• Selecting an item from a combo box, etc
Core Concepts:
JDBC is an API that helps applications to communicate with databases, it allows Java programs to connect
to a database, run queries, retrieve, and manipulate data. Because of JDBC, Java applications can easily
work with different relational databases like MySQL, Oracle, PostgreSQL, and more.
Explanation:
Application: It can be a Java application or servlet that communicates with a data source.
The JDBC API: It allows Java programs to execute SQL queries and get results from the database. Some
key components of JDBC API include -
o Interfaces like Driver, ResultSet, RowSet, PreparedStatement, and Connection that helps
managing different database tasks.
o Classes like DriverManager, Types, Blob, and Clob that helps managing database connections.
DriverManager: Driver manager is responsible for loading the correct database-specific driver to establish
a connection with the database. It manages the available drivers and ensures the right one is used to
process user requests and interact with the database.
It plays an important role in the JDBC architecture. It uses some database-specific drivers to effectively
connect enterprise applications to databases.
JDBC Test Suite: It is used to test the operation(such as insertion, deletion, updating) being performed by
JDBC Drivers.
JDBC drivers: These drivers handle interactions between the application and the database. JDBC drivers
are client-side adapters (installed on the client machine, not on the server) that convert requests from Java
programs to a protocol that the DBMS can understand.
Connecting to the Database using JDBC
To connect a Java application to a database, you need to perform the following steps using JDBC:
JDBC Drivers :-
JDBC (Java Database Connectivity) drivers are essential components that act as a bridge between Java
applications and databases. They allow Java programs to communicate with various database systems by
converting Java calls into database-specific commands. There are four types of JDBC drivers, each with
different architectures and use cases.
1. Type 1 – JDBC-ODBC Bridge Driver: This driver uses the ODBC (Open Database Connectivity)
driver to interact with the database. It acts as a bridge between JDBC and ODBC. However, it's
platform-dependent and no longer supported in Java 8 and above.
2. Type 2 – Native-API Driver: This driver converts JDBC calls into native database API calls using
native libraries provided by the database vendor. It is faster than Type 1 but still platform-dependent.
3. Type 3 – Network Protocol Driver: It communicates with a middleware server, which then
communicates with the database. This driver is platform-independent and suitable for internet-based
applications.
4. Type 4 – Thin Driver (Pure Java Driver): This is the most widely used and recommended driver.
It converts JDBC calls directly into the database-specific protocol without needing native libraries or
middleware. It is platform-independent and ideal for web-based applications.
In JDBC, the Connection interface is used to establish a link between a Java application and a database. It
represents a session with a specific database. Through this connection, SQL statements can be executed,
transactions can be managed, and data can be retrieved or updated.
To obtain a Connection object, the DriverManager.getConnection() method is used, which requires the
database URL, username, and password.
Statement in JDBC
The Statement interface in JDBC is used to execute static SQL queries (i.e., queries without parameters)
against the database. It is a part of the java.sql package and works with the Connection interface to send
SQL commands such as SELECT, INSERT, UPDATE, or DELETE.
PreparedStatement in JDBC
Steps to Connect: