AJAVA
AJAVA
b. What is a PreparedStatement?:
- A PreparedStatement is an interface in Java’s JDBC (Java Database Connectivity) API that is used to
execute parameterized SQL queries against a database. It extends the functionality of a Statement by allo
wing you to create SQL statements with placeholders for parameters. These placeholders are later bound
with specific values using setter methods. PreparedStatement objects are precompiled and cached by th
e database, resulting in better performance, especially when the same query is executed multiple times wi
th different parameter values. Additionally, PreparedStatement helps prevent SQL injection attacks by aut
omatically escaping and sanitizing parameter values.
h. Write a program to connect to a database using MySQL and display all rows of an "employee" table:
java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
try {
Connection conn = DriverManager.getConnection(jdbcUrl, username, password);
Statement statement = conn.createStatement();
String sql = "SELECT * FROM employee";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int empId = resultSet.getInt("empId");
String name = resultSet.getString("name");
String dept = resultSet.getString("dept");
resultSet.close();
statement.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
i. Write a program to display the current date and time using servlets, using annotations in place of a web.
xml file for configuration:
java
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/CurrentDateTimeServlet")
public class CurrentDateTimeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
jsp
<html>
<body>
<form action="Welcome.jsp" method="post">
Enter your name: <input type="text" name="userName">
<input type="submit" value="Submit">
</form>
</body>
</html>
jsp
<html>
<body>
<%
String userName = request.getParameter("userName");
out.println("Welcome, " + userName + "!");
%>
</body>
</html>
l. Write a JSP program to insert roll number, name, and marks of students into a MySQL Database:
- Please specify if you need this code, as it involves more extensive input validation and database intera
ction.
m. Write a program to show how to use PreparedStatement in JDBC, using the "employee" table for this p
rogram:
- The code for using PreparedStatement in JDBC was provided in the previous answers.
n. Explain the life cycle phases of JSP:
- The life cycle phases of a JSP (JavaServer Pages) include:
1. Translation: JSP pages are translated into Java servlets.
2. Compilation: The generated servlets are compiled into bytecode.
3. Initialization: Servlet instances are created and initialized.
4. Request Handling: Servlets handle client requests and generate responses.
5. Destruction: Servlets are eventually destroyed when they are no longer needed.