[英]Sending and receiving an Object from an RMI Server and Client using seralization
我正在實施銀行系統,並且想將ResultSet
發送給客戶端。 但是Java向我顯示了一個錯誤。
public interface SekelatonInterface extends Remote {
public String test() throws RemoteException; // this is ok it works fine
public ConnectionClass getConnection() throws RemoteException; //shows error on client call
public ResultSet getAllDeposits(Integer CustomerId) throws RemoteException;
}
public class SekelatonImpl extends UnicastRemoteObject implements SekelationInterface {
SekelatonImpl() throws RemoteException{
}
//sekelaton implemeation
public ConnectionClass getConnection() {
try {
dbobject = new ConnectionClass();
dbobject.connectDb();
dbObject.setQuery("select * from cutomer");
return dbobject; //this method is on connection class ,dont be confuse
}
catch(Exception ex)
{
System.out.println("Error :"+ex.getMessage());
}
}
}
public class Server {
public void PackServerandRun(String SecurityFilePath,Integer port,String rmiUrl) {
//do rmi registery stuff and run server
SekelatonImpl databaseObject = new SekelationImpl(); // rebind this object
}
}
cleintstub.test(); //this recive the server message or works fine
cleintStub.getConnection(); //why couldn't i get a ConnectionClass Object ?
當我運行客戶端時,我看到的錯誤是:
注冊表查找有錯誤錯誤解組返回頭; 嵌套的異常是:java.io.EOFException
我想送一個
ResultSet
運氣不好,你不能。 ResultSet
不可Serializable
。
或連接到客戶端的對象,實際上后期並不是一個好主意。
這不僅是一個“好主意”,而且是一個完全沒有意義的主意。 您不能通過電話線發送電話。 同樣,您不能通過另一個連接發送連接。 更不用說Connection
也不可Serializable
的事實。
但是,Java向我顯示了一個錯誤。
下次您在任何地方發布問題報告時,請確保包括實際問題,包括錯誤消息, 逐字記錄,剪切粘貼,未手動轉錄的內容,以及源代碼的相關部分,並在必要時顯示行號,以及預期和實際輸出。
catch(){
}
永遠不要忽略異常。 否則,您會將調試變成純粹的猜測游戲。
好的,在我完成一些研究之后,我將嘗試回答我自己的問題。
錯誤是發送未序列化的對象。
因為,ResultSet不支持序列化,所以我還創建了一個Model類和該模型類對象的返回列表給客戶端。通過向要傳遞的每個類中添加Serialization作為參數,還包括骨架實現序列化,請注意所有類都需要在客戶端定義。
public class ConnectionClass implements java.io.Serializable {
}
public interface SekelatonInterface extends Remote ,java.io.Serializable{
public Login getTestClass()throws RemoteException;
public String test() throws RemoteException;
public List<Login> getConnection() throws RemoteException;
public void sayHitoServer(String messages) throws RemoteException;
}
public class SkeletonInterfaceImpl extends UnicastRemoteObject implements SekelatonInterface,Serializable {
//implement all your skeleton methods
}
//this to change result set into model class to transefer it to client
public class Login {
private int Id;
private String Username;
private String Password;
public Login() {
}
public int getId() {
return Id;
}
public void setId(int Id) {
this.Id = Id;
}
public String getUsername() {
return Username;
}
public void setUsername(String Username) {
this.Username = Username;
}
public String getPassword() {
return Password;
}
public void setPassword(String Password) {
this.Password = Password;
}
public Login(ResultSet resultset){
try{
// resultset.next();
Id = resultset.getInt("LoginId");
Username =resultset.getString("Uname");
Password = resultset.getString("Password");
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, "Error Setting Up Login"+ex.getMessage());
}
}
}
所有上述方法都可以正常工作。但是,這里的問題是我想將模型類的列表綁定到jtable模型。如何傳遞適合TableModel的列?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.