Advanced JAVA: April 2023
Q1) Attempt any EIGHT of the following :
b) What is IP Address?
An IP (Internet Protocol) Address is a unique numerical label assigned to each device connected to a
network. It is used for identifying and locating devices on a network.
ypes of IP Addresses
1. IPv4 (Internet Protocol version 4) – 32-bit address (e.g., [Link])
2. IPv6 (Internet Protocol version 6)
import [Link];
public class IPAddressExample
public static void main(String[] args)
Try
InetAddress myIP = [Link]();
[Link]("My IP Address: " + [Link]());
} catch (Exception e)
[Link]("Error: " + [Link]());
c) What is JDBC
d) What is servlet
A Servlet is a Java program that runs on a web server and handles client requests to generate
dynamic web content
Life Cycle of a Servlet
A servlet goes through the following stages:
1. Loading & Instantiation – The servlet class is loaded when the server starts or on
the first request.
2. Initialization (init()) – Called once when the servlet is created.
3. Request Handling (service()) – Called for every client request.
4. Destruction (destroy()) – Called before the servlet is removed from memory.
Program
g) What is thread
A thread in Java is the smallest unit of a process that runs independently. It allows multiple
tasks to be executed concurrently within a program, enabling multitasking.
Java provides multithreading, which means running multiple threads simultaneously to
improve performance.
Creating a Thread in Java
There are two ways to create a thread:
1. Extending Thread Class
class MyThread extends Thread {
public void run() {
[Link]("Thread is running...");
public static void main(String[] args) {
MyThread t1 = new MyThread();
[Link](); // Start the thread
1. Implementing Runnable Interface
class MyRunnable implements Runnable {
public void run() {
[Link]("Thread is running...");
}
public static void main(String[] args) {
Thread t1 = new Thread(new MyRunnable());
[Link](); // Start the thread
}
}
h) What is socket
A socket in Java is an endpoint for communication between two machines over a network. It
allows data to be sent and received between a client and a server using TCP/IP or UDP
protocols.
Program
j) What is networking?
Networking in Java allows communication between two or more computers over a network using
Java's built-in networking API. Java provides various classes and interfaces in the [Link] package
to enable network communication.
Key Concepts in Java Networking
1. IP Address – Unique identifier for a device on a network.
2. Port Number – A numerical value used to identify specific processes.
3. Socket – Endpoint for communication between two devices.
4. Protocol – A set of rules for communication (e.g., TCP/IP, UDP, HTTP).
i) j) List the directives in JSP.
1. page Directive
Defines global settings for the JSP page, such as importing packages, defining error
pages, and setting session control.
<%@ page attribute="value" %>
[Link] Directive
Includes the content of another file at compile time.
Used for reusing headers, footers, or common elements
<%@ include file="[Link]" %>
3. taglib Directive
Used to import custom JSP tags or Java Standard Tag Library (JSTL).
<%@ taglib uri="URI" prefix="prefix" %>
Q2) Attempt any FOUR of the following :
[Link] & explain all the interfaces used in JDBC
JDBC provides several interfaces in the [Link] package to interact with relational
databases. These interfaces help in establishing connections, executing queries, and
retrieving results efficiently.
[Link] Interface
The entry point for JDBC. It provides methods for loading and registering database
drivers.
Implemented by database vendors (e.g., [Link]).
import [Link];
public class LoadDriver {
public static void main(String[] args) {
try {
[Link]("[Link]"); // Load MySQL Driver
[Link]("Driver Loaded Successfully");
} catch (Exception e) {
[Link]();
}
}
}
2. Connection Interface
Represents a connection to a database.
Used to send queries and manage transactions.
Method Description
createStatement() Creates a Statement object.
prepareStatement() Creates a PreparedStatement object.
setAutoCommit(boolean) Enables/disables auto-commit.
commit() Commits a transaction.
rollback() Rolls back a transaction.
close() Closes the connection.
3. PreparedStatement Interface
Used for executing parameterized queries (more secure & efficient).
Precompiles the SQL query for better performance.
Prevents SQL Injection.
PreparedStatement pstmt = [Link]("SELECT * FROM students WHERE id
= ?");
[Link](1, 101); // Set parameter
ResultSet rs = [Link]();
[Link]