如何获取完整的数据库中的数据表,通过我国使用的阵列的方法?

0

的问题

我想要获取完整的数据库中的数据表,通过RMI。 我用的阵列方法在Java口和我已经实施了这一方法在实施类。 我的意图是把数据列通过执行情况,并显示它通过 JTable 在客户端。 我已经创建了一个列表中的数据库。 我必须得到整个数据表给客户的侧面。

我附上的编码,我没有。 我有评论中的错误代码部分,我得到了。

接口

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

执行情况

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

抽象的方法 的远程接口 不是静态的,所以你需要改变的接口定义如下。

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

值返回通过远程方法,必须 序列化. 阵列在java序列化,但是阵列有固定的尺寸,因为你正在返回的结果的数据库查询,你不知道的尺寸。 因此我建议,方法 getArray 返回 或者更好的是,一 CachedRowSet.

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

由于类 TheImplementation 是你马绍尔群岛共和国服务器类,它可能是更好地 记录 例外情况,而不是一个显示器 JOptionPane 你应该始终记录 堆栈. 注意到,远程方法,必须声明,他们扔 RemoteException 为了通知马绍尔群岛共和国的客户,远程方法失败。 因此除了记录例外方法 getArray 还可以扔了 RemoteException.

以下代码证明了这一点。

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

注意上述代码还利用 试用资源 来确保 ResultSet, StatementConnection 都是封闭的。

2021-11-24 08:26:23

其他语言

此页面有其他语言版本

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