Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to return resultset from server to client in rmi

 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hii, i am writing a application, in which i have to transfer resultset from server to client to display data from database, how can i do that, if i m trying to return resulset directly it showing me an exception.
do i need to serialize it, if yes then how??
here is part of my code:
Server Part.


Client Part where i am fetching:


Part of the exception

java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.mysql.jdbc.JDBC4ResultSet
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at ImplementClass_Stub.displayData(Unknown Source)
at Client$1.actionPerformed(Client.java:46)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)


how do i solve this..?

is it possible, create a bean class and return the object? if yes than how??

Thank You
 
Tim Moores
Bartender
Posts: 2687
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ResultSets are not serializable, so you can't transport them over RMI. The only exception would be WebRowSet, but you probably don't want to use that.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what can i do??
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take the primitives out of the result set. Create your own Class that looks like this


Now you can pass MyClass back to your caller.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, i have done this, my values are displaying.
one thing i want to ask.
i have also added a next button to my frame, initially all my text fields showing first record from my db, what i want is when i click on next it should show me second record then third and so on, how can i do this??
in case of resultset, i can directly fetch resultset.next(), but how to do this with arraylist??

here is what i m doinf for displayin first record initially(when i run client.java).



this is the code which displaying 1st record from the database(when i click next), but i want it should display next record and so on...
how can i do this?
 
Madhavi Subramaniam
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jain,

To get second row of books table in SQL database, execute the following query

rs = st.executeQuery("with cte as (select t.*,row_number() over (order by (select 0)) as num from (SELECT * FROM BOOKS) t)select * from cte where num=2");

It will return 2nd record. Likewise you can get 3rd,4th,5th..... by using where condition num=3, num=4,num=5......






 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hii Madhavi Subramaniam, as you said,

rs = st.executeQuery("with cte as (select t.*,row_number() over (order by (select 0)) as num from (SELECT * FROM BOOKS) t)select * from cte where num=2");

but i don't want to execute query again and again, my all data is in array list, if i am printing that on my system whole data is printing but i want, one by one record when i click next.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:but i don't want to execute query again and again, my all data is in array list, if i am printing that on my system whole data is printing but i want, one by one record when i click next.


Your desires are good and well founded. But you are asking the wrong question. which is causing you to chase for solutions that can't exist.

You can't serialize the ResultSet, so don't do that. And thus RMI is not your solution.

Write a server-side process (servlet, bean, etc.) that your client talks to. Pass messages using REST or similar simple, easy to debug protocol.

Have one message be "do the following select for me" which will return a nonce. and the first few rows of data, in CSV, JSON, etc.

Have the client then say "here is the nonce, get the next 20 rows"

repeat until done.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i m doing something like this, i have created a bean class, and returning values from database in arraylist and returning that arraylist, and on the client side i am fetching that list and displaying records, but how do i fetch next records from that??
here is what i am doing:
BookBean.java

client.java (part of code):

this is showing first record from the arraylist, but i want on each click it should show next record.
 
Madhavi Subramaniam
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jain,

you have done mistake in preparing list of BookBean.

The return type of display method should be List<BookBean>.

Keep your BookBean as separate class and have your display() method in separate class


public List<BookBean> display()
{
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,pass);
Statement st = conn.createStatement();
ResultSet rs;
rs = st.executeQuery("SELECT * FROM books");
List<BookBean> aa = new ArrayList<BookBean>();
while(rs.next()){
BookBean book=new BookBean();
book.setBook_id(rs.getInt(1));
book.setBook_name(rs.getString(2));
book.setBook_publisher(rs.getString(3));
book.setBook_price(rs.getInt(4));
aa.add(book);

}
return aa;

}catch(Exception e){
e.printStackTrace();
return null;
}
}
}

when you call display method, it returns the list of book details what you have in book table.

List<BookBean> fetch = bb.display();

Then you can get 1st book details by

txtid.setText(fetch(0).getBook_id);
txtName.setText(fetch(0).getBook_Name);
txtPublisher.setText(fetch(0).getBook_Publisher);
txtPrice.setText(fetch(0).getBook_Price);

Likewise you can get 2nd book details by fetch(1),3rd fetch(2),etc...,
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hii Madhavi Subramaniam,
This particular block of code showing me error.


error is:-
The method fetch(int) is undefined for the type new ActionListener(){}

i am keeping the display method in separate class.
here is what i did..

i keep my display method in ImplementClass.

 
Madhavi Subramaniam
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Punit Jain,

Check out you getting list of books in fetch after cc.display() execution.

fetch is arraylist variable... I don't know why you getting undefined method error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic