如何使用数组方法通过RMI 从数据库的table 中获取完整的数据?
How to fetch complete data from the database's table through RMI using the array method?
我想通过 RMI 从数据库 table 中获取完整的数据。我在 Java 接口中使用了数组方法,并且在实现 class 中实现了该方法。我的意图是通过实现获取数组中的数据,并在客户端通过 JTable
显示它。我在数据库中创建了一个单列 table。我必须从 table 获取全部数据到客户端。
我附上了我所做的编码。
我已经评论了我得到的代码部分的错误。
界面
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.
}
}
remote interfaces中的抽象方法不能是静态的,所以需要将接口定义改成下面这样
public interface Interface extends java.rmi.Remote {
public String[] getArray() throws RemoteException;
}
远程方法返回的值必须是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;
}
由于 class TheImplementation
是您的 RMI 服务器 class,log exceptions rather than display a JOptionPane
and you should always log the stack trace 可能更好。请注意,远程方法必须声明它们抛出 RemoteException
以通知 RMI 客户端远程方法失败。因此,除了记录异常之外,方法 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);
}
}
}
注意上面的代码还用了try-with-resources保证ResultSet
,Statement
和Connection
都关闭了
我想通过 RMI 从数据库 table 中获取完整的数据。我在 Java 接口中使用了数组方法,并且在实现 class 中实现了该方法。我的意图是通过实现获取数组中的数据,并在客户端通过 JTable
显示它。我在数据库中创建了一个单列 table。我必须从 table 获取全部数据到客户端。
我附上了我所做的编码。 我已经评论了我得到的代码部分的错误。
界面
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.
}
}
remote interfaces中的抽象方法不能是静态的,所以需要将接口定义改成下面这样
public interface Interface extends java.rmi.Remote {
public String[] getArray() throws RemoteException;
}
远程方法返回的值必须是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;
}
由于 class TheImplementation
是您的 RMI 服务器 class,log exceptions rather than display a JOptionPane
and you should always log the stack trace 可能更好。请注意,远程方法必须声明它们抛出 RemoteException
以通知 RMI 客户端远程方法失败。因此,除了记录异常之外,方法 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);
}
}
}
注意上面的代码还用了try-with-resources保证ResultSet
,Statement
和Connection
都关闭了