-1

The idea is to pass a query to a remote method, have that remote method execute it and let it send the result set back to the calling method.

Here is the caller method

btnOK.setOnAction((e)-> {
    String query = "SELECT * FROM schoolmanagement.director;";
    System.out.println("Calling RMI");
    try {
        Registry registry = LocateRegistry.getRegistry("localhost");
        RMIInterface rmi = (RMIInterface)registry.lookup("remoteObject");
        System.out.println(rmi.Query(query));
    } catch (RemoteException e2) {
        e2.printStackTrace();
    } catch (NotBoundException e1) {
        e1.printStackTrace();
    }
});

And here is the remote method

public String Query(String query) throws RemoteException {
    try {
        Connection mycon = DriverManager.getConnection("jdbc:mysql://localhost:3306/schoolmanagement", "root", "root");
        Statement mystatement = mycon.createStatement();
        ResultSet rs = mystatement.executeQuery(query);
        while(rs.next()){
            System.out.println(rs.getString("Name") + " "+ rs.getString("Age"));
        } 
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

}

I know that rs is not serializable. How do I work around this?

  • It isn't a `String` either. Not making much sense. – user207421 Feb 12 '17 at 10:36
  • @EJP return rs is just until I get a solution. I'm not actually trying to return an object while the method is of type String. –  Feb 12 '17 at 10:49
  • So what are you talking about? Your title says pass `ResultSet` *to* a remote method, your question says return it *from* a remote method, and your code doesn't do either. – user207421 Feb 12 '17 at 22:34
  • @EJP It was my mistake. Ill change the title to " returning a result set from a remote method". –  Feb 13 '17 at 05:42

1 Answers1

3

ResultSet is not serializable because it does not actually contain all the results, it is more accurate to think of it as an iterator or a pointer into a list.

So in order to send the results over RMI you will have to actually extract the results and store them in for example a list of records (or list of Director objects in your case). Make sure those objects are all serializable.

john16384
  • 7,800
  • 2
  • 30
  • 44