How to fetch complete data from the database's table through RMI using the array method?

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.
    }
}
java rmi
2021-11-24 05:53:25
1

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.

2021-11-24 08:26:23

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................