Advanced Java Programming - Unit 1 (Detailed Notes)
JDBC (Java Database Connectivity)
---------------------------------
JDBC is a Java API that helps Java applications interact with databases. It allows Java programs to send
SQL commands like INSERT, SELECT, UPDATE, and DELETE.
Steps to connect Java with a database using JDBC:
1. Import the JDBC package.
2. Load the JDBC driver (example: [Link]("[Link]")).
3. Open a connection using DriverManager.
4. Create a Statement or PreparedStatement.
5. Execute SQL commands.
6. Process the result.
7. Close the connection.
Types of JDBC Drivers:
1. Type 1: JDBC-ODBC Bridge - connects through ODBC driver.
2. Type 2: Native API Driver - uses native DB libraries.
3. Type 3: Network Protocol Driver - uses middleware server.
4. Type 4: Thin Driver - 100% Java-based driver, directly connects to DB.
Statements in JDBC:
- Statement: For static SQL.
- PreparedStatement: For dynamic SQL, supports parameters.
- CallableStatement: Used to call stored procedures.
PreparedStatement Example:
PreparedStatement stmt = [Link]("INSERT INTO Emp VALUES (?, ?)");
[Link](1, 101);
[Link](2, "Ratan");
[Link]();
Transaction Management
-----------------------
A transaction is a set of database operations that must be executed together to maintain data accuracy.
ACID Properties:
- Atomicity: All operations succeed or fail together.
- Consistency: Maintains database integrity.
- Isolation: Transactions work independently.
- Durability: Once committed, changes are permanent.
To handle transactions:
[Link](false);
[Link](); // or [Link]();
Stored Procedures
-----------------
A stored procedure is a group of pre-compiled SQL statements stored in the database.
To call them from Java:
CallableStatement stmt = [Link]("{call insertR(?,?)}");
Servlets (Server-side Java Programs)
------------------------------------
Servlets handle web requests on the server side. They are Java classes that generate dynamic content.
Servlet Lifecycle:
1. init(): Initializes servlet.
2. service(): Handles client requests (GET/POST).
3. destroy(): Called before servlet is destroyed.
Types of Servlet Classes:
- GenericServlet: Protocol-independent.
- HttpServlet: Handles HTTP-specific tasks like GET/POST.
Handling HTTP Requests:
- doGet(): Handles data via URL (not secure).
- doPost(): Handles data in request body (secure).
Example: doPost()
public void doPost(HttpServletRequest request, HttpServletResponse response) {
String name = [Link]("name");
PrintWriter out = [Link]();
[Link]("Hello " + name);
}
Session Tracking
----------------
HTTP is stateless, so we use sessions to remember users.
Techniques:
1. Cookies - Small pieces of data stored on the client.
2. HttpSession - Maintains session data on server.
3. URL Rewriting - Passes session info in URL.
Example for HttpSession:
HttpSession session = [Link]();
[Link]("username", "John");
Events and Listeners in Servlet
-------------------------------
Servlets support event handling using listeners. These help track things like user logins or context
initialization.
Common Listener Interfaces:
- ServletContextListener
- HttpSessionListener
- ServletRequestListener
Example:
ServletContextListener can initialize a DB connection when app starts.