Nitin Kumar Chandrawanshi
2101330100159
CSE_D
Program 1 :
Write a program to connect to the database and extract data from the Employee table and display the
following :
a.) Details of all the employees
b.) Display details of all employees who are from Banglore
c.) Display details of all employees whose salary is less than 25,000.
Code:
import java.sql.*;
public class EmployeeDetails {
// JDBC URL, username, and password of MySQL server
static final String JDBC_URL = "jdbc:mysql://localhost:3306/your_database_name";
static final String USERNAME = "your_username";
static final String PASSWORD = "your_password";
public static void main(String[] args) {
try {
// Establish a connection to the database
Connection conn = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
Nitin Kumar Chandrawanshi
2101330100159
CSE_D
Statement statement = conn.createStatement();
// a.) Details of all the employees
ResultSet allEmployees = statement.executeQuery("SELECT * FROM Employee");
System.out.println("Details of all employees:");
printResultSet(allEmployees);
// b.) Display details of all employees who are from Bangalore
ResultSet bangaloreEmployees = statement.executeQuery("SELECT * FROM Employee WHERE
city = 'Bangalore'");
System.out.println("\nDetails of employees from Bangalore:");
printResultSet(bangaloreEmployees);
// c.) Display details of all employees whose salary is less than 25,000
ResultSet lowSalaryEmployees = statement.executeQuery("SELECT * FROM Employee WHERE
salary < 25000");
System.out.println("\nDetails of employees with salary less than 25,000:");
printResultSet(lowSalaryEmployees);
// Close the connections
statement.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
Nitin Kumar Chandrawanshi
2101330100159
CSE_D
// Method to print the result set
private static void printResultSet(ResultSet resultSet) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
int columnsNumber = metaData.getColumnCount();
while (resultSet.next()) {
for (int i = 1; i <= columnsNumber; i++) {
System.out.print(resultSet.getString(i) + " ");
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")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
Nitin Kumar Chandrawanshi
2101330100159
CSE_D
// Perform authentication (dummy check)
if ("admin".equals(username) && "admin123".equals(password)) {
// Redirect to WelcomeServlet along with username as parameter
response.sendRedirect("WelcomeServlet?username=" + username);
} else {
response.sendRedirect("login.html"); // Redirect back to login page
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")
public class WelcomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Nitin Kumar Chandrawanshi
2101330100159
CSE_D
String username = request.getParameter("username");
response.setContentType("text/html");
response.getWriter().println("<html><body>");
response.getWriter().println("<h2>Welcome, " + username + "!</h2>");
response.getWriter().println("</body></html>");
login.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h2>Login Page</h2>
<form action="LoginServlet" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
Nitin Kumar Chandrawanshi
2101330100159
CSE_D
<input type="submit" value="Login">
</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 {
// JDBC URL, username, and password of MySQL server
static final String JDBC_URL = "jdbc:mysql://localhost:3306/your_database_name";
static final String USERNAME = "your_username";
static final String PASSWORD = "your_password";
public static void main(String[] args) {
// SQL query to insert a new record into the table
String sqlQuery = "INSERT INTO users (username, password) VALUES (?, ?)";
try (
// Establish a connection to the database
Connection conn = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
// Create a PreparedStatement object for executing SQL queries with parameters
PreparedStatement preparedStatement = conn.prepareStatement(sqlQuery)
){
// Set values for the parameters in the SQL query
preparedStatement.setString(1, "john_doe");
preparedStatement.setString(2, "password123");
// Execute the query to insert the record
int rowsAffected = preparedStatement.executeUpdate();
// Check if the record was successfully inserted
if (rowsAffected > 0) {
Nitin Kumar Chandrawanshi
2101330100159
CSE_D
System.out.println("Record inserted successfully!");
} else {
System.out.println("Failed to insert record.");
} 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 action="CalculatorServlet" method="post">
<input type="text" name="num1" placeholder="Enter first number" required><br><br>
<input type="text" name="num2" placeholder="Enter second number" required><br><br>
<input type="submit" name="operation" value="Add">
<input type="submit" name="operation" value="Subtract">
<input type="submit" name="operation" value="Multiply">
<input type="submit" name="operation" value="Divide">
</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")
public class CalculatorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int num1 = Integer.parseInt(request.getParameter("num1"));
int num2 = Integer.parseInt(request.getParameter("num2"));
String operation = request.getParameter("operation");
int result = 0;
switch (operation) {
case "Add":
result = num1 + num2;
break;
case "Subtract":
result = num1 - num2;
break;
case "Multiply":
result = num1 * num2;
break;
case "Divide":
result = num1 / num2;
break;
Nitin Kumar Chandrawanshi
2101330100159
CSE_D
default:
break;
response.setContentType("text/html");
response.getWriter().println("<html><body>");
response.getWriter().println("<h2>Result: " + result + "</h2>");
response.getWriter().println("</body></html>");
Output: