Come recuperare i dati completi dal database della tabella attraverso RMI utilizzando il metodo di matrice?

0

Domanda

Voglio recuperare i dati completi dal database della tabella attraverso RMI. Ho usato il metodo di matrice nell'interfaccia Java e ho implementato il metodo nella classe di implementazione. La mia intenzione è di prendere i dati nella matrice, tramite la realizzazione e mostra la via JTable sul lato client. Ho creato una tabella con una colonna nel database. Devo prendere quel complesso di dati dalla tabella a lato client.

Ho allegato la codifica che ho fatto. Ho commentato gli errori nella sezione di codice che ho ottenuto.

interfaccia

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

Attuazione

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

Migliore risposta

0

I metodi astratti in interfacce remote non può essere statico, in modo che è necessario modificare la definizione dell'interfaccia per il seguente.

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

Valori restituiti dai metodi remoti devono essere serializzabili. Gli array in java sono serializzabili, tuttavia gli array hanno una dimensione fissa e visto che sono di ritorno il risultato di una query di database, è possibile sapere le dimensioni. Quindi suggerisco che il metodo getArray restituire un ArrayList o, meglio ancora, un CachedRowSet.

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

Dal momento che la classe TheImplementation è il tuo RMI server di classe, è probabilmente meglio di registro eccezioni, piuttosto che mostrare un JOptionPane e si deve sempre effettuare l' analisi dello stack. Si noti che i metodi remoti deve dichiarare che buttare RemoteException al fine di informare il RMI client che il metodo remoto non riuscita. Quindi, a parte la registrazione eccezione, il metodo getArray può anche lanciare un RemoteException.

Il codice riportato di seguito viene illustrato.

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);
        }
    }
}

Si noti che il codice di cui sopra utilizza anche provare-con-le risorse per garantire che il ResultSet, Statement e Connection sono tutte chiuse.

2021-11-24 08:26:23

In altre lingue

Questa pagina è in altre lingue

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