0

I want to fetch complete data from the database's table through RMI. I used the array method in the Java interface and I have implemented that method in  implementation class. My intention is to take the data in the array via implementation and show it via JTable on the client side. I have created a one-column table in the database. I have to get that whole data from that table to the client side.

I have attached the coding that I did. I have commented the errors in the code section that I got.

interface

public interface Interface extends Remote {
     public static String[] getArray() throws Remote Exception; // Here it shows missing method 
                                                               //  body or declare abstract
}

Implementation

public class TheImplementation extends UnicastRemoteObject implements Interface{
    
    public TheImplementation()throws Remote Exception{
        super();
    }
    
    private static final long serialVersionUID = -3763231206310559L;
    
    Connection con;
    PreparedStatement pst;
    ResultSet rst;

    public static String[] getArray() throws RemoteException{
        String fruitdetails = null; 
        try {
            Connection connection=ConnectionProvider.getConnection();
            Statement st=connection.createStatement();
            ResultSet rs=st.executeQuery("select *from details");
            while(rs.next()) { 
                fruitdetails= rs.getString("fruit");
                String tbData[]={fruitdetails};
            }
        }
        catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }
        return tbData;// Here it shows error. Cannot find symbol.
                           // I tried to declare array at top. But, It didn't work.
    }
}
  • 1
    `tbData` is declared in the while loop, so it's scope is only within the while loop. Your sql will throw an error when you get this to compile. Other `Interface` is not a good name for a Java Object. `con` `pst` and `rst` are declared but never used. Why have `getArray` as a `static` method and why bother to interface it? – Scary Wombat Nov 24 '21 at 06:04
  • Connection, Statement and ResultSet are never closed. JOptionPane is used on server side to display error messages. – dpr Nov 24 '21 at 06:06
  • Refer to [Java doc for Scope of variables](https://stackoverflow.com/questions/39867972/java-doc-for-scope-of-variables) – Abra Nov 24 '21 at 06:08

1 Answers1

0

Abstract methods in remote interfaces cannot be static, so you need to change the interface definition to the following.

public interface Interface extends java.rmi.Remote {
    public String[] getArray() throws RemoteException;
}

Values returned by remote methods must be serializable. Arrays in java are serializable, however arrays have a fixed size and since you are returning the result of a database query you cannot know the size. Hence I suggest that method getArray return an ArrayList or better yet, a CachedRowSet.

public interface Interface extends Remote {
    public CachedRowSet getArray() throws RemoteException;
}

Since class TheImplementation is your RMI server class, it is probably better to log exceptions rather than display a JOptionPane and you should always log the stack trace. Note that remote methods must declare that they throw RemoteException in order to inform the RMI client that the remote method failed. Hence apart from logging the exception, method getArray can also throw a RemoteException.

The following code demonstrates.

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;

public class TheImplementation extends UnicastRemoteObject implements Interface {

    public TheImplementation() throws RemoteException {
        super();
    }

    private static final long serialVersionUID = -3763231206310559L;

    public CachedRowSet getArray() throws RemoteException {
        try (Connection con = ConnectionProvider.getConnection();
             Statement st = con.createStatement();
             ResultSet rs = st.executeQuery("select * from details")) {
            RowSetFactory factory = RowSetProvider.newFactory();
            CachedRowSet fruitDetails = factory.createCachedRowSet();
            fruitDetails.populate(rs);
            return fruitDetails;
        }
        catch (SQLException e) {
            throw new RemoteException("Method 'getArray()' failed.", e);
        }
    }
}

Note that the above code also uses try-with-resources to ensure that the ResultSet, Statement and Connection are all closed.

Abra
  • 19,142
  • 7
  • 29
  • 41