Link to home
Start Free TrialLog in
Avatar of kllewelyn
kllewelyn

asked on

RMI using JDBC to pass back a ResultSet

I'm trying to make an RMI database object so the client can just pass an sql to the server and the server object will return a ResultSet record.  Is this possible? Basically I'm trying to remove any need for JDBC on the client side.

Thanks.
Avatar of doronb
doronb

Hi,


I've done exactly the same thing, only in my implementation, the client doesn't even know anything about the SQL's running in the DB. The client simply sends requests through an RMI component which then translates these requests to SQL queries which are executed through JDBC by the RMI component. Instead of passing ResultSets I pass Vectors or Hashtables as responses. The client simply uses the RMI-DB component as just another component w/o knowing there's even a DB behind the scenes.


Doron
Avatar of Jim Cakalic
You might want to consider using the CachedRowSet -- one of the JDBC RowSet interface implementations from Sun. It is specifically intended for the purposes of "send a set of rows across a network" and "send data to a thin client". The client receiving the RowSet accesses it like a ResultSet (which RowSet extends). As such, it doesn't completely eliminate JDBC from the client but eliminates the database access that would normally occur when using JDBC calls.

Jim
Avatar of kllewelyn

ASKER

doronb, so the Vector that gets passed back then need to be manipulated on the client side to view the contents correctly? How do you handle the Vector once the client receives it?  I'm wanting to take the Vector/Recordset and toss it into a JTable to display the results.
ASKER CERTIFIED SOLUTION
Avatar of doronb
doronb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
kllewelyn>>

Just a clarification. You cannot pass a ResultSet over an RMI connection. The immediate reason beeing that ResultSets are not serializable. As you may or may not know, RMI uses the JAVA serialization functionality in order to pass objects between server and client. The main reason why you can't serialize a resultset is because it holds internal references and state information about the original connection to the DB.

By default, most JDBC drivers doesn't populate a resultset, but rather maintains a cursor used to fetch information from the db as you're traversing the resultset (rs.next()). This cursor is only valid in the open session (ie connection) to the db.

Because of this you have to actully fetch all the data from the resultset move it to a serializable structure (ie Vector, HashMap or a custom object structure).

doronb gave you the proper sollution. I just thought I'd try and clarify the reason. If you need more help/clarification, don't hesitate to ask

Good luck
Or, as I indicated, use a CachedRowSet to achieve serializability of a ResultSet. If you already intend to construct a collection of data objects using the ResultSet at the client, then doing so on the server is no additional implementation overhead. But if you would really rather have a ResultSet-like object on the client then CachedRowSet would allow you to send the query results without the intermediate data-object and collection building.

Looks like I forgot the link in my first post (sorry):
    http://developer.java.sun.com/developer/earlyAccess/crs/

Jim