Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

RMI Object Serialization problem  RSS feed

 
John Banerjee
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!