使用RMI的arraydata到jtable

时间:2015-06-28 19:31:45

标签: java swing netbeans rmi

//我有返回列表的方法pulic列表,我想将数据显示到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:remot.mahasiswa at   sun.rmi.server.UnicastRef.invoke(UnicastRef.java:193)at   java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)     在   java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)     在com.sun.proxy。$ Proxy0.SelectMahasiswa(未知来源)at   klaiyen.CRUD2.getAll(CRUD2.java:218)at   klaiyen.CRUD2。(CRUD2.java:30)at   klaiyen.CRUD2 $ 4.run(CRUD2.java:263)

1 个答案:

答案 0 :(得分:0)

您的班级mahasiswa应如下所示:

 class mahasiswa implements Serializable {
    ...
    }