Nitin - 2101330100159 - CSE D
Nitin - 2101330100159 - CSE D
2101330100159
CSE_D
Program 1 :
Write a program to connect to the database and extract data from the Employee table and display the
following :
c.) Display details of all employees whose salary is less than 25,000.
Code:
import java.sql.*;
try {
printResultSet(allEmployees);
printResultSet(bangaloreEmployees);
// c.) Display details of all employees whose salary is less than 25,000
printResultSet(lowSalaryEmployees);
statement.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
Nitin Kumar Chandrawanshi
2101330100159
CSE_D
while (resultSet.next()) {
System.out.println();
Output:
Nitin Kumar Chandrawanshi
2101330100159
CSE_D
Program 2 :
Write Servlet program to create a login page having username and password and fetch the details by
using the sendRedirect() concept from one servlet to other servlet.
Code:
LoginServlet.java:
import java.io.IOException;
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("/LoginServlet")
response.sendRedirect("WelcomeServlet?username=" + username);
} else {
WelcomeServlet.java:
import java.io.IOException;
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("/WelcomeServlet")
response.setContentType("text/html");
response.getWriter().println("<html><body>");
response.getWriter().println("</body></html>");
login.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h2>Login Page</h2>
<label for="username">Username:</label>
<label for="password">Password:</label>
</form>
</body>
</html>
Output:
Program 3 :
Write a Program to insert a record in database having two fields username and password using JDBC
with the help of PreparedStatement interface and display record inserted message in java application.
Code:
import java.sql.*;
Nitin Kumar Chandrawanshi
2101330100159
CSE_D
public class InsertRecordExample {
String sqlQuery = "INSERT INTO users (username, password) VALUES (?, ?)";
try (
){
preparedStatement.setString(1, "john_doe");
preparedStatement.setString(2, "password123");
if (rowsAffected > 0) {
Nitin Kumar Chandrawanshi
2101330100159
CSE_D
System.out.println("Record inserted successfully!");
} else {
} catch (SQLException e) {
e.printStackTrace();
Output:
Program 4 :
Create an application displaying the arithmetic operations as in calculator by using the concept of
servlets and html.
Code:
index.html:
<!DOCTYPE html>
Nitin Kumar Chandrawanshi
2101330100159
CSE_D
<html>
<head>
<meta charset="UTF-8">
<title>Simple Calculator</title>
</head>
<body>
<h2>Simple Calculator</h2>
</form>
</body>
</html>
CalculatorServlet.java:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Nitin Kumar Chandrawanshi
2101330100159
CSE_D
@WebServlet("/CalculatorServlet")
int result = 0;
switch (operation) {
case "Add":
break;
case "Subtract":
break;
case "Multiply":
break;
case "Divide":
break;
Nitin Kumar Chandrawanshi
2101330100159
CSE_D
default:
break;
response.setContentType("text/html");
response.getWriter().println("<html><body>");
response.getWriter().println("</body></html>");
Output: