.:For Network Programming ( SCN 4223 ) Purpose Only:.

AC990856 Page

--»Assignment 7 --- RMI Airline

--Airline Interface

import java.rmi.*;

public interface AirlineInterface extends Remote
{
public int bookSeat(String FlightNo) throws RemoteException;
}
 
--Airline Server
 

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.util.*;
import java.sql.*;


public class AirlineServer extends UnicastRemoteObject implements AirlineInterface
{
String url="jdbc:odbc:airline";
Connection conn;
//server contructor---
public AirlineServer() throws RemoteException
{
System.out.println("Initializing the Server");
}
//the bookSeat() method---
public int bookSeat(String FlightNo)
{
int seatdtls=0;
System.out.println("Flight is"+FlightNo);

try
{
//Load the Driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

//Pass the static fields containing the data source name
conn=DriverManager.getConnection(url);

Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("Select Seats from Flight where FlightNo="+FlightNo+"");



while(rs.next())
{
seatdtls=rs.getInt("Seats");
}
}


catch(Exception excp){}

if (seatdtls>0)
return seatdtls;
else
return 0;
}//end of bookSeat() method


//Implement the main() method
public static void main(String args[])
{
try
{
//create the instance of the Server object to export
AirlineServer arlnServer=new AirlineServer();
Naming.rebind("airlineServ",arlnServer);

//airlineServ is the name under whisch the server regs. itself in the RMI regis.
System.out.println("Server Ready");
}//end try

catch(RemoteException RemoteExcp)
{
System.out.println("Remote Server Error:"+RemoteExcp.getMessage());
System.exit(0);
}//end catch1

catch(Exception excp)
{
System.out.println("Error :"+excp.getMessage());
System.exit(0);
}//end catch2
}//end of main() method
}
 

--Airline Client
 

import java.rmi.*;
import java.rmi.registry.*;
import java.awt.*;
import java.awt.event.*;


public class AirlineClient extends Frame implements ActionListener
{
Button btnBookSeat=new Button("Seat Availability");

//create labels
Label lblFlightNo=new Label("Flight No :");
Label lblResult=new Label("Result :");

//create txtarea
TextArea taResults=new TextArea();

//Create txt Items
TextField txtFlightNo=new TextField(20);


//Constructor of the AirlineClient
public AirlineClient()
{
super("Airline Reservation System(Remote Client-RMI)");
setLayout(null);

lblFlightNo.setBounds(20,50,55,25);
add(lblFlightNo);
txtFlightNo.setBounds(150,50,100,25);
add(txtFlightNo);

lblResult.setBounds(20,155,100,25);
add(lblResult);
taResults.setBounds(150,155,400,200);
add(taResults);

btnBookSeat.setBounds(175,375,250,25);
add(btnBookSeat);

//add actionlistener button
btnBookSeat.addActionListener(this);

enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}//end of const. airlineClient


public void processWindowEvent(WindowEvent winEvt)
{
if(winEvt.getID()==WindowEvent.WINDOW_CLOSING)
{
setVisible(false);
dispose();
System.exit(0);
}
}//end processWindowEvent


//To trap events for each button
public void actionPerfomed(ActionEvent actEvt)
{
if(actEvt.getSource()==btnBookSeat)
{
seatcheck();
}
}//end actionPerfomed

public void seatcheck()
{
String Flight=txtFlightNo.getText();
int val;

try
{
//String ServerURL="//159.254.151.112/airlineServ";
String ServerURL="//131.100.2.712/airlineServ";
AirlineInterface allntf=(AirlineInterface)Naming.lookup(ServerURL);
val=allntf.bookSeat(Flight);

if(val==0)
{
taResults.setText("No seats available on flight "+Flight);
}
else
{
taResults.setText("The seats available on flight "+Flight+" are "+val);

}
}//end try

catch(Exception excp)
{
System.out.println("Exception :"+excp);
}//end catch
}//end seatcheck


//AirlineClient main() method
public static void main(String[] args)
{
//invoke the con of the AirlineClient class
AirlineClient rmtAirline=new AirlineClient();
rmtAirline.setVisible(true);
rmtAirline.setSize(600,500);
}

public void actionPerformed(java.awt.event.ActionEvent actionEvent)
{
}

//end main
}//end class AirlineClient
 

Copyright @2003 Ac990856
U6C3-9,Kolej Perdana.
Tel: 6012-7615174 ;
e-mail: masalapang@hotmail.com.my