Two Laptop Bag
The moose likes Distributed Java and the fly likes RMI Object Serialization problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Distributed Java
Reply Bookmark "RMI Object Serialization problem" Watch "RMI Object Serialization problem" New topic
Author

RMI Object Serialization problem

John Banerjee
Greenhorn

Joined: Mar 22, 2005
Posts: 1
Hi, my name is John.I am registered user of javaranch.com
I am facing a problem of RMI Object Serialization.From a server program I want to pass an Object as a parameter but I can't serialize the object.Plz help me.Send me a good material on Object Serialization.

Interface
--------------
import java.rmi.*;
import java.sql.*;
public interface Interface2 extends Remote
{
String getcustid(String s1) throws RemoteException;

ResultSet display() throws RemoteException;
}

Server program
-----------------
import java.io.Serializable;
import java.sql.*;
import java.rmi.*;
import java.rmi.server.*;


public class JdbcServer extends UnicastRemoteObject implements Interface2,Serializable
{

Connection c;
Statement s;
transient ResultSet r;
String s2;

public String getcustid(String s1) throws RemoteException
{
s2=s1;
return s2;
}
public ResultSet display() throws RemoteException
{

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c=DriverManager.getConnection("Jdbc dbc:EBANKING","db2admin","db2admin");
s=c.createStatement();
r=s.executeQuery("SELECT * from CURRENTAC,BRANCH where CURRENTAC.BRANCHID=BRANCH.BRANCHID AND CURRENTAC.CUSTOMERID='" +s2+ "'");

}catch(Exception e1){System.out.println(e1);}

return r;

}



JdbcServer() throws RemoteException
{}

public static void main(String args[])
{
try{

JdbcServer js = new JdbcServer();
System.out.println("connecting to server.....");
Naming.rebind("rmi://localhost:1099/Balanceenq",js);
System.out.println("Server Registered and Ready");
}catch(Exception e){System.out.println(e);}
}
}
Client Program
--------------------
import java.rmi.*;
import java.sql.*;
import java.io.*;

public class JdbcClient
{


public static void main(String args[])
{

ResultSet Result;

try{

Interface2 ref=(Interface2)Naming.lookup("rmi://localhost:1099/Balanceenq");
ref.getcustid("john_p2");
Result=ref.display();

while(Result.next())
{
System.out.println("Account Number::" +Result.getString("ACCNO"));
System.out.println("Balance::" +Result.getString("BALANCE"));
System.out.println("Branch id::" +Result.getString("BRANCHID"));
System.out.println("---------------------------------------");
}

}catch(Exception e){System.out.println(e);}
}
}
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Here's the problem... ResultSet isn't serializable, so you can't return it from your remote method. You'll need to do something like this for it to work -

Make a class that implements Serializable. Have a field in this class for each column you are concerned with in the tables that you are querying with JDBC.

Inside the display() method, you'll need to get the ResultSet. Then create a Serializable Collection - like an ArrayList. Loop through the ResultSet. Each time through the loop create a new object of the class you have created above and set it's values from the ResultSet. Add your new object to the ArrayList.

Once you are done looping through the ResultSet, return the ArrayList - it contains all the data from your query in the form of objects of the class you have created.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: RMI Object Serialization problem
 
Similar Threads
Why does RMI naming lookup work on the local host when I give it a garbage name?
How to Fetch The Record From the Client
Problem while starting rmi server
exception in client side
need help. I have problems running the client