Advanced Java Notes
Advanced Java Notes
1
2. Set layout if needed.
3. Add components to the panel.
4. Add the panel to the frame.
Example:
JFrame frame = new JFrame("Panel Example");
JPanel panel = new JPanel();
JButton button = new JButton("Click Me");
panel.add(button);
frame.add(panel);
frame.setSize(300, 200);
frame.setVisible(true);
2
4. Define Container list any 4 type of container
Answer:
A container is a class in Swing that is used to hold and manage GUI
components like buttons, text fields, labels, and even other containers.
Containers help in organizing the layout of components on the screen.
Four types of commonly used containers are:
1. JFrame – Main window for a GUI application.
2. JPanel – Used to group components inside a frame.
3. JDialog – Popup window for messages or inputs.
4. JScrollPane – Provides scrolling for large components.
3
7. FocusListener – Handles focus gained or lost events.
8. TextListener – Handles text changes in text components.
4
8. Name method of MouseListener with syntax
Answer:
Methods of MouseListener:
1. public void mouseClicked(MouseEvent e)
– Called when a mouse button is clicked.
2. public void mousePressed(MouseEvent e)
– Called when a mouse button is pressed.
3. public void mouseReleased(MouseEvent e)
– Called when a mouse button is released.
4. public void mouseEntered(MouseEvent e)
– Called when the mouse enters a component.
5. public void mouseExited(MouseEvent e)
– Called when the mouse leaves a component.
Syntax Example:
import java.awt.event.*;
5
}
public void mouseExited(MouseEvent e) {
System.out.println("Mouse Exited");
}
}
6
10. Name method of ItemListener , ActionListener with syntax
Answer:
ItemListener:
ItemListener is used to handle item selection events, such as when a checkbox
or choice is selected or deselected.
• Method:
public void itemStateChanged(ItemEvent e)
ActionListener:
ActionListener is used to handle action events, such as button clicks.
• Method:
public void actionPerformed(ActionEvent e)
7
12. Name the package which is used to design AWT control and swing.
Answer:
To design AWT and Swing controls in Java, two different packages are used:
java.awt – This package is used to create AWT (Abstract Window Toolkit)
controls like buttons, labels, text fields, checkboxes, etc.
javax.swing – This package is used to create Swing controls, which are
advanced and flexible GUI components like JButton, JLabel, JTextField, etc.
Swing is built on top of AWT and provides more features and a better look.
8
4 Marks
3. Explain feature of swing.
Answer:
Swing is a part of Java Foundation Classes (JFC) used to create Graphical
User Interface (GUI) applications. It is more powerful and flexible than AWT.
Swing is available in the javax.swing package.
Features of Swing:
1. Lightweight – Swing components do not depend on the operating
system, so they are more flexible and portable.
2. Platform Independent Look and Feel – Swing allows the GUI to look
the same on all platforms or take the look of the current OS.
3. Pluggable Look and Feel – Developers can change the appearance of
components without changing their code.
4. Rich Set of Components – Swing provides advanced components like
JTable, JTree, JTabbedPane, etc.
5. MVC Architecture – Swing follows Model-View-Controller for better
separation of data and UI.
6. Customizable – Swing components can be easily customized.
9
In this section, we will discuss event processing and how to implement the
delegation event model in Java. We will also discuss the different components
of an Event Model.
10
18. Differences between SWING and AWT.
Asnwer:
Point AWT Swing
11
UNIT 5 – BASICS OF NETWORK PROGRAMMING
2 Marks
1. What is networking? Write its advantage and disadvantage
Answer:
Networking is the process of connecting two or more computers to share
data, files, and resources like printers or internet. It is used in communication
systems, the internet, and client-server models.
Advantages:
• Easy data sharing
• Centralized data management
• Cost-effective resource sharing
Disadvantages:
• Security risks
• Virus and malware spread easily
• Expensive setup for large networks
12
3. Write the use of getByName() and getAllByName() method
Answer:
• getByName(String host): Returns the IP address of the given host.
• getAllByName(String host): Returns all IP addresses associated with the
host name.
Both methods are from the InetAddress class.
13
4 Marks
3. Write the difference between SeverSocket and DatagramPacket
Answer:
Point ServerSocket DatagramPacket
14
• getFile() – returns the file name.
• openStream() – opens a stream to read content.
Example:
import java.net.*;
15
6. Explain URL class Constructor (any 4)
Answer:
The URL class constructors are used to create URL objects with different
inputs. These are present in the java.net package.
Common constructors of URL class:
1. URL(String spec)
– Creates a URL from a full string.
Example: new URL("https://www.google.com")
2. URL(String protocol, String host, String file)
– Creates a URL with protocol, host, and file path.
Example: new URL("https", "www.google.com", "/search")
3. URL(String protocol, String host, int port, String file)
– Same as above but includes a port number.
Example: new URL("http", "localhost", 8080, "/index.html")
4. URL(URL context, String spec)
– Creates a URL using a base URL and relative path.
Example: new URL(baseURL, "/page.html")
16
3. getLocalHost()
– Returns the IP address of the local machine.
Example: InetAddress local = InetAddress.getLocalHost();
These methods help in DNS lookup and network communication.
17
UNIT 6 – INTERACTING WITH DATABASE
2 Marks
1. Name the Types of drivers for database connectivity.
Answer:
There are 4 types of JDBC drivers for database connectivity in Java:
1. Type-1: JDBC-ODBC Bridge driver
2. Type-2: Native-API driver
3. Type-3: Network Protocol driver
4. Type-4: Thin driver (Pure Java driver)
18
4. Write Advantages and Disadvantages of Statements and
PreparedStatement
Answer:
Point Statement PreparedStatement
Query Type Used for static queries Used for dynamic queries
Performance Slower for repeated use Faster due to pre-
compilation
Security Prone to SQL injection Safe from SQL injection
19
Parameter Does not support Supports placeholders (?)
Support parameters
20
4 Marks
5. Explain Types of ResultSet
Answer:
The ResultSet in JDBC is an object that holds data retrieved from a database
after executing a SQL query. It is part of the java.sql package.
There are three main types of ResultSet based on movement and updates:
1. TYPE_FORWARD_ONLY
o Cursor moves only forward.
o This is the default type.
o Fast and memory efficient.
2. TYPE_SCROLL_INSENSITIVE
o Cursor can move forward and backward.
o Data is not updated if the database changes after fetching.
3. TYPE_SCROLL_SENSITIVE
o Cursor can move in any direction.
o Data is updated if the database changes after fetching.
Syntax to create a scrollable ResultSet:
Statement stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
These types help control how data is read and navigated in result sets.
21
1. next()
o Moves the cursor to the next row.
o Returns false if there are no more rows.
2. getString(int columnIndex) / getString(String columnName)
o Returns the value of the specified column as a String.
3. getInt(int columnIndex) / getInt(String columnName)
o Returns the value of the specified column as an integer.
4. previous()
o Moves the cursor to the previous row (only in scrollable
ResultSet).
5. first() / last()
o Moves the cursor to the first or last row (scrollable ResultSet).
6. absolute(int row)
o Moves the cursor to the given row number.
7. close()
o Closes the ResultSet to free up resources.
These methods help in navigating and accessing the data row by row.
22
2. JDBC Driver Layer
o Converts Java calls into database-specific calls.
o Uses one of the four JDBC driver types to interact with the
database.
Steps in JDBC Communication:
• Java Application → JDBC API → JDBC Driver → Database
• Database → JDBC Driver → JDBC API → Java Application
23
1. The Java application uses JDBC to send SQL queries.
2. The JDBC driver converts these queries into database-specific
commands.
3. The Database server processes the request and sends back the result.
4. The result is shown to the user through the Java application.
Main Components:
• Client (Java Application)
• JDBC API
• JDBC Driver
• Database Server
Features:
• Simple and fast communication.
• Best for small-scale applications.
• Less secure for large systems as direct access to DB is given.
24
o Platform-dependent.
o Better performance than Type-1.
3. Type-3: Network Protocol Driver
o Sends JDBC calls to a middleware server, which then
communicates with the database.
o Flexible, suitable for enterprise apps.
o Requires network configuration.
4. Type-4: Thin Driver
o Pure Java driver, directly connects to the database.
o Best performance and platform-independent.
o Commonly used in modern applications.
25