Advanced Java Lab– PART B- Program 01 to 04
package [Link];
import [Link];
import [Link].*;
public class StudentMgt {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
try {
[Link]("[Link]");
Connection conn =
[Link]("jdbc:derby://localhost:1527/StudentDatabase", "vcp","vcp");
int choice;
do {
[Link]("1. Add new Student");
[Link]("2. Delete a specified student's Record");
[Link]("3. Update Student's Address");
[Link]("4. Search for a particular Student");
[Link]("5. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
switch (choice) {
case 1:
addNewStudent(conn, scanner);
break;
case 2:
deleteStudentRecord(conn, scanner);
break;
case 3:
updateStudentAddress(conn, scanner);
break;
case 4:
searchStudent(conn, scanner);
break;
case 5:
[Link]("Exiting...");
break;
default:
[Link]("Invalid choice. Please try again.");
}
} while (choice != 5);
[Link]();
[Link]();
} catch (SQLException se) {
[Link]();
} catch (Exception e) {
[Link]();
}
}
static void addNewStudent(Connection conn, Scanner scanner) throws SQLException {
[Link]("Enter Student Details:");
[Link]("Registration Number: ");
int regNo = [Link]();
[Link]("Name: ");
[Link](); // Consume newline character
String sname = [Link]();
[Link]("Date of Birth (YYYY-MM-DD): ");
String dob = [Link]();
[Link]("Address: ");
String address = [Link]();
[Link]("Class: ");
String class_ = [Link]();
[Link]("Course: ");
String course = [Link]();
String sql = "INSERT INTO student (regno, sname, dob, address, class, course) VALUES (?, ?, ?,
?, ?, ?)";
PreparedStatement preparedStatement = [Link](sql);
[Link](1, regNo);
[Link](2, sname);
[Link](3, dob);
[Link](4, address);
[Link](5, class_);
[Link](6, course);
[Link]();
[Link]("Student added successfully!");
}
static void deleteStudentRecord(Connection conn, Scanner scanner) throws SQLException {
[Link]("Enter Registration Number of the student to delete: ");
int regNo = [Link]();
String sql = "DELETE FROM Student WHERE regno = ?";
PreparedStatement preparedStatement = [Link](sql);
[Link](1, regNo);
int rowsDeleted = [Link]();
if (rowsDeleted > 0) {
[Link]("Student record deleted successfully!");
} else {
[Link]("No such student found with the given Registration Number.");
}
}
static void updateStudentAddress(Connection conn, Scanner scanner) throws SQLException {
[Link]("Enter Registration Number of the student to update: ");
int regNo = [Link]();
[Link](); // Consume newline character
[Link]("Enter new Address: ");
String newAddress = [Link]();
String sql = "UPDATE Student SET address = ? WHERE regno = ?";
PreparedStatement preparedStatement = [Link](sql);
[Link](1, newAddress);
[Link](2, regNo);
int rowsUpdated = [Link]();
if (rowsUpdated > 0) {
[Link]("Student address updated successfully!");
} else {
[Link]("No such student found with the given Registration Number.");
}
}
static void searchStudent(Connection conn, Scanner scanner) throws SQLException {
[Link]("Enter Registration Number of the student to search: ");
int regNo = [Link]();
String sql = "SELECT * FROM Student WHERE regno = ?";
PreparedStatement preparedStatement = [Link](sql);
[Link](1, regNo);
ResultSet resultSet = [Link]();
if ([Link]()) {
[Link]("Student Details:");
[Link]("Registration Number: " + [Link]("regno"));
[Link]("Name: " + [Link]("name"));
[Link]("Date of Birth: " + [Link]("dob"));
[Link]("Address: " + [Link]("address"));
[Link]("Class: " + [Link]("class"));
[Link]("Course: " + [Link]("course"));
} else {
[Link]("No student found with the given Registration Number.");
}
}
}
import [Link].*;
import [Link];
import [Link].*;
public class BankMgt {
public static void main(String[] args) {
Connection conn = null;
Scanner scanner = new Scanner([Link]); // Create a single Scanner object
try {
[Link]("[Link]");
conn = [Link]("jdbc:derby://localhost:1527/BankDB", "vcp", "vcp");
int choice;
do {
[Link]("MENU");
[Link]("1. Add new Account Holder information");
[Link]("2. Amount Deposit");
[Link]("3. Amount Withdrawal (Maintain minimum balance 500 Rs)");
[Link]("4. Display all information");
[Link]("5. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
switch (choice) {
case 1:
addNewAccountHolder(conn, scanner);
break;
case 2:
depositAmount(conn, scanner);
break;
case 3:
withdrawAmount(conn, scanner);
break;
case 4:
displayAllInformation(conn);
break;
case 5:
[Link]("Exiting...");
break;
default:
[Link]("Invalid choice. Please try again.");
}
} while (choice != 5);
[Link]();
[Link]();
} catch (SQLException se) {
[Link]();
} catch (Exception e) {
[Link]();
}
}
static void addNewAccountHolder(Connection conn, Scanner scanner) {
try {
[Link]("Enter Account Number: ");
int accNo = [Link]();
[Link](); // Consume newline character
[Link]("Enter Account Holder Name: ");
String accHolderName = [Link]();
[Link]("Enter Address: ");
String address = [Link]();
[Link]("Enter Initial Balance: ");
double balance = [Link]();
String sql = "INSERT INTO bank (accno, acname, address, balance) VALUES (?, ?, ?, ?)";
PreparedStatement preparedStatement = [Link](sql);
[Link](1, accNo);
[Link](2, accHolderName);
[Link](3, address);
[Link](4, balance);
int rowsInserted = [Link]();
if (rowsInserted > 0) {
[Link]("New account holder information added successfully!");
} else {
[Link]("Failed to add new account holder information.");
}
} catch (SQLException se) {
[Link]();
}
}
static void depositAmount(Connection conn, Scanner scanner) {
try {
[Link]("Enter Account Number: ");
int accNo = [Link]();
[Link]("Enter Deposit Amount: ");
double amount = [Link]();
String sql = "UPDATE bank SET balance = balance + ? WHERE accno = ?";
PreparedStatement preparedStatement = [Link](sql);
[Link](1, amount);
[Link](2, accNo);
int rowsUpdated = [Link]();
if (rowsUpdated > 0) {
[Link]("Amount deposited successfully!");
} else {
[Link]("No account found with the given Account Number.");
}
} catch (SQLException se) {
[Link]();
}
}
static void withdrawAmount(Connection conn, Scanner scanner) {
try {
[Link]("Enter Account Number: ");
int accNo = [Link]();
[Link]("Enter Withdrawal Amount: ");
double amount = [Link]();
String checkBalanceSql = "SELECT balance FROM bank WHERE accno = ?";
PreparedStatement checkBalanceStmt = [Link](checkBalanceSql);
[Link](1, accNo);
ResultSet resultSet = [Link]();
if ([Link]()) {
double balance = [Link]("balance");
if (balance - amount >= 500) {
String updateSql = "UPDATE bank SET balance = balance - ? WHERE accno = ?";
PreparedStatement updateStmt = [Link](updateSql);
[Link](1, amount);
[Link](2, accNo);
int rowsUpdated = [Link]();
if (rowsUpdated > 0) {
[Link]("Amount withdrawn successfully!");
} else {
[Link]("Failed to withdraw amount.");
}
} else {
[Link]("Insufficient balance! Minimum balance of 500 Rs should be
maintained.");
}
} else {
[Link]("No account found with the given Account Number.");
}
} catch (SQLException se) {
[Link]();
}
}
static void displayAllInformation(Connection conn) {
try {
Statement stmt = [Link]();
String sql = "SELECT * FROM bank";
ResultSet resultSet = [Link](sql);
while ([Link]()) {
[Link]("Account Number: " + [Link]("accno"));
[Link]("Account Name: " + [Link]("acname"));
[Link]("Address: " + [Link]("address"));
[Link]("Balance: " + [Link]("balance"));
[Link]("-----------------------------");
}
} catch (SQLException se) {
[Link]();
}
}
}
[Link]
import [Link];
import [Link];
public interface Tax extends Remote {
double calculateTax(double income) throws RemoteException;
}
[Link]
import [Link];
import [Link];
public class TaxImpl extends UnicastRemoteObject implements Tax {
public TaxImpl() throws RemoteException {
super();
}
@Override
public double calculateTax(double income) throws RemoteException {
if (income <= 300000) {
return 0;
} else if (income <= 600000) {
return (income - 300000) * 0.05;
} else if (income <= 900000) {
return (300000 * 0.05) + ((income - 600000) * 0.10);
} else if (income <= 1200000) {
return (300000 * 0.05) + (300000 * 0.10) + ((income - 900000) * 0.15);
} else if (income <= 1500000) {
return (300000 * 0.05) + (300000 * 0.10) + (300000 * 0.15) + ((income - 1200000) * 0.20);
} else {
return (300000 * 0.05) + (300000 * 0.10) + (300000 * 0.15) + (300000 * 0.20) + ((income -
1500000) * 0.30);
}
}
}
[Link]
import [Link];
public class TaxServer {
public static void main(String[] args) {
try {
Tax tax = new TaxImpl();
[Link]("TaxService", tax);
[Link]("Tax service is ready...");
} catch (Exception e) {
[Link]("Server exception: " + [Link]());
[Link]();
}
}
}
[Link]
import [Link];
import [Link];
public class TaxClient {
public static void main(String[] args) {
try {
Tax tax = (Tax) [Link]("//localhost/TaxService");
Scanner scanner = new Scanner([Link]);
[Link]("Enter income: ");
double income = [Link]();
double taxAmount = [Link](income);
[Link]("Tax amount: " + taxAmount);
} catch (Exception e) {
[Link]("Client exception: " + [Link]());
[Link]();
}
}
}
4. Write a Java class called SimpleInterest with methods for calculating simple
interest. Have this class as a servant and create a server program and register in
the rmiregistry. Write a client program to invoke these remote methods of the
servant and do the calculations. Accept inputs at command prompt.
[Link]
import [Link];
import [Link];
public interface SimpleInterest extends Remote {
double calculateSimpleInterest(double principal, double rate, double time)
throws RemoteException;
}
[Link]
import [Link];
import [Link];
public class SimpleInterestImpl extends UnicastRemoteObject implements SimpleInterest {
protected SimpleInterestImpl() throws RemoteException {
super();
}
@Override
public double calculateSimpleInterest(double principal, double rate, double time) throws
RemoteException {
return (principal * rate * time) / 100;
}
}
[Link]
import [Link];
import [Link];
public class SimpleInterestServer {
public static void main(String[] args) {
try {
SimpleInterest simpleInterest = new SimpleInterestImpl();
[Link](1098); // Start RMI registry on port 1098
[Link]("//localhost/SimpleInterestService", simpleInterest); // Bind the remote object's
stub in the registry
[Link]("Server ready");
} catch (Exception e) {
[Link]("Server exception: " + [Link]());
[Link]();
}
}
}
[Link]
import [Link];
public class SimpleInterestClient {
public static void main(String[] args) {
try {
SimpleInterest simpleInterest = (SimpleInterest)
[Link]("//localhost/SimpleInterestService");
double principal = [Link](args[0]);
double rate = [Link](args[1]);
double time = [Link](args[2]);
double result = [Link](principal, rate, time);
[Link]("Simple Interest: " + result);
} catch (Exception e) {
[Link]("Client exception: " + [Link]());
[Link]();
}
}
}