Array yöntemini kullanarak veritabanının tablosundan RMI aracılığıyla tam veri nasıl alınır?

0

Soru

Veritabanının tablosundan RMI aracılığıyla tam veri almak istiyorum. Java arayüzünde array yöntemini kullandım ve bu yöntemi ımplementation sınıfında uyguladım. Amacım dizideki verileri uygulama yoluyla almak ve bunu göstermek JTable müşteri tarafında. Veritabanında tek sütunlu bir tablo oluşturdum. Tüm verileri o tablodan müşteri tarafına aktarmalıyım.

Yaptığım kodlamayı ekledim. Aldığım kod bölümündeki hataları yorumladım.

arayüz

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

Uygulama

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

En iyi cevabı

0

Uzak arabirimlerdeki soyut yöntemler statik olamaz, bu nedenle arabirim tanımını aşağıdaki gibi değiştirmeniz gerekir.

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

Uzak yöntemlerle döndürülen değerler seri hale getirilebilir olmalıdır. Java'daki diziler seri hale getirilebilir, ancak diziler sabit bir boyuta sahiptir ve bir veritabanı sorgusunun sonucunu döndürdüğünüzden boyutu bilemezsiniz. Bu nedenle bu yöntemi öneriyorum getArray bir ArrayList veya daha iyisi, bir CachedRowSet döndürün.

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

Sınıftan beri TheImplementation RMI sunucu sınıfınız mı, muhtemelen istisnaları görüntülemek yerine günlüğe kaydetmek daha iyidir JOptionPane ve her zaman yığın izlemeyi günlüğe kaydetmelisiniz. Uzak yöntemlerin attıklarını bildirmesi gerektiğini unutmayın RemoteException RMI istemcisine uzak yöntemin başarısız olduğunu bildirmek için. Özel oturum dışında dolayısıyla, yöntem getArray ayrıca bir atabilir RemoteException.

Aşağıdaki kod gösterilmektedir.

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

Not bu yukarıdaki kod da kullanır deneyin-ile-kaynakları emin olmak için ResultSet, Statement ve Connection tüm kapalı.

2021-11-24 08:26:23

Diğer dillerde

Bu sayfa diğer dillerde

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