Remote Objects for Database Access
Pass remote objects from the server to the client. The client will receive the stub object (through remote interfaces) and saves it in an object variable with the same type as the remote interface. Then the client can access the actual object on the server through the variable. Make use of JDBC and RMI for accessing multiple data access objects.
A) Retrieve the list of books available in the library.
B) Retrieve the MTNL billing information form the MTNL database
C) Retrieve the Student Information from Student database.
(A) Retrieve the list of books available in the library.
Code:
LibraryIntf.java
LibraryImpl.java
LibraryClient.java
LibraryServer.java
Output:
(B) Retrieve the MTNL billing information form the MTNL database
Code:
MtnlIntf.java
MtnlImpl.java
MtnlClient.java
MtnlServer.java
Output:
(C) Retrieve the Student Information from Student database.
Code:
StudentIntf.java
StudentImpl.java
StudentClient.java
StudentServer.java
Output:
Pass remote objects from the server to the client. The client will receive the stub object (through remote interfaces) and saves it in an object variable with the same type as the remote interface. Then the client can access the actual object on the server through the variable. Make use of JDBC and RMI for accessing multiple data access objects.
A) Retrieve the list of books available in the library.
B) Retrieve the MTNL billing information form the MTNL database
C) Retrieve the Student Information from Student database.
(A) Retrieve the list of books available in the library.
Code:
LibraryIntf.java
import java.rmi.*;
import java.net.*;
import java.sql.*;
interface LibraryIntf extends Remote
{
public String[] getResult()throws RemoteException;
public int getCount()throws RemoteException;
}
import java.net.*;
import java.sql.*;
interface LibraryIntf extends Remote
{
public String[] getResult()throws RemoteException;
public int getCount()throws RemoteException;
}
LibraryImpl.java
import java.rmi.*;
import java.net.*;
import java.rmi.server.*;
import java.sql.*;
import java.io.*;
class LibraryImpl extends UnicastRemoteObject implements LibraryIntf,Serializable
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
ResultSetMetaData rsmd;
int cnt=0;
public LibraryImpl()throws RemoteException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:mydsn1");
stmt=con.createStatement();
}
catch (Exception ar)
{
}
}
int getCounty()
{
int count=0;
try
{
ResultSet rs1=stmt.executeQuery("Select * From Book");
while(rs1.next())
{
count++;
}
}
catch (Exception att)
{
}
return count;
}
public String[] getResult()
{
int county=getCounty();
String str[]=new String[county];
try
{
rs=stmt.executeQuery("Select * From Book");
//stmt.executeUpdate("Insert into Stud values ('abc',45,'hhh')");
rsmd=rs.getMetaData();
int col=rsmd.getColumnCount();
int j=0;
int count=0;
String temp="";
while(rs.next())
{
cnt++;
str[j]="";
for(int i=1;i<=col;i++)
{
temp=rs.getString(i);
str[j]=str[j]+temp+"\t";
}
j++;
}
}
catch (Exception ass)
{
}
return str;
}
public int getCount()
{
return cnt;
}
}
import java.net.*;
import java.rmi.server.*;
import java.sql.*;
import java.io.*;
class LibraryImpl extends UnicastRemoteObject implements LibraryIntf,Serializable
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
ResultSetMetaData rsmd;
int cnt=0;
public LibraryImpl()throws RemoteException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:mydsn1");
stmt=con.createStatement();
}
catch (Exception ar)
{
}
}
int getCounty()
{
int count=0;
try
{
ResultSet rs1=stmt.executeQuery("Select * From Book");
while(rs1.next())
{
count++;
}
}
catch (Exception att)
{
}
return count;
}
public String[] getResult()
{
int county=getCounty();
String str[]=new String[county];
try
{
rs=stmt.executeQuery("Select * From Book");
//stmt.executeUpdate("Insert into Stud values ('abc',45,'hhh')");
rsmd=rs.getMetaData();
int col=rsmd.getColumnCount();
int j=0;
int count=0;
String temp="";
while(rs.next())
{
cnt++;
str[j]="";
for(int i=1;i<=col;i++)
{
temp=rs.getString(i);
str[j]=str[j]+temp+"\t";
}
j++;
}
}
catch (Exception ass)
{
}
return str;
}
public int getCount()
{
return cnt;
}
}
LibraryClient.java
import java.rmi.*;
import java.net.*;
import java.sql.*;
import java.io.*;
class LibraryClient implements Serializable
{
public static void main(String args[])
{
try
{
ResultSet rs1;
ResultSetMetaData rsmd;
System.out.println("This is client screen");
LibraryIntf ob=(LibraryIntf)Naming.lookup("Lib");
String str[]=ob.getResult();
int colcnt=ob.getCount();
for(int i=0;i<colcnt;i++)
{
System.out.println(str[i]);
}
}
catch (Exception att)
{
System.out.println(att.toString()+"");
}
}}
import java.net.*;
import java.sql.*;
import java.io.*;
class LibraryClient implements Serializable
{
public static void main(String args[])
{
try
{
ResultSet rs1;
ResultSetMetaData rsmd;
System.out.println("This is client screen");
LibraryIntf ob=(LibraryIntf)Naming.lookup("Lib");
String str[]=ob.getResult();
int colcnt=ob.getCount();
for(int i=0;i<colcnt;i++)
{
System.out.println(str[i]);
}
}
catch (Exception att)
{
System.out.println(att.toString()+"");
}
}}
LibraryServer.java
import java.rmi.*;
import java.net.*;
import java.rmi.server.*;
import java.io.*;
class LibraryServer implements Serializable
{
public static void main(String args[])
{
try
{
LibraryImpl ob1=new LibraryImpl();
Naming.rebind("Lib",ob1);
}
catch (Exception ar)
{
}
}
}
import java.net.*;
import java.rmi.server.*;
import java.io.*;
class LibraryServer implements Serializable
{
public static void main(String args[])
{
try
{
LibraryImpl ob1=new LibraryImpl();
Naming.rebind("Lib",ob1);
}
catch (Exception ar)
{
}
}
}
Output:
(B) Retrieve the MTNL billing information form the MTNL database
Code:
MtnlIntf.java
import java.rmi.*;
import java.net.*;
import java.sql.*;
interface MtnlIntf extends Remote
{
public String[] getResult()throws RemoteException;
public int getCount()throws RemoteException;
}
import java.net.*;
import java.sql.*;
interface MtnlIntf extends Remote
{
public String[] getResult()throws RemoteException;
public int getCount()throws RemoteException;
}
MtnlImpl.java
import java.rmi.*;
import java.net.*;
import java.rmi.server.*;
import java.sql.*;
import java.io.*;
class MtnlImpl extends UnicastRemoteObject implements MtnlIntf,Serializable
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
ResultSetMetaData rsmd;
int cnt=0;
public MtnlImpl()throws RemoteException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:mydsn2");
stmt=con.createStatement();
}
catch (Exception ar)
{
}
}
int getCounty()
{
int count=0;
try
{
ResultSet rs1=stmt.executeQuery("Select * From MTNLTab");
while(rs1.next())
{
count++;
}
}
catch (Exception att)
{
}
return count;
}
public String[] getResult()
{
int county=getCounty();
String str[]=new String[county];
try
{
rs=stmt.executeQuery("Select * From MTNLTab");
//stmt.executeUpdate("Insert into Stud values ('abc',45,'hhh')");
rsmd=rs.getMetaData();
int col=rsmd.getColumnCount();
int j=0;
int count=0;
String temp="";
while(rs.next())
{
cnt++;
str[j]="";
for(int i=1;i<=col;i++)
{
temp=rs.getString(i);
str[j]=str[j]+temp+"\t";
}
j++;
}
}
catch (Exception ass)
{
}
return str;
}
public int getCount()
{
return cnt;
}
}
import java.net.*;
import java.rmi.server.*;
import java.sql.*;
import java.io.*;
class MtnlImpl extends UnicastRemoteObject implements MtnlIntf,Serializable
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
ResultSetMetaData rsmd;
int cnt=0;
public MtnlImpl()throws RemoteException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:mydsn2");
stmt=con.createStatement();
}
catch (Exception ar)
{
}
}
int getCounty()
{
int count=0;
try
{
ResultSet rs1=stmt.executeQuery("Select * From MTNLTab");
while(rs1.next())
{
count++;
}
}
catch (Exception att)
{
}
return count;
}
public String[] getResult()
{
int county=getCounty();
String str[]=new String[county];
try
{
rs=stmt.executeQuery("Select * From MTNLTab");
//stmt.executeUpdate("Insert into Stud values ('abc',45,'hhh')");
rsmd=rs.getMetaData();
int col=rsmd.getColumnCount();
int j=0;
int count=0;
String temp="";
while(rs.next())
{
cnt++;
str[j]="";
for(int i=1;i<=col;i++)
{
temp=rs.getString(i);
str[j]=str[j]+temp+"\t";
}
j++;
}
}
catch (Exception ass)
{
}
return str;
}
public int getCount()
{
return cnt;
}
}
MtnlClient.java
import java.rmi.*;
import java.net.*;
import java.sql.*;
import java.io.*;
class MtnlClient implements Serializable
{
public static void main(String args[])
{
try
{
ResultSet rs1;
ResultSetMetaData rsmd;
System.out.println("This is client screen");
MtnlIntf ob=(MtnlIntf)Naming.lookup("mtnl");
String str[]=ob.getResult();
int colcnt=ob.getCount();
for(int i=0;i<colcnt;i++)
{
System.out.println(str[i]);
}
}
catch (Exception att)
{
System.out.println(att.toString()+"");
}
}
}
import java.net.*;
import java.sql.*;
import java.io.*;
class MtnlClient implements Serializable
{
public static void main(String args[])
{
try
{
ResultSet rs1;
ResultSetMetaData rsmd;
System.out.println("This is client screen");
MtnlIntf ob=(MtnlIntf)Naming.lookup("mtnl");
String str[]=ob.getResult();
int colcnt=ob.getCount();
for(int i=0;i<colcnt;i++)
{
System.out.println(str[i]);
}
}
catch (Exception att)
{
System.out.println(att.toString()+"");
}
}
}
MtnlServer.java
import java.rmi.*;
import java.net.*;
import java.rmi.server.*;
import java.io.*;
class MtnlServer implements Serializable
{
public static void main(String args[])
{
try
{
MtnlImpl ob1=new MtnlImpl();
Naming.rebind("mtnl",ob1);
}
catch (Exception ar)
{
}
}
}
import java.net.*;
import java.rmi.server.*;
import java.io.*;
class MtnlServer implements Serializable
{
public static void main(String args[])
{
try
{
MtnlImpl ob1=new MtnlImpl();
Naming.rebind("mtnl",ob1);
}
catch (Exception ar)
{
}
}
}
Output:
(C) Retrieve the Student Information from Student database.
Code:
StudentIntf.java
import java.rmi.*;
import java.net.*;
import java.sql.*;
interface StudentIntf extends Remote
{
public String[] getResult()throws RemoteException;
public int getCount()throws RemoteException;
}
import java.net.*;
import java.sql.*;
interface StudentIntf extends Remote
{
public String[] getResult()throws RemoteException;
public int getCount()throws RemoteException;
}
StudentImpl.java
import java.rmi.*;
import java.net.*;
import java.rmi.server.*;
import java.sql.*;
import java.io.*;
class StudentImpl extends UnicastRemoteObject implements StudentIntf,Serializable
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
ResultSetMetaData rsmd;
int cnt=0;
public StudentImpl()throws RemoteException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:mydsn");
stmt=con.createStatement();
}
catch (Exception ar)
{
}
}
int getCounty()
{
int count=0;
try
{
ResultSet rs1=stmt.executeQuery("Select * From Stud");
while(rs1.next())
{
count++;
}
}
catch (Exception att)
{
}
return count;
}
public String[] getResult()
{
int county=getCounty();
String str[]=new String[county];
try
{
rs=stmt.executeQuery("Select * From Stud");
//stmt.executeUpdate("Insert into Stud values ('abc',45,'hhh')");
rsmd=rs.getMetaData();
int col=rsmd.getColumnCount();
int j=0;
int count=0;
String temp="";
while(rs.next())
{
cnt++;
str[j]="";
for(int i=1;i<=col;i++)
{
temp=rs.getString(i);
str[j]=str[j]+temp+"\t";
}
j++;
}
}
catch (Exception ass)
{
}
return str;
}
public int getCount()
{
return cnt;
}
}
import java.net.*;
import java.rmi.server.*;
import java.sql.*;
import java.io.*;
class StudentImpl extends UnicastRemoteObject implements StudentIntf,Serializable
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
ResultSetMetaData rsmd;
int cnt=0;
public StudentImpl()throws RemoteException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:mydsn");
stmt=con.createStatement();
}
catch (Exception ar)
{
}
}
int getCounty()
{
int count=0;
try
{
ResultSet rs1=stmt.executeQuery("Select * From Stud");
while(rs1.next())
{
count++;
}
}
catch (Exception att)
{
}
return count;
}
public String[] getResult()
{
int county=getCounty();
String str[]=new String[county];
try
{
rs=stmt.executeQuery("Select * From Stud");
//stmt.executeUpdate("Insert into Stud values ('abc',45,'hhh')");
rsmd=rs.getMetaData();
int col=rsmd.getColumnCount();
int j=0;
int count=0;
String temp="";
while(rs.next())
{
cnt++;
str[j]="";
for(int i=1;i<=col;i++)
{
temp=rs.getString(i);
str[j]=str[j]+temp+"\t";
}
j++;
}
}
catch (Exception ass)
{
}
return str;
}
public int getCount()
{
return cnt;
}
}
StudentClient.java
import java.rmi.*;
import java.net.*;
import java.sql.*;
import java.io.*;
class StudentClient implements Serializable
{
public static void main(String args[])
{
try
{
ResultSet rs1;
ResultSetMetaData rsmd;
System.out.println("This is client screen");
StudentIntf ob=(StudentIntf)Naming.lookup("Student");
String str[]=ob.getResult();
int colcnt=ob.getCount();
for(int i=0;i<colcnt;i++)
{
System.out.println(str[i]);
}
}
catch (Exception att)
{
System.out.println(att.toString()+"");
}
}
}
import java.net.*;
import java.sql.*;
import java.io.*;
class StudentClient implements Serializable
{
public static void main(String args[])
{
try
{
ResultSet rs1;
ResultSetMetaData rsmd;
System.out.println("This is client screen");
StudentIntf ob=(StudentIntf)Naming.lookup("Student");
String str[]=ob.getResult();
int colcnt=ob.getCount();
for(int i=0;i<colcnt;i++)
{
System.out.println(str[i]);
}
}
catch (Exception att)
{
System.out.println(att.toString()+"");
}
}
}
StudentServer.java
import java.rmi.*;
import java.net.*;
import java.rmi.server.*;
import java.io.*;
class StudentServer implements Serializable
{
public static void main(String args[])
{
try
{
StudentImpl ob1=new StudentImpl();
Naming.rebind("Student",ob1);
}
catch (Exception ar)
{
}
}
}
import java.net.*;
import java.rmi.server.*;
import java.io.*;
class StudentServer implements Serializable
{
public static void main(String args[])
{
try
{
StudentImpl ob1=new StudentImpl();
Naming.rebind("Student",ob1);
}
catch (Exception ar)
{
}
}
}
Output:
0 comments:
Confused? Feel free to ask
Post a Comment