0% found this document useful (0 votes)
14 views17 pages

122 Java 6

Uploaded by

717823l121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views17 pages

122 Java 6

Uploaded by

717823l121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

Ex no: 6
Implement programs using JDBC to establish and manage database
Date: connections for data persistence and retrieval

Question 1:
.Write a Java program to create the following staff_incentive table using JDBC.

Perform the following operations using JDBC:

• Display staff details those who got incentive more than 1000 in one month.

• Update the incentive to Rs. 900 for the staff_id 1103.

• Display staff names whose total incentives are above 800.

Aim:

To write the java programs using JDBC.


Code:

import java.util.ArrayList;
import java.util.List;
import
java.util.Map.Entry;

class Staff { int


staffId;
String name;
String department;
int incentive;
String month;

public Staff(int staffId, String name, String department, int incentive, String month) {
this.staffId = staffId; this.name = name; this.department = department; this.incentive
= incentive;
this.month = month;
}

@Override

Register Number : 717823L121


22
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

public String toString() { return "Staff ID: " + staffId + ", Name: " + name + ",
Department: " + department + ", Incentive: Rs. " + incentive + ", Month: " + month; }
}

public class StaffIncentiveManagement { private


List<Staff> staffIncentives;

public StaffIncentiveManagement() { staffIncentives =


new ArrayList<>();
initializeData();
}

private void initializeData() { staffIncentives.add(new Staff(1101, "Mukesh


R", "CSE", 500, "June")); staffIncentives.add(new Staff(1102, "Yokesh S",
"IT", 700, "June")); staffIncentives.add(new Staff(1101, "Mukesh R", "CSE",
600, "July")); staffIncentives.add(new Staff(1103, "Devi K", "ECE", 1100,
"August")); }

public void displayIncentivesAbove(int amount) {


System.out.println("Staff details with incentives above Rs. " + amount + ":");
for (Staff staff : staffIncentives) { if (staff.incentive > amount) {
System.out.println(staff);
}
}
}

public void updateIncentive(int staffId, int newIncentive) {


for (Staff staff : staffIncentives) { if (staff.staffId ==
staffId) { staff.incentive = newIncentive;
System.out.println("Updated Incentive for Staff ID " + staffId + " to Rs. " + newIncentive); return;
}}
System.out.println("Staff ID " + staffId + " not found."); }

public void displayTotalIncentivesAbove(int amount) {


System.out.println("Staff names with total incentives above Rs. " + amount + ":");
java.util.Map<Integer, Integer> totalIncentives = new java.util.HashMap<>();

for (Staff staff : staffIncentives) { totalIncentives.put(staff.staffId,


totalIncentives.getOrDefault(staff.staffId, 0) + staff.incentive);
}

for (Entry<Integer, Integer> entry : totalIncentives.entrySet()) { if


(entry.getValue() > amount) { for (Staff staff : staffIncentives) {
if (staff.staffId == entry.getKey()) {
System.out.println(staff.name); break;

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

}
}
}
}
}

public static void main(String[] args) {


StaffIncentiveManagement management = new StaffIncentiveManagement();

management.displayIncentivesAbove(1000);
management.updateIncentive(1103, 900);
management.displayTotalIncentivesAbove(800);
}}
Output

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

Question 2

Create the following cars table in the Database using JDBC:

Write a Java program to connect with database using JDBC and perform the following operations on
the Cars table using Prepared Statement:

• Display Number of the models in the same Brand name.

• Update the price of a given car name.

Aim:
To write the java programs using JDBC.

Code:

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

class Car {

String brandName; String

modelName;

int price;

public Car(String brandName, String modelName, int price) {

this.brandName = brandName; this.modelName =

modelName;

this.price = price;

@Override

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

public String toString() { return "Brand: " + brandName + ", Model: " + modelName

+ ", Price: Rs. " + price;

public class CarManagement { private

List<Car> cars;

public CarManagement() {

cars = new ArrayList<>();

initializeData();

private void initializeData() {

cars.add(new Car("Honda", "Honda City ZX", 400000));

cars.add(new Car("Honda", "Honda City iVTEC", 950000));

cars.add(new Car("Suzuki", "S-Cross", 850000));

cars.add(new Car("Renault", "Triber", 975000));

public void displayNumberOfModels(String brandName) {

int count = 0; for (Car car : cars) {

if (car.brandName.equalsIgnoreCase(brandName)) {
count++;

System.out.println("Number of models for brand " + brandName + ": " + count);

public void updateCarPrice(String modelName, int newPrice) {

for (Car car : cars) {

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

if (car.modelName.equalsIgnoreCase(modelName)) { car.price

= newPrice;

System.out.println("Updated price for " + modelName + " to Rs. " + newPrice); return;

System.out.println("Car model " + modelName + " not found.");

public static void main(String[] args) {

CarManagement carManagement = new CarManagement();

carManagement.displayNumberOfModels("Honda");

carManagement.updateCarPrice("Honda City ZX", 450000);

System.out.println("\nUpdated car details:"); for (Car car :

carManagement.cars) {

System.out.println(car);

Output:

Question 3:

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

Create the following cars table in the Database using JDBC:

• Write a Java program to create the employee table and insert the 3 rows given in the table using
Prepared Statement.

• Create a Stored Procedure to calculate the tax for the salary of the employees in the employee table
and update the ‘tax_per_month’ column. Invoke the stored procedure using Callable Statement. The tax
is calculated based on the table given below:

Aim:
To write the java programs using JDBC.
Code:

import java.util.ArrayList;

import java.util.List;

class Employee {

int empId;

String name;

int salary;

int taxPerMonth;

public Employee(int empId, String name, int salary) {

this.empId = empId; this.name = name;

this.salary = salary; this.taxPerMonth = 0; //

Initially set tax to 0

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

@Override

public String toString() {

return "EmpID: " + empId + ", Name: " + name + ", Salary: Rs. " + salary +

", Tax per Month: Rs. " + taxPerMonth;

public class EmployeeManagement {

private List<Employee> employees;

public EmployeeManagement() { employees =

new ArrayList<>();

initializeData();

private void initializeData() { employees.add(new

Employee(101, "Arun", 35000)); employees.add(new

Employee(102, "Deepak", 41500)); employees.add(new

Employee(103, "Ashik", 55000));

public void displayEmployees() {

System.out.println("Employee

Details:"); for (Employee employee :

employees) {

System.out.println(employee);

public void calculateAndUpdateTax() { for (Employee

employee : employees) { if (employee.salary < 50000) {

employee.taxPerMonth = (int) (employee.salary * 0.02);

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

} else {

employee.taxPerMonth = (int) (employee.salary * 0.023);

public static void main(String[] args) {

EmployeeManagement employeeManagement = new EmployeeManagement();

employeeManagement.displayEmployees();

employeeManagement.calculateAndUpdateTax();

System.out.println("\nUpdated Employee Details after Tax Calculation:");

employeeManagement.displayEmployees();

Output:

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

Result:
Thus, the Java programs using the JDBC has been successfully developed and the output was verified.

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

Ex no: 7
Develop programs using multithreading to achieve concurrent execution and
Date: improve application performance

Create a thread, called the producer, which keeps inserting strings into the LinkedList as long as there
are fewer than 10 elements in it.When the ArrayList gets too full, the thread waits. Create a second
thread, called the consumer that keeps removing and printing strings from the ArrayList as long as the
array is not empty. When the array is empty, the thread waits. Both the consumer and producer threads
should run for 100 iterations..

Aim:

To write the java programs using multithreading. Code:

import java.util.LinkedList;

class ProducerConsumer { private LinkedList<String>

list = new LinkedList<>(); private final int MAX_SIZE

= 10;

public void produce() { for (int i = 0; i

< 100; i++) { synchronized (list) {

while (list.size() >= MAX_SIZE) { try

{ list.wait();

} catch (InterruptedException e) {

Thread.currentThread().interrupt();

String item = "Item " + i;

list.add(item);

System.out.println("Produced: " + item);

list.notify();

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

public void consume() {

for (int i = 0; i < 100; i++) {

synchronized (list) {

while (list.isEmpty()) {

try { list.wait();

} catch (InterruptedException e) {

Thread.currentThread().interrupt();

String item = list.removeFirst(); System.out.println("Consumed:

" + item);

list.notify();

public static void main(String[] args) {

ProducerConsumer pc = new ProducerConsumer();

Thread producerThread = new Thread(() -> pc.produce());

Thread consumerThread = new Thread(() -> pc.consume());

producerThread.start(); consumerThread.start();

try {

producerThread.join();

consumerThread.join();

} catch (InterruptedException e) {

Thread.currentThread().interrupt();

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

OUTPUT:

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

Question 2
Consider a banking scenario in which a customer wishes to withdraw an amount from his/her
account. The withdraw operation is successful if and only if a valid amount is available in the
account else the customer has to deposit the amount first and then perform withdraw operation.

• Create a thread to handle withdraw operation

• Create another thread to handle deposit operation


• Illustrate the above scenario using the synchronization concept.

Aim:
To write the java programs using JDBC.

Code:

class BankAccount { private

int balance;

public BankAccount(int initialBalance) { this.balance

= initialBalance;

public synchronized void withdraw(int amount) { if

(amount > balance) {

System.out.println("Insufficient balance! Please deposit first.");

} else {

System.out.println("Withdrawing: Rs. " + amount); balance

-= amount;

System.out.println("Withdrawal successful. New balance: Rs. " + balance);

public synchronized void deposit(int amount) {

System.out.println("Depositing: Rs. " + amount);

balance += amount;

System.out.println("Deposit successful. New balance: Rs. " + balance);

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

class WithdrawThread extends Thread { private

BankAccount account;

private int amount;

public WithdrawThread(BankAccount account, int amount) {

this.account = account; this.amount = amount;

public void run() { account.withdraw(amount);

class DepositThread extends Thread { private

BankAccount account;

private int amount;

public DepositThread(BankAccount account, int amount) {

this.account = account; this.amount = amount;

public void run() { account.deposit(amount);

public class BankingScenario { public

static void main(String[] args) {

BankAccount account = new BankAccount(5000);

WithdrawThread withdrawThread1 = new WithdrawThread(account, 6000);

DepositThread depositThread1 = new DepositThread(account, 2000);

WithdrawThread withdrawThread2 = new WithdrawThread(account, 3000);

withdrawThread1.start(); depositThread1.start();

withdrawThread2.start();

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

try {

withdrawThread1.join();

depositThread1.join();

withdrawThread2.join();

} catch (InterruptedException e) {

Thread.currentThread().interrupt();

Output:

Register Number : 717823L121


DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306/JAVA PROGRAMMING LABORATORY

Result:
Thus, the Java programs using the multithreading has been successfully developed and the output was
verified.

Register Number : 717823L121

You might also like