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

Assignment 6

Uploaded by

Karan Margaje
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)
7 views

Assignment 6

Uploaded by

Karan Margaje
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/ 16

SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

6. Make use of JDBC and RMI for accessing multiple data access objects.

a) Retrieve Information from a Inventory database

b) Retrieve information from a Employee database

c) Retrieve Information using any Utility Payment database

Code:

Ijbc.java

package RMI_JDBC;

import java.rmi.*;

/**

* @author Karan

*/

public interface ijdbc extends Remote

public String getdata(String dburl,String dbquery,int opt) throws RemoteException;

Jdbcimpl.java

package RMI_JDBC;

import java.rmi.*;

import java.rmi.server.UnicastRemoteObject;

import java.sql.*;

/**

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

* @author Karan

*/

public class jdbcimpl extends UnicastRemoteObject implements ijdbc

public jdbcimpl() throws RemoteException

super();

@Override

public String getdata(String dburl, String dbquery,int opt) throws RemoteException {

String res = " ";

String pout = "";

String eout = "";

String upout = "";

try

if(dburl.equals("inventory"))

Class.forName("com.mysql.jdbc.Driver");

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dburl,"root","");

Statement stmt=con.createStatement();

if(opt == 1)

ResultSet rs1=stmt.executeQuery(dbquery);

while(rs1.next())

pout = pout+rs1.getString(1)+" | "+rs1.getString(2)+" |


"+rs1.getString(3)+"\n";

res = pout;

else

ResultSet rs2=stmt.executeQuery(dbquery);

while(rs2.next())

pout = pout+rs2.getString(1)+" | "+rs2.getString(2)+"\n";

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

res = pout;

con.close();

else if(dburl.equals("employee"))

Class.forName("com.mysql.jdbc.Driver");

Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dburl,"root","");

Statement stmt=con.createStatement();

if(opt == 1)

ResultSet rs3=stmt.executeQuery(dbquery);

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

while(rs3.next())

eout = eout+rs3.getString(1)+" | "+rs3.getString(2)+" |


"+rs3.getString(3)+"\n";

res = eout;

else

ResultSet rs4=stmt.executeQuery(dbquery);

while(rs4.next())

eout = eout+rs4.getString(1)+" | "+rs4.getString(2)+" |


"+rs4.getString(3)+"\n";

res = eout;

con.close();

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

else

Class.forName("com.mysql.jdbc.Driver");

Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dburl,"root","");

Statement stmt=con.createStatement();

if(opt == 1)

ResultSet rs5=stmt.executeQuery(dbquery);

while(rs5.next())

upout = upout+rs5.getString(1)+" | "+rs5.getString(2)+" |


"+rs5.getString(3)+"\n";

res = upout;

else

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

ResultSet rs6=stmt.executeQuery(dbquery);

while(rs6.next())

upout = upout+rs6.getString(1)+" | "+rs6.getString(2)+"\n";

res = upout;

con.close();

catch(Exception e)

System.out.println(e);

return res;

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Server.java

package RMI_JDBC;

import java.rmi.*;

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

/**

* @author Karan

*/

public class server extends jdbcimpl{

public server() throws RemoteException

public static void main(String args[]) throws RemoteException{

try

Registry reg = LocateRegistry.createRegistry(9999);

jdbcimpl s = new jdbcimpl();

reg.rebind("Server",s);

System.out.println("Server is ready!");

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

catch(Exception e)

System.out.println(e);

Client.java

package RMI_JDBC;

import java.io.*;

import java.util.*;

import java.rmi.*;

import java.rmi.registry.Registry;

import java.rmi.registry.LocateRegistry;

import java.sql.*;

/**

* @author Karan

*/

public class client {

public static void main(String args[]) throws RemoteException{

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

int choice,option;

String dbname,query;

Scanner scan =new Scanner(System.in);

try

Registry reg=LocateRegistry.getRegistry("127.0.0.1",9999);

ijdbc i = (ijdbc)reg.lookup("Server");

do

System.out.println("RMI JDBC Demo");

System.out.println("\n1. Inventory Database");

System.out.println("\n2. Employee Database");

System.out.println("\n3. Utility Payment Database");

System.out.println("\n4. Exit");

System.out.println("\nSelect database: ");

choice = scan.nextInt();

switch(choice)

//Inventory Option

case 1:

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

System.out.println("\n1. List Product Name, Product Line and Product


Vendor who belongs to product scale 1:12");

System.out.println("\n2. List Product Name and Product Vendor who's stock


quantity is between 2000 and 3000");

System.out.println("\nEnter your choice: ");

option = scan.nextInt();

if(option == 1)

dbname="inventory";

query="SELECT productName,productLine,productVendor FROM products


WHERE productScale = '1:12'";

System.out.println("Product Name | Product Line | ProductVendor\n");

System.out.println(i.getdata(dbname, query, option));

else

dbname="inventory";

query="SELECT productName,productVendor FROM products WHERE


quantityInStock BETWEEN 2000 AND 3000";

System.out.println("Product Name | Product Vendor\n");

System.out.println(i.getdata(dbname, query, option));

break;

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

//Employee Option

case 2:

System.out.println("\n1. List Employee first, last and Designation who


reports to Sales Manager Anthony");

System.out.println("\n2. List Employee first,last and Designation who


belongs to office code 6");

System.out.println("\nEnter your choice: ");

option = scan.nextInt();

if(option == 1)

dbname="employee";

query="SELECT firstName,lastName,jobTitle FROM emp WHERE reportsTo


= 1143";

System.out.println("First Name | Last Name | Job Title\n");

System.out.println(i.getdata(dbname, query, option));

else

dbname="employee";

query="SELECT firstName,lastName,jobTitle FROM emp WHERE officeCode


= 6";

System.out.println("First Name | Last Name | Job Title\n");

System.out.println(i.getdata(dbname, query, option));

break;

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

//Utility Option

case 3:

System.out.println("\n1. List Check Nmuber, Payment Date, Amount of


Customer Number 103");

System.out.println("\n2. List sum of total payment done by each customer");

System.out.println("\nEnter your choice: ");

option = scan.nextInt();

if(option == 1)

dbname="utility payment";

query="SELECT checkNumber,paymentDate,amount FROM payments


WHERE customerNumber = 103";

System.out.println("Check Number | Payment Date | Amount\n");

System.out.println(i.getdata(dbname, query, option));

else

dbname="utility payment";

query="SELECT customerNumber, SUM(amount) AS Total_Payment FROM


payments GROUP BY customerNumber ORDER BY Total_Payment LIMIT 0,10";

System.out.println("Check Number | Total Payment\n");

System.out.println(i.getdata(dbname, query, option));

break;

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

}while(choice != 4);

catch(Exception e)

System.out.println(e);

Output:

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020

You might also like