0% found this document useful (0 votes)
2 views

Advanced Java Lab PB1 to PB4

The document contains Java code for two applications: a Student Management System and a Bank Management System, both utilizing JDBC for database operations. It also includes RMI-based implementations for tax calculation and simple interest calculation, with server and client programs for each. The code demonstrates functionalities such as adding, deleting, and updating student and bank account records, as well as calculating taxes and simple interest remotely.

Uploaded by

Akash Akash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Advanced Java Lab PB1 to PB4

The document contains Java code for two applications: a Student Management System and a Bank Management System, both utilizing JDBC for database operations. It also includes RMI-based implementations for tax calculation and simple interest calculation, with server and client programs for each. The code demonstrates functionalities such as adding, deleting, and updating student and bank account records, as well as calculating taxes and simple interest remotely.

Uploaded by

Akash Akash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Advanced Java Lab– PART B- Program 01 to 04

package com.studentmgt;

import java.util.Scanner;
import java.sql.*;

public class StudentMgt {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection conn =
DriverManager.getConnection("jdbc:derby://localhost:1527/StudentDatabase", "vcp","vcp");
int choice;

do {
System.out.println("1. Add new Student");
System.out.println("2. Delete a specified student's Record");
System.out.println("3. Update Student's Address");
System.out.println("4. Search for a particular Student");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();

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:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);

conn.close();
scanner.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

static void addNewStudent(Connection conn, Scanner scanner) throws SQLException {


System.out.println("Enter Student Details:");
System.out.print("Registration Number: ");
int regNo = scanner.nextInt();
System.out.print("Name: ");
scanner.nextLine(); // Consume newline character
String sname = scanner.nextLine();
System.out.print("Date of Birth (YYYY-MM-DD): ");
String dob = scanner.nextLine();
System.out.print("Address: ");
String address = scanner.nextLine();
System.out.print("Class: ");
String class_ = scanner.nextLine();
System.out.print("Course: ");
String course = scanner.nextLine();

String sql = "INSERT INTO student (regno, sname, dob, address, class, course) VALUES (?, ?, ?,
?, ?, ?)";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setInt(1, regNo);
preparedStatement.setString(2, sname);
preparedStatement.setString(3, dob);
preparedStatement.setString(4, address);
preparedStatement.setString(5, class_);
preparedStatement.setString(6, course);
preparedStatement.executeUpdate();
System.out.println("Student added successfully!");
}

static void deleteStudentRecord(Connection conn, Scanner scanner) throws SQLException {


System.out.print("Enter Registration Number of the student to delete: ");
int regNo = scanner.nextInt();

String sql = "DELETE FROM Student WHERE regno = ?";


PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setInt(1, regNo);
int rowsDeleted = preparedStatement.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("Student record deleted successfully!");
} else {
System.out.println("No such student found with the given Registration Number.");
}
}
static void updateStudentAddress(Connection conn, Scanner scanner) throws SQLException {
System.out.print("Enter Registration Number of the student to update: ");
int regNo = scanner.nextInt();
scanner.nextLine(); // Consume newline character
System.out.print("Enter new Address: ");
String newAddress = scanner.nextLine();

String sql = "UPDATE Student SET address = ? WHERE regno = ?";


PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, newAddress);
preparedStatement.setInt(2, regNo);
int rowsUpdated = preparedStatement.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("Student address updated successfully!");
} else {
System.out.println("No such student found with the given Registration Number.");
}
}

static void searchStudent(Connection conn, Scanner scanner) throws SQLException {


System.out.print("Enter Registration Number of the student to search: ");
int regNo = scanner.nextInt();

String sql = "SELECT * FROM Student WHERE regno = ?";


PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setInt(1, regNo);
ResultSet resultSet = preparedStatement.executeQuery();

if (resultSet.next()) {
System.out.println("Student Details:");
System.out.println("Registration Number: " + resultSet.getInt("regno"));
System.out.println("Name: " + resultSet.getString("name"));
System.out.println("Date of Birth: " + resultSet.getString("dob"));
System.out.println("Address: " + resultSet.getString("address"));
System.out.println("Class: " + resultSet.getString("class"));
System.out.println("Course: " + resultSet.getString("course"));
} else {
System.out.println("No student found with the given Registration Number.");
}
}
}
import java.sql.*;
import java.util.Scanner;
import java.sql.*;

