Advanced Java Programming - 1st
Assessment
Name: Sanyam Choudhary
Roll No: 48
Branch: CSER
Section: A
Workshop Lab Number: 001
Question 1:
Differentiate between Statement and PreparedStatement with the help of JDBC program.
JDBC Program:
import java.sql.*;
public class StatementVsPrepared {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "root";
String password = "password";
try (Connection conn = DriverManager.getConnection(url, user, password)) {
Statement stmt = conn.createStatement();
String sql1 = "INSERT INTO students (id, name) VALUES (1, 'John')";
stmt.executeUpdate(sql1);
System.out.println("Inserted using Statement.");
String sql2 = "INSERT INTO students (id, name) VALUES (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql2);
pstmt.setInt(1, 2);
pstmt.setString(2, "Alice");
pstmt.executeUpdate();
System.out.println("Inserted using PreparedStatement.");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Question 2:
Describe stored procedures in CallableStatement and create Java program to fetch data
using ResultSet.
JDBC Program:
import java.sql.*;
public class CallableAndResultSetExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "root";
String password = "password";
try (Connection conn = DriverManager.getConnection(url, user, password)) {
CallableStatement cs = conn.prepareCall("{call getStudents()}");
ResultSet rs = cs.executeQuery();
System.out.println("ID | Name");
while (rs.next()) {
System.out.println(rs.getInt("id") + " | " + rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Question 3:
Explain the lifecycle of servlet program and create a servlet that communicates with login
form.
HTML (login.html):
<!DOCTYPE html>
<html>
<body>
<form action="LoginServlet" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
Servlet (LoginServlet.java):
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
out.println("<h2>Welcome " + username + "</h2>");
}
}
web.xml Configuration:
<web-app>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
Question 4:
Explain RequestDispatcher and Cookies. Create JSP program to calculate factorial values.
HTML Form (input.html):
<!DOCTYPE html>
<html>
<body>
<form action="factorial.jsp" method="post">
Enter number: <input type="text" name="number">
<input type="submit" value="Calculate">
</form>
</body>
</html>
JSP File (factorial.jsp):
<%@ page import="java.io.*" %>
<%
int num = Integer.parseInt(request.getParameter("number"));
int fact = 1;
for(int i = 1; i <= num; i++) {
fact *= i;
}
out.println("Factorial of " + num + " is " + fact);
%>