• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RMI Object Serialization problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);}
}
}
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic