Lab Record
of
Advanced Java Programming
(CAN604)
Submitted to: Submitted by:
Mr. Ravi Shankar Jha Sawan
Assistant Professor 1000026853
School of Computing MCA (P1)
DIT University I Yr (II Sem)
Session 2024-25
School of Computing
Index
[Link] Title of Objective / Experiment Signature
. of Faculty
1. Write a program in java to perform basic arithmetic
operations.
2. Write a program in java to create database connectivity
with organization database and display the record of
employee table.
3. Write a program to implement Runnable Interface.
4. Write a program to create 5 thread by extending thread
class.
5. Write a program to create table Register in database
and perform navigation operation using ResultSet.
6. Write a program in JDBC to perform Transaction
Management by using SetAutoCommit(), Commit() &
rollback() in the table student.
7. Write a program in JDBC to insert Oganization details
by using PreparedStatement Interface.
8. Write a program to demonstrate Life Cycle of Servlet.
9. Write a program to switch ON and OFF the bulb by
using visible bean.
10. Create a java application by implementing Generic
Servlet Class.
11. Write a program to display the utilization of
HttpServletClass.
12. Create login application in java by using MVC and
mysql.
Practical – 1
School of Computing
Objective- Write a program in java to perform basic arithmetic operations.
Code:
import [Link];
public class ArithmeticOperations {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
double num1 = [Link]();
[Link]("Enter second number: ");
double num2 = [Link]();
[Link]("Select operation: ");
[Link]("1. Addition");
[Link]("2. Subtraction");
[Link]("3. Multiplication");
[Link]("4. Division");
[Link]("Enter choice (1-4): ");
int choice = [Link]();
double result;
switch (choice) {
case 1:
result = num1 + num2;
[Link]("Result: " + result);
break;
case 2:
result = num1 - num2;
[Link]("Result: " + result);
break;
case 3:
result = num1 * num2;
[Link]("Result: " + result);
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
[Link]("Result: " + result);
} else {
[Link]("Error! Division by zero is not allowed.");
}
break;
default:
[Link]("Invalid choice! Please select a valid option.");
}
}
}
School of Computing
Output:
School of Computing
School of Computing
Practical – 2
Objective - Write a program in java to create database connectivity with
organization database and display the record of employee table.
SQL Query:
Code:
import [Link].*;
public class EmployeeDatabase {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/organize1";
String username = "root";
String password = "tanish@#17&%";
try {
[Link]("[Link]");
Connection con = [Link](url, username, password);
[Link]("Connected to the database.");
String query = "SELECT * FROM employee";
Statement stmt = [Link]();
ResultSet rs = [Link](query);
[Link]("Employee Records:");
while ([Link]()) {
int id = [Link]("id");
String name = [Link]("name");
String position = [Link]("position");
double salary = [Link]("salary");
[Link]("ID: " + id + ", Name: " + name +
", Position: " + position + ", Salary: " + salary);
School of Computing
}
[Link]();
[Link]();
[Link]();
} catch (Exception e) {
[Link]();
}
}
}
Output:
School of Computing
Practical – 3
Objective - Write a program to implement Runnable Interface.
Code:
class MyRunnable implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("Running in thread: " + i);
try {
[Link](500);
} catch (InterruptedException e) {
[Link]("Thread interrupted");
}
}
}
}
public class RunnableCode{
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
[Link]();
for (int i = 1; i <= 5; i++) {
[Link]("Main thread: " + i);
try {
[Link](500);
} catch (InterruptedException e) {
[Link]("Main thread interrupted");
}
}
}
}
Output:
School of Computing
Practical – 4
Objective - Write a program to create 5 thread by extending thread class.
Code:
class MyThread extends Thread {
private String threadName;
MyThread(String name) {
[Link] = name;
}
public void run() {
for (int i = 1; i <= 3; i++) {
[Link](threadName + " is running: step " + i);
try {
[Link](500);
} catch (InterruptedException e) {
[Link](threadName + " was interrupted.");
}
}
}
}
public class MultiThread {
public static void main(String[] args){
MyThread t1 = new MyThread("Thread-1");
MyThread t2 = new MyThread("Thread-2");
MyThread t3 = new MyThread("Thread-3");
MyThread t4 = new MyThread("Thread-4");
MyThread t5 = new MyThread("Thread-5");
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Output:
School of Computing
School of Computing
Practical – 5
Objective - Write a program to create table Register in database and
perform navigation operation using ResultSet.
SQL Query:
Code:
import [Link].*;
public class RegisterNavigation {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/entry";
String username = "root";
String password = "tanish@#17&%";
try {
[Link]("[Link]");
Connection conn = [Link](url, username, password);
Statement stmt = [Link](ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = [Link]("SELECT * FROM Register");
[Link]("First record:");
if ([Link]()) {
displayRow(rs);
}
[Link]("\nLast record:");
if ([Link]()) {
displayRow(rs);
School of Computing
}
[Link]("\nMoving to previous record (if any):");
if ([Link]()) {
displayRow(rs);
}
[Link]("\nMoving to next record (if any):");
if ([Link]()) {
displayRow(rs);
}
[Link]();
[Link]();
[Link]();
} catch (Exception e) {
[Link]();
}
}
private static void displayRow(ResultSet rs) throws SQLException {
int id = [Link]("id");
String name = [Link]("name");
String email = [Link]("email");
String course = [Link]("course");
[Link]("ID: " + id + ", Name: " + name + ", Email: " + email + ", Course: "
+ course);
}
}
Output:
School of Computing
Practical – 6
Objective - Write a program in JDBC to perform Transaction
Management by using SetAutoCommit(), Commit() & rollback() in the
table student.
SQL Query:
Code:
import [Link].*;
public class StudentTransactionExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/organization"; // change DB name if needed
String user = "root"; // your DB username
String pass = "password"; // your DB password
Connection conn = null;
try {
// Load JDBC driver (optional in newer Java versions)
[Link]("[Link]");
// Establish connection
conn = [Link](url, user, pass);
[Link](false); // Start transaction
// Prepare statements
Statement stmt = [Link]();
// First operation
[Link]("INSERT INTO student (id, name, marks) VALUES (1, 'Alice',
85)");
// Second operation (intentional error: duplicate primary key to trigger rollback)
[Link]("INSERT INTO student (id, name, marks) VALUES (1, 'Bob',
90)");
School of Computing
// If both succeed, commit
[Link]();
[Link]("Transaction committed successfully.");
} catch (Exception e) {
[Link]("Exception occurred: " + [Link]());
try {
if (conn != null) {
[Link]();
[Link]("Transaction rolled back.");
}
} catch (SQLException rollbackEx) {
[Link]("Rollback failed: " + [Link]());
}
} finally {
try {
if (conn != null) [Link]();
} catch (SQLException closeEx) {
[Link]("Failed to close connection: " + [Link]());
}
}
}
}
Output:
School of Computing
Practical – 7
Objective - Write a program in JDBC to insert Oganization details by
using PreparedStatement Interface.
SQL Query:
Code:
import [Link].*;
import [Link];
public class InsertOrganization {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/organize3";
String user = "root";
String pass = "tanish@#17&%";
Scanner sc = new Scanner([Link]);
try {
[Link]("[Link]");
Connection conn = [Link](url, user, pass);
String sql = "INSERT INTO organization (org_id, org_name, location) VALUES
(?, ?, ?)";
PreparedStatement pstmt = [Link](sql);
[Link]("Enter Organization ID: ");
int id = [Link]();
[Link]();
[Link]("Enter Organization Name: ");
String name = [Link]();
[Link]("Enter Location: ");
String location = [Link]();
School of Computing
[Link](1, id);
[Link](2, name);
[Link](3, location);
int rowsInserted = [Link]();
if (rowsInserted > 0) {
[Link]("Organization inserted successfully!");
}
[Link]();
[Link]();
} catch (Exception e) {
[Link]();
} finally {
[Link]();
}
}
}
Output:
School of Computing
Practical - 8
Objective - Write a program to demonstrate Life Cycle of Servlet.
Code:
School of Computing