Java Unit1
Java Unit1
UNIT 1
By Shivani Deopa
SWING
What is JFC and AWT?
• The Java Foundation Classes are a graphical framework for building
portable Java-based graphical user interfaces. JFC consists of the
Abstract Window Toolkit, Swing and Java 2D.
• Abstract Window Toolkit (AWT) is a set of application program
interfaces ( API s) used by Java programmers to create graphical user
interface ( GUI ) objects, such as buttons, scroll bars, and windows.
What is Swing
• Java Swing is a part of Java Foundation Classes (JFC) that is used to create
window-based applications. Swing components facilitate efficient graphical user
interface (GUI) development.
• Swing Components are collection of lightweight visual components they contain
replacement for heavyweight AWT components as well as complex user-interface
components such as tables and tress.
• Components contain a pluggable look and feel (PL&F) that allows application to
run with native look and feel on different platforms. PL&F allows application to
have the same behaviour on various platform. JFC contains operating system
neutral look and feel.
• Swing component do not contain peers. Swing components allow mixing of AWT
heavyweight and swing light weight components in an application. Heavyweight
components have opaque pixels and are always rectangular whereas lightweight
components have transparent pixels and are non-regular.
Features of Swing:
1. Platform Independent: It is platform independent, the swing components
that are used to build the program are not platform specific. It can be used at
any platform and anywhere.
2. Lightweight: Swing components are lightweight which helps in creating
the UI lighter. Swings component allows it to plug into the operating system
user interface framework that includes the mappings for screens or device
and other user interactions like key press and mouse movements.
3. Plugging: It has a powerful component that can be extended to provide
the support for the user interface that helps in good look and feel to the
application. It refers to the highly modular-based architecture that allows it
to plug into other customized implementations and framework for user
interfaces. Its components are imported through a package called java.swing.
4. Manageable: It is easy to manage and configure. Its mechanism and
composition pattern allows changing the settings at run time as well.
The uniform changes can be provided to the user interface without
doing any changes to application code.
5. MVC: They mainly follows the concept of MVC that is Model View
Controller. With the help of this, we can do the changes in one
component without impacting or touching other components. It is
known as loosely coupled architecture as well.
6. Customizable: Swing controls can be easily customized. It can be
changed and the visual appearance of the swing component
application is independent of its internal representation.
Difference Between AWT and Swing
AWT Swing
AWT components are platform-dependent Java swing components are platform-independent.
2-A Class.forName()
• Here we load the driver’s class file into memory at the runtime. No need of using new or
create objects. The following example uses Class.forName() to load the Oracle driver as
shown below as follows:
Class.forName(“com.mysql.jdbc.Driver”);
2-B DriverManager.registerDriver()
• DriverManager is a Java inbuilt class with a static member register. Here we call the
constructor of the driver class at compile time. The following example uses
DriverManager.registerDriver()to register the Oracle driver as shown below:
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver())
Step 3: Establish a connection using the Connection class object
• After loading the driver, establish connections as shown below as follows:
Connection con = DriverManager.getConnection(url,user,password)
a) user: Username from which your SQL command prompt can be accessed.
b) password: password from which the SQL command prompt can be
accessed.
c) con: It is a reference to the Connection interface.
d) Url: Uniform Resource Locator which is created as shown below:
String url = “jdbc:mysql://localhost/empdetails”
• Where oracle is the database used, thin is the driver used, @localhost is
the IP Address where a database is stored,empdetails is the database
name. All 3 parameters above are of String type and are to be declared by
the programmer before calling the function. Use of this can be referred to
form the final code.
Step 4: Create a statement
• Once a connection is established you can interact with the database. The JDBCStatement,
CallableStatement, and PreparedStatement interfaces define the methods that enable you
to send SQL commands and receive data from your database.
• Use of JDBC Statement is as follows:
Statement st = con.createStatement();
rs.last();
int rowCount = rs.getRow();
Updating Data in a ResultSet
• By default, the ResultSet is read-only. However, we can use an updatable
ResultSet to insert, update, and delete the rows.
• ResultSet Concurrency
• The concurrency mode indicates if our ResultSet can update the data.
• The CONCUR_READ_ONLY option is the default and should be used if we don’t
need to update the data using our ResultSet.
• However, if we need to update the data in our ResultSet, then the
CONCUR_UPDATABLE option should be used.
• Not all databases support all the concurrency modes for all ResultSet types.
Therefore, we need to check if our desired type and concurrency mode are
supported using the supportsResultSetConcurrency() method:
This is used to store large binary data. This is used to store large textual data.
This stores values in the form of binary streams. This stores values in the form of character streams.
Using this you can stores files like videos, images, Using this you can store files like text files, PDF
gifs, and audio files. documents, word documents etc.
BLOB CLOB
MySQL supports this with the following MySQL supports this with the following
datatypes:TINYBLOB datatypes:TINYTEXT
•BLOB •TEXT
•MEDIUMBLOB •MEDIUMTEXT
•LONGBLOB •LONGTEXT
In JDBC API it is represented by java.sql.Blob In JDBC it is represented by java.sql.Clob Interface.
Interface.
The Blob object in JDBC points to the location of BLOB The Blob object in JDBC points to the location of BLOB
instead of holding its binary data. instead of holding its character data.
To store Blob JDBC (PreparedStatement) provides To store Clob JDBC (PreparedStatement) provides methods
methods like:setBlob() like:setClob()
•setBinaryStream() •setCharacterStream()
And to retrieve (ResultSet) Blob it provides methods And to retrieve (ResultSet) Clob it provides methods
like:getBlob() like:getClob()
•getBinaryStream •getCharacterStream()