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
S.No 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 java.util.Scanner;
public class ArithmeticOperations {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter second number: ");
double num2 = sc.nextDouble();
System.out.println("Select operation: ");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.print("Enter choice (1-4): ");
int choice = sc.nextInt();
double result;
switch (choice) {
case 1:
result = num1 + num2;
System.out.println("Result: " + result);
break;
case 2:
result = num1 - num2;
System.out.println("Result: " + result);
break;
case 3:
result = num1 * num2;
System.out.println("Result: " + result);
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
System.out.println("Result: " + result);
} else {
System.out.println("Error! Division by zero is not allowed.");
}
break;
default:
System.out.println("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 java.sql.*;
public class EmployeeDatabase {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/organize1";
String username = "root";
String password = "tanish@#17&%";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(url, username, password);
System.out.println("Connected to the database.");
String query = "SELECT * FROM employee";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
System.out.println("Employee Records:");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String position = rs.getString("position");
double salary = rs.getDouble("salary");
System.out.println("ID: " + id + ", Name: " + name +
", Position: " + position + ", Salary: " + salary);
School of Computing
}
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
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++) {
System.out.println("Running in thread: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Thread interrupted");
}
}
}
}
public class RunnableCode{
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
for (int i = 1; i <= 5; i++) {
System.out.println("Main thread: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("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) {
this.threadName = name;
}
public void run() {
for (int i = 1; i <= 3; i++) {
System.out.println(threadName + " is running: step " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(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");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
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 java.sql.*;
public class RegisterNavigation {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/entry";
String username = "root";
String password = "tanish@#17&%";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("SELECT * FROM Register");
System.out.println("First record:");
if (rs.first()) {
displayRow(rs);
}
System.out.println("\nLast record:");
if (rs.last()) {
displayRow(rs);
School of Computing
}
System.out.println("\nMoving to previous record (if any):");
if (rs.previous()) {
displayRow(rs);
}
System.out.println("\nMoving to next record (if any):");
if (rs.next()) {
displayRow(rs);
}
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void displayRow(ResultSet rs) throws SQLException {
int id = rs.getInt("id");
String name = rs.getString("name");
String email = rs.getString("email");
String course = rs.getString("course");
System.out.println("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 java.sql.*;
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)
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish connection
conn = DriverManager.getConnection(url, user, pass);
conn.setAutoCommit(false); // Start transaction
// Prepare statements
Statement stmt = conn.createStatement();
// First operation
stmt.executeUpdate("INSERT INTO student (id, name, marks) VALUES (1, 'Alice',
85)");
// Second operation (intentional error: duplicate primary key to trigger rollback)
stmt.executeUpdate("INSERT INTO student (id, name, marks) VALUES (1, 'Bob',
90)");
School of Computing
// If both succeed, commit
conn.commit();
System.out.println("Transaction committed successfully.");
} catch (Exception e) {
System.out.println("Exception occurred: " + e.getMessage());
try {
if (conn != null) {
conn.rollback();
System.out.println("Transaction rolled back.");
}
} catch (SQLException rollbackEx) {
System.out.println("Rollback failed: " + rollbackEx.getMessage());
}
} finally {
try {
if (conn != null) conn.close();
} catch (SQLException closeEx) {
System.out.println("Failed to close connection: " + closeEx.getMessage());
}
}
}
}
Output:
School of Computing
Practical – 7
Objective - Write a program in JDBC to insert Oganization details by
using PreparedStatement Interface.
SQL Query:
Code:
import java.sql.*;
import java.util.Scanner;
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(System.in);
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, user, pass);
String sql = "INSERT INTO organization (org_id, org_name, location) VALUES
(?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
System.out.print("Enter Organization ID: ");
int id = sc.nextInt();
sc.nextLine();
System.out.print("Enter Organization Name: ");
String name = sc.nextLine();
System.out.print("Enter Location: ");
String location = sc.nextLine();
School of Computing
pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.setString(3, location);
int rowsInserted = pstmt.executeUpdate();
if (rowsInserted > 0) {
System.out.println("Organization inserted successfully!");
}
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
sc.close();
}
}
}
Output:
School of Computing
Practical - 8
Objective - Write a program to demonstrate Life Cycle of Servlet.
Code:
School of Computing