Advanced Java – 8 Marks Questions and Answers
Explain the steps to connect a Java application to a database using JDBC with example.
Steps:
1. Load Driver Class: [Link]("[Link]");
2. Establish Connection: Connection con = [Link](url, user, pass);
3. Create Statement: Statement st = [Link]();
4. Execute Query: ResultSet rs = [Link]("SELECT * FROM table");
5. Process Result: Loop through rs to fetch data.
6. Close Connection: [Link]();
Example:
[Link]("[Link]");
Connection con = [Link]("jdbc:mysql://localhost:3306/db","root","pass");
Statement st = [Link]();
ResultSet rs = [Link]("SELECT * FROM student");
while([Link]()) {
[Link]([Link](1)+" "+[Link](2));
}
[Link]();
Explain the difference between Statement, PreparedStatement, and CallableStatement.
Statement: For simple queries without parameters.
PreparedStatement: Precompiled, used for queries with parameters, prevents SQL injection.
CallableStatement: Used for calling stored procedures in DB.
What is transaction management in JDBC? Explain with example.
Transaction management ensures data integrity using commit() and rollback().
Example:
[Link](false);
PreparedStatement ps = [Link]("INSERT INTO account VALUES(?,?)");
[Link](1, 101); [Link](2, 5000); [Link]();
[Link](); // or [Link]();
Explain the life cycle of a servlet with methods.
init() – Called once when servlet is loaded.
service() – Handles requests (doGet(), doPost()).
destroy() – Called when servlet is removed.
Lifecycle managed by Servlet Container (Tomcat).
Explain session tracking techniques in servlets.
Cookies: Stores info on client side.
URL Rewriting: Appends session ID to URL.
Hidden Form Fields: Stores data in hidden fields of HTML forms.
HttpSession: Built-in object to maintain session.
Explain JSP life cycle in detail.
Translation: JSP → Servlet
Compilation: Servlet → Class file
Initialization: jspInit()
Execution: _jspService() handles requests
Cleanup: jspDestroy()
Explain JSP implicit objects with example.
request, response, session, application, out, config, pageContext, page, exception.
Example:
<%= [Link]("name") %>
Explain the architecture and steps of RMI.
Architecture: Client, Server, RMI Registry, Stub, Skeleton.
Steps:
1. Create remote interface.
2. Implement interface.
3. Generate stub & skeleton.
4. Start RMI registry.
5. Run server & client.
Explain types of EJB with examples.
Session Beans: Business logic for client.
Entity Beans: Represents database entity.
Message-Driven Beans: Handles asynchronous messages.
Explain benefits of EJB in enterprise applications.
Simplifies development
Built-in transaction management
Security
Scalability