Advance Java Answers
Advance Java Answers
Advance Java Answers
1. JDBC stands for Java Database Connectivity. It is a Java API that allows Java programs to interact
with databases. JDBC enables developers to write database applications in Java without having to
deal with the low-level details of database connectivity.
Components of JDBC:
- DriverManager: Manages a list of database drivers. It can be used to establish a connection to the
database.
- Driver: Provides the implementation for the JDBC API. Different database vendors provide their own
JDBC drivers.
- Statement: Used to execute SQL queries and statements against the database.
- ResultSet: Represents the result set of a SQL query. It allows the retrieval and manipulation of data
returned by a SELECT statement.
2. Types of drivers:
- Platform independence: These drivers are written purely in Java and do not rely on native code,
making them platform-independent.
- Performance: Since they directly communicate with the database server, they can offer better
performance compared to other drivers.
- Security: They can be more secure as they do not rely on external libraries or components.
- No client-side configuration: They do not require additional client-side configuration as they are
self-contained within the Java application.
- Performance: They might not offer the same level of performance as native protocol pure Java
drivers.
- Installation and configuration: They require additional setup and configuration on the client-side.
- Limited to specific databases: Each native API driver is typically designed for a specific database
vendor, limiting their usability across different databases.
- Statement
- PreparedStatement
- CallableStatement
- `createStatement()`: This method is used to create a Statement object for sending SQL statements
to the database.
- `executeQuery()`: This method is used to execute a SQL SELECT statement and return the ResultSet
object representing the result set of the query.
- `TYPE_SCROLL_SENSITIVE` is a constant that indicates that the ResultSet object is scrollable and
sensitive to changes made by others to the database. This means that if another process modifies
the data in the database, those changes will be reflected in the ResultSet.
8. Metadata:
Metadata refers to data that describes other data. In the context of databases and JDBC, metadata
provides information about the structure and properties of database objects such as tables, columns,
and constraints.
- Database metadata: Provides information about the database as a whole, such as its name, version,
and supported features.
- Result set metadata: Provides information about the structure of the result set returned by a query,
such as column names, types, and sizes.
- Parameter metadata: Provides information about the parameters of a PreparedStatement object,
such as parameter types and modes.
9. Methods of ResultSetMetaData:
- `getColumnLabel(int column)`: Returns the label for the specified column, if a label is specified for
the column in the SQL query; otherwise, it returns the column name.
10. Multithreading:
- Creation: Threads are created using either extending the Thread class or implementing the
Runnable interface.
- Sleeping or waiting: Threads can be made to sleep or wait for a specified period of time.
- Termination: Threads terminate either by completing their tasks or by being explicitly stopped.
- Implementing the `Runnable` interface and providing the implementation for the `run()` method.
The main thread is the thread from which a Java program starts its execution. It is created
automatically when a Java program begins execution and serves as the entry point for the program.
All other threads in the program are spawned from the main thread or from threads created by it.
- Port numbers: Port numbers are used to uniquely identify different communication endpoints
within a single host. They range from 0 to 65535, with well-known ports (0-1023) reserved for
specific services and applications.
A proxy server is an intermediary server between a client and the internet. It acts as a gateway,
forwarding requests from clients to other servers and caching responses to improve performance
and security. The purpose of a proxy server includes caching frequently accessed resources,
controlling access to the internet, and enhancing privacy by hiding the client's IP address.
- Socket class: Represents a client-side endpoint for communication over a network. It can establish a
connection to a server and send and receive data.
- ServerSocket class: Represents a server-side endpoint that listens for incoming connections from
clients. It can accept incoming connections and create Socket objects to handle communication with
clients.
The format of a URL (Uniform Resource Locator) in networking typically consists of the following
components:
```
protocol://hostname:port/path?query
```
- Protocol: Specifies the communication protocol to be used, such as HTTP, FTP, or JDBC.
- Port: Specifies the port number on the server where the service is running.
The DatagramSocket class in Java is used to implement networking applications based on the UDP
protocol. It represents a socket for sending and receiving datagram packets, which are individual
units of data transmitted over a network.
- `getInetAddress()`: Returns the InetAddress object representing the IP address of the remote
endpoint of the socket.
- `getPort()`: Returns the port number of the remote endpoint of the socket.
classes and interfaces for networking in Java. It provides support for networking operations such as
socket programming, URL handling, and protocol implementation.
4 MARKS QUESTIONS
The JDBC-ODBC bridge driver is a type 1 JDBC driver that enables Java applications to interact with
databases using ODBC (Open Database Connectivity) drivers. It acts as a mediator between Java
applications and ODBC-compliant databases, allowing Java programs to access data stored in
databases via SQL queries.
The JDBC Net Pure Java driver, also known as the type 4 driver, is a JDBC driver that communicates
directly with the database server using a database-specific protocol. Unlike the JDBC-ODBC bridge
driver, it does not rely on ODBC, making it purely Java-based and platform-independent. This driver is
faster and more efficient compared to the JDBC-ODBC bridge driver.
3. **Advantages and Disadvantages of Multithreading:**
Advantages:
- Simplified program design by breaking complex tasks into smaller, manageable threads.
Disadvantages:
- Complexity in programming and debugging due to concurrency issues like race conditions and
deadlocks.
4. **JDBC Process:**
The JDBC (Java Database Connectivity) process involves the following steps:
- Loading the JDBC driver: Load the appropriate JDBC driver using `Class.forName()` method.
- Creating a statement: Create a statement object for executing SQL queries against the database.
- Executing queries: Execute SQL queries using the statement object (`Statement` or
`PreparedStatement`).
- Handling results: Process the results obtained from the database (if any).
- Closing resources: Close the connection and other JDBC resources to release database and system
resources.
5. **PreparedStatement():**
Example:
```java
String query = "INSERT INTO employees (id, name, age) VALUES (?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(query);
```
6. **CallableStatement:**
`CallableStatement` is a type of JDBC statement used to call stored procedures in the database. It
extends `PreparedStatement` and provides methods to execute stored procedures with input and
output parameters.
Please note that this question is repeated. Refer to the answer given in question 3.
- New: The thread is in the new state if it has been created but not yet started.
- Runnable: The thread is in the runnable state if it's eligible to run but the scheduler has not
selected it to be the running thread.
- Running: The thread is in the running state if it's currently executing its code.
- Blocked: The thread is in the blocked state if it's waiting for a monitor lock to enter a synchronized
block/method.
- Waiting: The thread is in the waiting state if it's waiting indefinitely for another thread to perform
a particular action.
- Terminated: The thread is in the terminated state if it has exited its run method and has stopped.
9. **Thread Priorities:**
Thread priorities in Java range from 1 to 10, where 1 is the lowest priority and 10 is the highest.
Thread priorities are used by the thread scheduler to determine the order in which threads are
executed. Higher-priority threads are given preference over lower-priority threads.
Example:
```java
```
Listening for UDP packets involves creating a `DatagramSocket` and using it to receive incoming
UDP packets. Sending UDP packets involves creating a `DatagramSocket`, constructing a
`DatagramPacket` containing the data to be sent, and then sending it using the `send()` method of
`DatagramSocket`.
The client-server model is a distributed computing architecture where client devices (e.g.,
computers, smartphones) request services or resources from server devices (e.g., servers, databases)
over a network. Clients initiate requests, and servers respond to these requests by providing services
or resources.
URL programming in Java involves manipulating URLs (Uniform Resource Locators) to perform
various networking tasks such as opening connections to remote servers, downloading resources, or
parsing URL components like protocol, host, port, path, etc. Java provides the `java.net.URL` class for
working with URLs.
Thread.currentThread().setName("Main Thread");
```
```java
long factorial = 1;
factorial *= i;
try {
} catch (InterruptedException e) {
e.printStackTrace();
```
19. **Java Program to Display Date and Time of Server Machine on Client Machine:**
20. **Java Program to Delete Details of Students Whose Initial Character of Their Name is 'S':**