【问题标题】:Sending and receiving an Object from an RMI Server and Client using seralization使用序列化从 RMI 服务器和客户端发送和接收对象
【发布时间】:2013-05-24 09:51:34
【问题描述】:

我正在实施一个银行系统,我想向客户发送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

【问题讨论】:

  • 错误是什么?发布堆栈跟踪

标签: java serialization rmi


【解决方案1】:

我想发送ResultSet

运气不好,你不能。 ResultSet 不是 Serializable

或连接对象到客户端,其实后者不是好主意。

这不仅不是一个“好主意”,而且是一个完全没有意义的主意。您不能通过电话线发送电话。同样,您不能通过另一个连接发送连接。更不用说Connection 也不是Serializable

但是,Java 显示一个错误。

下次您在任何地方发布问题报告时,请确保包括实际问题,包括错误消息,逐字,剪切和粘贴,而不是手动与源代码的相关部分一起转录,必要时提供行号指示,以及预期和实际输出。

catch(){
   }

从不忽略异常。否则你会把调试变成一场猜谜游戏。

【讨论】:

  • 好的,谢谢!但是你建议发送一个数据库的结果实际上如何不返回结果集本身,而是如何使其可序列化?
  • 你可以从你的ResultSet创建一个Map<Integer, Map<String, Object>>,在外层用唯一键索引,在内层用列名索引;或者您可以查看创建 WebRowSet 并从中创建 XML 文档,并将其作为字符串返回;但以我的拙见,这是对 RMI 的不良使用。您应该将数据保存在服务器上,只需提供
【解决方案2】:

好的,在我解决了一些问题后,我会尝试回答我自己的问题。

错误是发送未序列化的对象。

由于,ResultSet 不支持序列化,我还创建了一个模型类并返回 模型类对象到客户端的列表。通过将序列化添加到您要作为参数传递的每个类,还包括您的骨架实现序列化,请注意,所有类都需要在客户端定义。

 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 的列?

【讨论】:

    猜你喜欢
    • 2017-12-24
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多