Unit 4
Unit 4
Unit 4
Swing has about four times the number of User Interface [UI] components as
AWT and is part of the standard Java distribution. By today’s application GUI
requirements, AWT is a limited implementation, not quite capable of providing
the components required for developing complex GUI’s required in modern
commercial applications. The AWT component set has quite a few bugs and
really does take up a lot of system resources when compared to equivalent
Swing resources.
JFC
Java Foundation Classes (JFC) are a set of GUI components which simplify the
development of desktop applications.
import javax.swing.*;
public class Simple2 extends JFrame{//inheriting JFrame
JFrame f;
Simple2(){
JButton b=new JButton("click");
b.setBounds(130,100,100, 40);
add(b);
setSize(400,500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new Simple2();
}
}
Prepared by : Dr. Ahmad Jamal
Prepared by : Dr. Ahmad Jamal
Programming using Panes
A layered pane is a Swing container which is used to hold the various
components using the concept of layers. The components present in the upper
layer overlaps the components present in the lower layer. The layered pane is
created using the JLayeredPane class. the only constructor of this class is
JLayeredPane ().
“Look” refers to the appearance of GUI widgets and “feel” refers to the way the
widgets behave.
import javax.swing.UIManager;
public class MainClass { public static void main(String[] a)
{
UIManager.LookAndFeelInfo[] looks =
UIManager.getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo look : looks) {
System.out.println(look.getClassName());
}
}
}
The layout manager automatically positions all the components within the
container. Even if you do not use the layout manager, the components are still
positioned by the default layout manager.
Java provides various layout managers to position the controls. Properties like
size, shape, and arrangement varies from one layout manager to the other.
When the size of the application window changes, the size, shape, and
arrangement of the components also changes in response, i.e. the layout
managers adapt to the dimensions of the application window.
Prepared by : Dr. Ahmad Jamal
AWT Layout Manager Classes
commonly used controls while designing GUI using AWT
BorderLayout: The borderlayout arranges the components to fit in the five
regions: east, west, north, south, and center.
JDBC-ODBC Bridge Driver
Native Driver
Network Protocol Driver
Thin Driver
We can use JDBC API to access tabular data stored in any relational database. By the
help of JDBC API, we can save, update, delete and fetch data from the database.
Before JDBC, ODBC API was the database API to connect and execute the query with the
database. But, ODBC API uses ODBC driver which is written in C language (i.e. platform
dependent and unsecured). That is why Java has defined its own API (JDBC API) that
uses JDBC drivers (written in Java language).
We can use JDBC API to handle database using Java program and can perform the
following activities:
JDBC Driver is a software component that enables java application to interact with the
database. There are 4 types of JDBC drivers:
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-
ODBC bridge driver converts JDBC method calls into the ODBC function calls.
Disadvantages:
1: Performance degraded because JDBC method call is converted into the ODBC function
calls.
2: The ODBC driver needs to be installed on the client machine.
The Native API driver uses the client-side libraries of the database.
The driver converts JDBC method calls into native calls of the
database API. It is not written entirely in java.
Advantage:
1: Performance upgraded than JDBC-ODBC bridge driver.
Disadvantage:
1: The Native driver needs to be installed on the each client machine.
Prepared by : Dr. Ahmad Jamal
2: The Vendor client library needs to be installed on client machine.
3) Network Protocol driver
The Network Protocol driver uses middleware (application server) that converts JDBC calls
directly or indirectly into the vendor-specific database protocol. It is fully written in java.
Advantage:
1: No client side library is required because of application server that can perform many tasks like
auditing, load balancing, logging etc.
Disadvantages:
1: Network support is required on client machine.
2: Requires database-specific coding to be done in the middle tier.
3: Maintenance of Network Protocol driver becomes costly because it requires database- specific
coding to be done in the middle tier. Prepared by : Dr. Ahmad Jamal
4: Thin driver
The thin driver converts JDBC calls directly into the vendor-
specific database protocol. That is why it is known as thin driver. It is
fully written in Java language.
Advantage:
1: Better performance than all other drivers.
2: No software is required at client side or server side.
Disadvantage:
1: Drivers depend on the Database.
Prepared by : Dr. Ahmad Jamal
Java Database Connectivity with 5 Steps
The forName() method of Class class is used to register the driver class. This method
is used to dynamically load the driver class.
Note: Since JDBC 4.0, explicitly registering the driver is optional. We just need to put
vender's Jar in the classpath, and then JDBC driver manager can detect and load the
driver automatically.
Note:
1. Username: The default username for the mysql database is root.
2. 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.
Prepared by : Dr. Ahmad Jamal
3) Create the Statement object
Example:
Statement stmt=con.createStatement()
Example:
Syntax
public void close()throws SQLException
Example:
con.close();
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;