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?