Lab 10: Install a database ( MySql or Oracle ) .
Create a table which should
contain at least the following fields: name, password, email-id, phone-
number. Write a java program/servlet/JSP to connect to that database and
extract data from the tables and display them . Insert the details of the
users who register with the website whenever a new user clicks the submit
button in the registration page.
Html:
<!DOCTYPE html>
<html>
<head>
<title>User Registration</title>
</head>
<body>
<h2>User Registration</h2>
<form action="[Link]" method="POST">
<label for="name">Name:</label>
<input type="text" name="name" id="name" required><br><br>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required><br><br>
<label for="email">Email:</label>
<input type="email" name="email" id="email" required><br><br>
<label for="phone">Phone Number:</label>
<input type="tel" name="phone" id="phone" required><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>
Java:
import [Link].*;
public class DatabaseExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/database_name";
String username = "username";
String password = "password";
try {
Connection conn = [Link](url, username, password);
String query = "SELECT * FROM users";
Statement statement = [Link]();
ResultSet resultSet = [Link](query);
while ([Link]()) {
int id = [Link]("id");
String name = [Link]("name");
String email = [Link]("email");
String phone = [Link]("phone");
[Link]("ID: " + id + ", Name: " + name + ", Email: " + email + ", Phone: " +
phone);
}
[Link]();
[Link]();
[Link]();
} catch (SQLException e) {
[Link]();
}
}
}
SQL:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
phone VARCHAR(20) NOT NULL
);
Output:
Lab 11: To Develop a student Marks sheet by using Servlet and HTML
with database Oracle
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Student Marks Entry</title>
</head>
<body>
<h2>Student Marks Entry</h2>
<form action="MarksEntryServlet" method="POST">
<label for="id">Student ID:</label>
<input type="text" name="id" id="id" required><br><br>
<label for="name">Name:</label>
<input type="text" name="name" id="name" required><br><br>
<label for="subject">Subject:</label>
<input type="text" name="subject" id="subject" required><br><br>
<label for="marks">Marks:</label>
<input type="number" name="marks" id="marks" required><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
JAVA:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MarksEntryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String JDBC_URL =
"jdbc:oracle:thin:@localhost:1521:your_oracle_service_name";
private static final String JDBC_USERNAME = "your_oracle_username";
private static final String JDBC_PASSWORD = "your_oracle_password";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
int id = [Link]([Link]("id"));
String name = [Link]("name");
String subject = [Link]("subject");
int marks = [Link]([Link]("marks"));
try {
Connection conn = [Link](JDBC_URL, JDBC_USERNAME,
JDBC_PASSWORD);
String query = "INSERT INTO student_marks (id, name, subject, marks) VALUES (?, ?,
?, ?)";
PreparedStatement statement = [Link](query);
[Link](1, id);
[Link](2, name);
[Link](3, subject);
[Link](4, marks);
int rowsInserted = [Link]();
if (rowsInserted > 0) {
[Link]().println("Marks entered successfully!");
} else {
[Link]().println("Failed to enter marks.");
}
[Link]();
[Link]();
} catch (SQLException e) {
[Link]();
}
}
}
SQL:
CREATE TABLE student_marks (
id NUMBER PRIMARY KEY,
name VARCHAR2(50) NOT NULL,
subject VARCHAR2(50) NOT NULL,
marks NUMBER
);
Lab 12: Design and implement a simple servlet for an entry form of student
details and send it to store at database server like SQL, Oracle or MS Access.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class StudentEntryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/database_name";
private static final String JDBC_USERNAME = "your_username";
private static final String JDBC_PASSWORD = "your_password";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String name = [Link]("name");
String email = [Link]("email");
String phone = [Link]("phone");
try {
Connection conn = [Link](JDBC_URL, JDBC_USERNAME,
JDBC_PASSWORD);
String query = "INSERT INTO students (name, email, phone) VALUES (?, ?, ?)";
PreparedStatement statement = [Link](query);
[Link](1, name);
[Link](2, email);
[Link](3, phone);
int rowsInserted = [Link]();
if (rowsInserted > 0) {
[Link]().println("Student details have been stored successfully!");
} else {
[Link]().println("Failed to store student details.");
}
[Link]();
[Link]();
} catch (SQLException e) {
[Link]();
}
}
}
Lab13: Write a JSP which insert the details of the 3 or 4 users who register
with the web site by using registration form. Authenticate the user when
he submits the login form using the user name and password from the
database
<%@ page import="[Link].*" %>
<%@ page import="[Link].*" %>
<%@ page import="[Link].*" %>
<%
String jdbcUrl = "jdbc:mysql://localhost:3306/database_name";
String jdbcUsername = "your_username";
String jdbcPassword = "your_password";
// Registration form submission
if ([Link]("register") != null) {
String name = [Link]("name");
String password = [Link]("password");
String email = [Link]("email");
String phone = [Link]("phone");
try {
Context initContext = new InitialContext();
Context envContext = (Context) [Link]("java:/comp/env");
DataSource dataSource = (DataSource) [Link]("jdbc/myDataSource");
Connection conn = [Link]();
String query = "INSERT INTO users (name, password, email, phone) VALUES (?, ?, ?,
?)";
PreparedStatement statement = [Link](query);
[Link](1, name);
[Link](2, password);
[Link](3, email);
[Link](4, phone);
int rowsInserted = [Link]();
if (rowsInserted > 0) {
[Link]("User registered successfully!");
} else {
[Link]("Failed to register user.");
}
[Link]();
[Link]();
} catch (Exception e) {
[Link]();
}
}
// Login form submission
if ([Link]("login") != null) {
String username = [Link]("username");
String password = [Link]("password");
try {
Context initContext = new InitialContext();
Context envContext = (Context) [Link]("java:/comp/env");
DataSource dataSource = (DataSource) [Link]("jdbc/myDataSource");
Connection conn = [Link]();
String query = "SELECT * FROM users WHERE name = ? AND password = ?";
PreparedStatement statement = [Link](query);
[Link](1, username);
[Link](2, password);
ResultSet rs = [Link]();
if ([Link]()) {
[Link]("Login successful!");
} else {
[Link]("Invalid username or password.");
}
[Link]();
[Link]();
[Link]();
} catch (Exception e) {
[Link]();
}
}
%>
<!DOCTYPE html>
<html>
<head>
<title>User Registration and Login</title>
</head>
<body>
<h2>User Registration</h2>
<form action="" method="POST">
<label for="name">Name:</label>
<input type="text" name="name" required><br><br>
<label for="password">Password:</label>
<input type="password" name="password" required><br><br>
<label for="email">Email:</label>
<input type="email" name="email" required><br><br>
<label for="phone">Phone Number:</label>
<input type="tel" name="phone" required><br><br>
<input type="submit" name="register" value="Register">
</form>
<h2>User Login</h2>
<form action="" method="POST">
<label for="username">Username:</label>
<input type="text" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" name="password" required><br><br>
<input type="submit" name="login" value="Login">
</form>
</body>
</html>
Lab14: Store 5 Students (name, branch, rollno, age) objects in list. Perform
any sorting technique so as to display list in ascending order of rollno and
display in descending order of age.
import [Link];
import [Link];
import [Link];
import [Link];
class Student {
private String name;
private String branch;
private int rollNo;
private int age;
public Student(String name, String branch, int rollNo, int age) {
[Link] = name;
[Link] = branch;
[Link] = rollNo;
[Link] = age;
}
public String getName() {
return name;
}
public String getBranch() {
return branch;
}
public int getRollNo() {
return rollNo;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", branch='" + branch + '\'' +
", rollNo=" + rollNo +
", age=" + age +
'}';
}
}
public class StudentSortingExample {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<>();
[Link](new Student("Alice", "CSE", 101, 20));
[Link](new Student("Bob", "ECE", 103, 22));
[Link](new Student("Charlie", "IT", 105, 19));
[Link](new Student("David", "MECH", 102, 21));
[Link](new Student("Eve", "EEE", 104, 20));
[Link](studentList, [Link](Student::getRollNo));
[Link]("List sorted in ascending order of roll number:");
for (Student student : studentList) {
[Link](student);
}
[Link](studentList, [Link](Student::getAge).reversed());
[Link]("\nList sorted in descending order of age:");
for (Student student : studentList) {
[Link](student);
}
}
}
Lab15: Perform union (AUB), intersection and difference (A-B) operations
using set collection.
import [Link];
import [Link];
public class SetOperationsExample {
public static void main(String[] args) {
// Set A
Set<Integer> setA = new HashSet<>();
[Link](1);
[Link](2);
[Link](3);
// Set B
Set<Integer> setB = new HashSet<>();
[Link](2);
[Link](3);
[Link](4);
// Union (A ∪ B)
Set<Integer> unionSet = new HashSet<>(setA);
[Link](setB);
[Link]("Union (A ∪ B): " + unionSet);
// Intersection (A ∩ B)
Set<Integer> intersectionSet = new HashSet<>(setA);
[Link](setB);
[Link]("Intersection (A ∩ B): " + intersectionSet);
// Difference (A - B)
Set<Integer> differenceSet = new HashSet<>(setA);
[Link](setB);
[Link]("Difference (A - B): " + differenceSet);
}
}
Lab16: Create a Map that consists of Country-Capital pair to store
information about various countries along with their capital name. Display
the entries in sorted order of Country and Capital.
import [Link].*;
public class CountryCapitalMapExample {
public static void main(String[] args) {
Map<String, String> countryCapitalMap = new HashMap<>();
[Link]("India", "New Delhi");
[Link]("United States", "Washington D.C.");
[Link]("Japan", "Tokyo");
[Link]("France", "Paris");
[Link]("Australia", "Canberra");
List<[Link]<String, String>> sortedEntries = new
ArrayList<>([Link]());
[Link](sortedEntries, new Comparator<[Link]<String, String>>() {
public int compare([Link]<String, String> entry1, [Link]<String, String>
entry2) {
int result = [Link]().compareTo([Link]());
if (result == 0) {
result = [Link]().compareTo([Link]());
}
return result;
}
});
[Link]("Country-Capital pairs (sorted):");
for ([Link]<String, String> entry : sortedEntries) {
[Link]([Link]() + " - " + [Link]());
}
}
}