【发布时间】:2022-01-02 13:28:34
【问题描述】:
我想通过 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.
}
}
【问题讨论】:
-
tbData是在 while 循环中声明的,所以它的作用域只在 while 循环内。当你编译它时,你的 sql 会抛出一个错误。其他Interface不是 Java 对象的好名字。conpst和rst已声明但从未使用过。为什么将getArray作为static方法,为什么还要麻烦接口它? -
连接、语句和结果集永远不会关闭。 JOptionPane 用于服务器端显示错误消息。