122 Java 6
122 Java 6
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.
• Display staff details those who got incentive more than 1000 in one month.
Aim:
import java.util.ArrayList;
import java.util.List;
import
java.util.Map.Entry;
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
public String toString() { return "Staff ID: " + staffId + ", Name: " + name + ",
Department: " + department + ", Incentive: Rs. " + incentive + ", Month: " + month; }
}
}
}
}
}
}
management.displayIncentivesAbove(1000);
management.updateIncentive(1103, 900);
management.displayTotalIncentivesAbove(800);
}}
Output
Question 2
Write a Java program to connect with database using JDBC and perform the following operations on
the Cars table using Prepared Statement:
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 {
modelName;
int price;
modelName;
this.price = price;
@Override
public String toString() { return "Brand: " + brandName + ", Model: " + modelName
List<Car> cars;
public CarManagement() {
initializeData();
if (car.brandName.equalsIgnoreCase(brandName)) {
count++;
if (car.modelName.equalsIgnoreCase(modelName)) { car.price
= newPrice;
System.out.println("Updated price for " + modelName + " to Rs. " + newPrice); return;
carManagement.displayNumberOfModels("Honda");
carManagement.cars) {
System.out.println(car);
Output:
Question 3:
• 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;
@Override
return "EmpID: " + empId + ", Name: " + name + ", Salary: Rs. " + salary +
new ArrayList<>();
initializeData();
System.out.println("Employee
employees) {
System.out.println(employee);
} else {
employeeManagement.displayEmployees();
employeeManagement.calculateAndUpdateTax();
employeeManagement.displayEmployees();
Output:
Result:
Thus, the Java programs using the JDBC has been successfully developed and the output was verified.
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:
import java.util.LinkedList;
= 10;
{ list.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
list.add(item);
list.notify();
synchronized (list) {
while (list.isEmpty()) {
try { list.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
" + item);
list.notify();
producerThread.start(); consumerThread.start();
try {
producerThread.join();
consumerThread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
OUTPUT:
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.
Aim:
To write the java programs using JDBC.
Code:
int balance;
= initialBalance;
} else {
-= amount;
balance += amount;
BankAccount account;
BankAccount account;
withdrawThread1.start(); depositThread1.start();
withdrawThread2.start();
try {
withdrawThread1.join();
depositThread1.join();
withdrawThread2.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Output:
Result:
Thus, the Java programs using the multithreading has been successfully developed and the output was
verified.