public class BankMgt {

public static void main(String[] args) {


Connection conn = null;
Scanner scanner = new Scanner(System.in); // Create a single Scanner object
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
conn = DriverManager.getConnection("jdbc:derby://localhost:1527/BankDB", "vcp", "vcp");

int choice;
do {
System.out.println("MENU");
System.out.println("1. Add new Account Holder information");
System.out.println("2. Amount Deposit");
System.out.println("3. Amount Withdrawal (Maintain minimum balance 500 Rs)");
System.out.println("4. Display all information");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();

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:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);

conn.close();
scanner.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

static void addNewAccountHolder(Connection conn, Scanner scanner) {


try {
System.out.print("Enter Account Number: ");
int accNo = scanner.nextInt();
scanner.nextLine(); // Consume newline character
System.out.print("Enter Account Holder Name: ");
String accHolderName = scanner.nextLine();
System.out.print("Enter Address: ");
String address = scanner.nextLine();
System.out.print("Enter Initial Balance: ");
double balance = scanner.nextDouble();

String sql = "INSERT INTO bank (accno, acname, address, balance) VALUES (?, ?, ?, ?)";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setInt(1, accNo);
preparedStatement.setString(2, accHolderName);
preparedStatement.setString(3, address);
preparedStatement.setDouble(4, balance);
int rowsInserted = preparedStatement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("New account holder information added successfully!");
} else {
System.out.println("Failed to add new account holder information.");
}
} catch (SQLException se) {
se.printStackTrace();
}
}

static void depositAmount(Connection conn, Scanner scanner) {


try {
System.out.print("Enter Account Number: ");
int accNo = scanner.nextInt();
System.out.print("Enter Deposit Amount: ");
double amount = scanner.nextDouble();

String sql = "UPDATE bank SET balance = balance + ? WHERE accno = ?";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setDouble(1, amount);
preparedStatement.setInt(2, accNo);
int rowsUpdated = preparedStatement.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("Amount deposited successfully!");
} else {
System.out.println("No account found with the given Account Number.");
}
} catch (SQLException se) {
se.printStackTrace();
}
}

static void withdrawAmount(Connection conn, Scanner scanner) {


try {
System.out.print("Enter Account Number: ");
int accNo = scanner.nextInt();
System.out.print("Enter Withdrawal Amount: ");
double amount = scanner.nextDouble();

String checkBalanceSql = "SELECT balance FROM bank WHERE accno = ?";


PreparedStatement checkBalanceStmt = conn.prepareStatement(checkBalanceSql);
checkBalanceStmt.setInt(1, accNo);
ResultSet resultSet = checkBalanceStmt.executeQuery();
if (resultSet.next()) {
double balance = resultSet.getDouble("balance");
if (balance - amount >= 500) {
String updateSql = "UPDATE bank SET balance = balance - ? WHERE accno = ?";
PreparedStatement updateStmt = conn.prepareStatement(updateSql);
updateStmt.setDouble(1, amount);
updateStmt.setInt(2, accNo);
int rowsUpdated = updateStmt.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("Amount withdrawn successfully!");
} else {
System.out.println("Failed to withdraw amount.");
}
} else {
System.out.println("Insufficient balance! Minimum balance of 500 Rs should be
maintained.");
}
} else {
System.out.println("No account found with the given Account Number.");
}
} catch (SQLException se) {
se.printStackTrace();
}
}

static void displayAllInformation(Connection conn) {


try {
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM bank";
ResultSet resultSet = stmt.executeQuery(sql);
while (resultSet.next()) {
System.out.println("Account Number: " + resultSet.getInt("accno"));
System.out.println("Account Name: " + resultSet.getString("acname"));
System.out.println("Address: " + resultSet.getString("address"));
System.out.println("Balance: " + resultSet.getDouble("balance"));
System.out.println("-----------------------------");
}
} catch (SQLException se) {
se.printStackTrace();
}
}
}
Tax.java

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Tax extends Remote {


double calculateTax(double income) throws RemoteException;
}

TaxImpl.java

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

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);
}
}
}
TaxServer.java

import java.rmi.Naming;

public class TaxServer {


public static void main(String[] args) {
try {
Tax tax = new TaxImpl();
Naming.rebind("TaxService", tax);
System.out.println("Tax service is ready...");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}

TaxClient.java

import java.rmi.Naming;
import java.util.Scanner;

public class TaxClient {


public static void main(String[] args) {
try {
Tax tax = (Tax) Naming.lookup("//localhost/TaxService");
Scanner scanner = new Scanner(System.in);
System.out.print("Enter income: ");
double income = scanner.nextDouble();
double taxAmount = tax.calculateTax(income);
System.out.println("Tax amount: " + taxAmount);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}

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.

SimpleInterest.java
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface SimpleInterest extends Remote {


double calculateSimpleInterest(double principal, double rate, double time)
throws RemoteException;
}

SimpleInterestImpl.java

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

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;
}
}

SimpleInterestServer.java

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

public class SimpleInterestServer {


public static void main(String[] args) {
try {
SimpleInterest simpleInterest = new SimpleInterestImpl();
LocateRegistry.createRegistry(1098); // Start RMI registry on port 1098
Naming.rebind("//localhost/SimpleInterestService", simpleInterest); // Bind the remote object's
stub in the registry
System.out.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
SimpleInterestClient.java

import java.rmi.Naming;

public class SimpleInterestClient {


public static void main(String[] args) {
try {
SimpleInterest simpleInterest = (SimpleInterest)
Naming.lookup("//localhost/SimpleInterestService");
double principal = Double.parseDouble(args[0]);
double rate = Double.parseDouble(args[1]);
double time = Double.parseDouble(args[2]);
double result = simpleInterest.calculateSimpleInterest(principal, rate, time);
System.out.println("Simple Interest: " + result);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}

You might also like