[英]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.