Saturday, January 16, 2010

Interface.java

import java.rmi.*; // Provides remote method invocation.
import java.sql.*; //Communicates with a database.

//creation of interface
public interface interfacestud extends Remote
{
void Connect() throws RemoteException;
void setData() throws RemoteException;

String[] getIDstud() throws RemoteException;
String[] getNamestud() throws RemoteException;
String[] getCitystud() throws RemoteException;

String[] getIDBook() throws RemoteException;
String[] getNameBook() throws RemoteException;
String[] getCostBook() throws RemoteException;
}


Implstud.java

import java.rmi.*; // Provides remote method invocation.
import java.rmi.server.*; // supports remote method invocation.
import java.sql.*; //Communicates with a database.
import java.io.*; //provides support for I/O operations

public class implstud extends UnicastRemoteObject implements interfacestud //implementation of interfacestud interface
{
//creation of resultset
ResultSet std=null;
ResultSet lib=null;

//variable declaration
int i=0;
String Idstud[]=new String[5];
String Namestud[]=new String[5];
String Citystud[]=new String[5];

String Idbook[]=new String[5];
String Namebook[]=new String[5];
String Costbook[]=new String[5];

Statement st,st1=null; //statement creation
public implstud() throws RemoteException
{
}

//connection establishment with database
public void Connect() throws RemoteException
{

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:stud");
st=con.createStatement();
st1=con.createStatement();

String sav1="Select * from stud";
String sav2="Select * from Library";

std=st.executeQuery(sav1);
lib=st1.executeQuery(sav2);

}catch(ClassNotFoundException e){}
catch(SQLException e){}

}
//method to insert data in database
public void setData() throws RemoteException
{
try
{

int i=0;
while(std.next())
{
Idstud[i]=std.getString(1);
Namestud[i]=std.getString(2);
Citystud[i]=std.getString(3);
i=i+1;
}
i=0;
while(lib.next())
{
Idbook[i]=lib.getString(1);
Namebook[i]=lib.getString(2);
Costbook[i]=lib.getString(3);
i=i+1;
}

}catch(Exception e){}

}

//methods to read data from database
public String[] getIDstud() throws RemoteException
{
return(Idstud);
}

public String[] getNamestud() throws RemoteException
{
return(Namestud);
}

public String[] getCitystud() throws RemoteException
{
return(Citystud);
}

public String[] getIDBook() throws RemoteException
{
return(Idbook);
}

public String[] getNameBook() throws RemoteException
{
return(Namebook);
}

public String[] getCostBook() throws RemoteException
{
return(Costbook);
}

}


Server.java

//server program with main method
import java.net.*;
import java.rmi.*; // Provides remote method invocation.
public class server
{
public static void main(String args[])
{
try
{
implstud dispImpl1 = new implstud();
Naming.rebind("Display1", dispImpl1);

implstud dispImpl2 = new implstud();
Naming.rebind("Display2", dispImpl2);
}
catch(Exception e)
{
System.out.println("Exception: " + e);
}
}

}


Client.java

import java.rmi.*;
import java.sql.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;



class dClient extends JFrame
{

JTabbedPane jtp=null;

dClient()
{
super("Database Details");


Container c = getContentPane();

jtp= new JTabbedPane();
jtp.addTab("Student Details", new StudentPanel());
jtp.addTab("Library", new LibraryPanel());
c.add(jtp);

setVisible(true);
setSize(850,850);
}

}




