Adv Java
Adv Java
• java.sql
• javax.sql
• The yield() method pauses the current thread and allows other threads of the same
priority to execute. It’s a way to give up CPU voluntarily.
c) What is JDBC?
• JDBC (Java Database Connectivity) is an API that allows Java programs to connect
and interact with databases using SQL.
• A scriptlet tag in JSP is <% %> and it is used to write Java code within HTML.
• wait() pauses the thread until another thread invokes notify() or notifyAll() on
the same object. These methods are used for inter-thread communication.
f) What is Networking?
h) What is ORM?
i) What is Session?
1
j) What is the role of Prepared Statement?
• createStatement()
• prepareStatement()
• setPriority(int newPriority)
• Socket
• ServerSocket
e) What is IP address?
h) What is JSP?
• JSP (JavaServer Pages) is a technology used to create dynamic web content using
HTML and embedded Java code.
2
i) What is Hibernate?
• Hibernate is a Java ORM framework used for mapping Java objects to database
tables.
• TYPE_FORWARD_ONLY
• TYPE_SCROLL_INSENSITIVE
• TYPE_SCROLL_SENSITIVE
b) What is IP Address?
c) What is JDBC?
• JDBC is an API for Java that allows the connection and execution of queries with
databases.
d) What is servlet?
• A servlet is a Java class used to handle requests and responses in a web application.
e) What is cookies?
• Cookies are small pieces of data stored on the client’s browser used to maintain
session information.
f) What is UDP?
• UDP (User Datagram Protocol) is a connectionless, fast transport layer protocol used
in networking.
g) What is thread?
3
h) What is socket?
j) What is networking?
✅ Q2 – Question Paper 1
Answer:
The servlet lifecycle is managed by the servlet container and involves:
Answer:
Statement PreparedStatement
Answer:
It allows synchronized threads to communicate using wait(), notify(), and notifyAll().
4
• wait() – thread waits.
• notify() – wakes one waiting thread.
• notifyAll() – wakes all waiting threads.
Answer:
JSP architecture follows MVC:
e) Write a JDBC Program to Insert the record into patient table (Use
prepared statement).
Answer:
java
CopyEdit
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital", "root",
"pass");
PreparedStatement pst = con.prepareStatement("INSERT INTO patient VALUES
(?, ?, ?)");
pst.setInt(1, 101);
pst.setString(2, "John");
pst.setInt(3, 45);
pst.executeUpdate();
System.out.println("Record Inserted");
✅ Q2 – Question Paper 2
Answer:
Statement is used to execute SQL queries.
Types:
5
b) Explain thread life cycle with diagram.
Answer:
Thread Lifecycle states:
Answer:
Answer:
doGet() doPost()
Answer:
java
CopyEdit
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "root",
"pass");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM student");
6
if(rs.next()) {
System.out.println("Total Records: " + rs.getInt(1));
}
✅ Q2 – Question Paper 3
Answer:
Answer:
HTTPServlet GenericServlet
Answer:
JSP lifecycle stages:
7
d) What is synchronization? Explain.
Answer:
Synchronization prevents multiple threads from accessing shared resources at the same time. It uses
the synchronized keyword.
Example:
java
CopyEdit
synchronized void printData() {
// code
}
1.
Diagram:
pgsql
Copy code
Java App
|
| (JDBC API)
↓
JDBC Driver Manager
↓
JDBC Driver (Type 1 / 2 / 3 / 4)
↓
Database
8
b) Use of InetAddress class with example
Common Methods:
Example:
java
Copy code
import java.net.*;
9
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/college", "root",
"password");
<%
String str = request.getParameter("n");
if(str != null){
int n = Integer.parseInt(str);
10
2- Q3: [4 × 4 = 16]
Diagram:
pgsql
CopyEdit
Application
|
SessionFactory
|
Session
| \
Transaction Query
|
Database
ResultSet is an interface in JDBC used to hold data retrieved from the database.
Important methods:
11
Diagram:
scss
CopyEdit
JSP Page
↓
Translation
↓
Compilation
↓
Servlet Class
↓
Initialization (jspInit)
↓
Request Handling (jspService)
↓
Destroy (jspDestroy)
12
-3 Q3: [4 × 4 = 16]
• Default: 5 (NORM_PRIORITY)
• Use setPriority(int) and getPriority().
• Higher priority threads are scheduled first (but not guaranteed).
13
try {
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/yourDB", "root",
"password");
Statement stmt = con.createStatement();
String sql = "ALTER TABLE Emp DROP COLUMN salary";
stmt.executeUpdate(sql);
System.out.println("Salary column deleted.");
} catch (Exception e) {
System.out.println(e);
}
}
}
java
CopyEdit
Class.forName("com.mysql.cj.jdbc.Driver");
2. Create connection:
java
CopyEdit
Connection con = DriverManager.getConnection(url, user, password);
3. Create statement:
java
CopyEdit
Statement stmt = con.createStatement();
4. Execute query:
java
CopyEdit
ResultSet rs = stmt.executeQuery("SELECT * FROM tablename");
5. Close connection:
java
CopyEdit
con.close();
14
c) doGet() vs doPost() methods:
doGet() doPost()
out.println("<table
border='1'><tr><th>ENO</th><th>ENAME</th><th>SAL</th><th>DESIGN</th></tr>")
;
while(rs.next()) {
out.println("<tr><td>" + rs.getInt("eno") + "</td><td>" +
rs.getString("ename") +
"</td><td>" + rs.getFloat("sal") + "</td><td>"
+ rs.getString("design") + "</td></tr>");
15
}
out.println("</table>");
} catch(Exception e) {
out.println("Error: " + e);
}
}
}
b) Cookies:
c) ResultSet Interface:
Techniques:
1. Cookies
2. Hidden Form Fields
3. URL Rewriting
4. HttpSession
16
c) Thread Priority:
java
CopyEdit
thread.setPriority(Thread.MAX_PRIORITY);
• Part of JDBC.
• Used to establish connection with database using:
java
CopyEdit
Connection con = DriverManager.getConnection(url, user, pass);
17
b) Runnable Interface:
java
CopyEdit
class MyThread implements Runnable {
public void run() {
// code
}
}
c) Thread Synchronization:
Q3.
Answer: Hibernate is an ORM (Object Relational Mapping) framework that maps Java classes to
database tables.
Hibernate Architecture includes:
18
6. Persistent Objects – Java classes mapped to database tables.
Diagram:
pgsql
CopyEdit
Application
|
Configuration --> SessionFactory
|
Session --> Transaction
|
Database
Answer: Servlet lifecycle is managed by the servlet container and includes the following phases:
Diagram:
rust
CopyEdit
Client Request
|
[Web Container]
|
-> init()
-> service()
-> destroy()
Advantages:
Example:
java
CopyEdit
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
19
}
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
}
}
e) Write a java servlet program to accept name from user & display on browser [use HTML].
HTML Form:
html
CopyEdit
<form action="NameServlet" method="post">
Enter Name: <input type="text" name="username">
<input type="submit" value="Submit">
</form>
Servlet Code:
java
CopyEdit
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
• wait() – Causes the current thread to wait until another thread invokes notify() or
notifyAll().
• notify() – Wakes up a single thread that is waiting on the object's monitor.
• notifyAll() – Wakes up all threads that are waiting on the object's monitor.
Example:
java
CopyEdit
synchronized(obj) {
obj.wait(); // waits
20
obj.notify(); // notifies one
obj.notifyAll(); // notifies all
}
b) Applications of Spring.
Answer: Spring is a powerful Java framework used for building enterprise-level applications.
Applications:
c) Connection Interface.
Answer: Connection interface in Java SQL package represents a connection (session) with a
database.
Important methods:
• createStatement()
• prepareStatement()
• commit()
• rollback()
• close()
Example:
java
CopyEdit
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
21