//我有一个返回列表的方法pulic list,我想把数据显示到jtable中,怎么做?
从数据库检索数据的方法
public List<mahasiswa> SelectMahasiswa() throws RemoteException {
List <mahasiswa>listMahasiswa = new ArrayList<mahasiswa>();
try {
PreparedStatement ps = con.prepareStatement("SELECT nim, nama, jurusan FROM mahasiswa");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
mahasiswa mhs = new mahasiswa();
mhs.setNim(rs.getString(1));
mhs.setNama(rs.getString(2));
mhs.setJurusan(rs.getString(3));
listMahasiswa.add(mhs);
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
return listMahasiswa;
}
将数据显示到JTable
public void getAll(){
try {
Registry regis = LocateRegistry.getRegistry("localhost", koneksi.Port_id );
remot rem = (remot) regis.lookup(koneksi.id);
DefaultTableModel model=new DefaultTableModel();
for(mahasiswa mhs:rem.SelectMahasiswa()){
String kolom1=mhs.getNim();
String kolom2=mhs.getNama();
String kolom3=mhs.getJurusan();
String kolom[]={kolom1,kolom2,kolom3};
model.addRow(kolom);
}
jTable1.setModel(model);
} catch (Exception ex) {
ex.printStackTrace();
}
}
错误消息
java.rmi.UnmarshalException:错误解组返回;嵌套的异常为:java.io.WriteAbortedException:写入中止;java.io.NotSerializableException:位于sun.rmi.server.UnincastRef.ioke(UnicastRef.java:193)位于java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)在java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)网址:com.sun.proxy.$Proxy0.SelectMahasiswa(未知来源)克莱恩。位于的CRUD2.getAll(CRUD2.java:218)克莱恩。CRUD2.(CRUD2.java:30)位于克莱恩。CRUD2$4.run(CRUD2.java:263)
您的类mahasiswa
应该如下所示:
class mahasiswa implements Serializable {
...
}