class StudentPanel extends JPanel implements ActionListener
{
JButton Save,Exit;

String Idstud[]=new String[5];
String Namestud[]=new String[5];
String Citystud[]=new String[5];

String[][] data=new String[5][3];
String[] colHeads = {"Id","Name","City"};

JScrollPane jsp=null;
JTable table=null;
int i=0;

//function to communicate with server
public StudentPanel()
{
try
{
interfacestud dispIntf =(interfacestud)Naming.lookup("rmi://localhost/Display1");

dispIntf.Connect();
dispIntf.setData();


Save=new JButton("Save");
Exit=new JButton("Exit");

Idstud=dispIntf.getIDstud();
Namestud=dispIntf.getNamestud();
Citystud=dispIntf.getCitystud();

for(i=0;i<=4;i++)
{
System.out.print("ID="+Idstud[i]);
System.out.print(" Name="+Namestud[i]);
System.out.print(" City="+Citystud[i]);
System.out.println("");
}
for(i=0;i<=4;i++)
{
for(int j=0;j<1;j++)
{
data[i][j]=Idstud[i];
data[i][j+1]=Namestud[i];
data[i][j+2]=Citystud[i];
}

}

}catch(Exception e){}


table = new JTable(data, colHeads);

int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
jsp = new JScrollPane(table, v, h);



add(jsp);
add(Save);
add(Exit);
Exit.addActionListener(this);
Save.addActionListener(this);

}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==Exit)
System.exit(0);
if(ae.getSource()==Save)
{

String space="\t";
String line="\n";
FileOutputStream fos=null;
DataOutputStream dos=null;
try
{

File in=new File("File1.txt");
fos=new FileOutputStream(in);

}catch(FileNotFoundException e){}


try
{
for(i=0;i<=4;i++)
{
for(int j=0;j<1;j++)
{

fos.write(data[i][j].getBytes());
fos.write(space.getBytes());
fos.write(data[i][j+1].getBytes());
fos.write(space.getBytes());
fos.write(data[i][j+2].getBytes());

}
fos.write(line.getBytes());

}
}catch(IOException e){}
}
}
}

class LibraryPanel extends JPanel implements ActionListener
{
JButton Save,Exit;

String Idbook[]=new String[5];
String Namebook[]=new String[5];
String Costbook[]=new String[5];

String[][] data=new String[5][3];
String[] colHeads = {"Id","Name","Cost"};

JScrollPane jsp=null;
JTable table=null;
int i=0;

public LibraryPanel()
{
try
{
interfacestud dispIntf =(interfacestud)Naming.lookup("rmi://localhost/Display2");
dispIntf.Connect();
dispIntf.setData();
Save=new JButton("Save");
Exit=new JButton("Exit");
Idbook=dispIntf.getIDBook();
Namebook=dispIntf.getNameBook();
Costbook=dispIntf.getCostBook();
for(i=0;i<=4;i++)
{
System.out.print("ID="+Idbook[i]);
System.out.print(" Name="+Namebook[i]);
System.out.print(" Cost="+Costbook[i]);
System.out.println("");
}

for(i=0;i<=4;i++)
{
for(int j=0;j<1;j++)
{
data[i][j]=Idbook[i];
data[i][j+1]=Namebook[i];
data[i][j+2]=Costbook[i];
}

}

}catch(Exception e){}


table = new JTable(data, colHeads);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
jsp = new JScrollPane(table, v, h);

add(jsp);
add(Save);
add(Exit);
Exit.addActionListener(this);
Save.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==Exit)
System.exit(0);
if(ae.getSource()==Save)
{
String space="\t";
String line="\n";
FileOutputStream fos=null;
DataOutputStream dos=null;
try
{
File in=new File("File2.txt");
fos=new FileOutputStream(in);

}catch(FileNotFoundException e){}
try
{
for(i=0;i<=4;i++)
{
for(int j=0;j<1;j++)
{
fos.write(data[i][j].getBytes());
fos.write(space.getBytes());
fos.write(data[i][j+1].getBytes());
fos.write(space.getBytes());
fos.write(data[i][j+2].getBytes()); }
fos.write(line.getBytes());

}
}catch(IOException e){}
}
}

}
//class of client program with main method
public class client
{
public static void main(String args[])
{
dClient sc=new dClient();
}
}

No comments:

Post a Comment