Aplikasi Client-server database bagian-3 (client)

Setelah API dan server jadi sekarang saatnya kta membuat project untuk klientnya…, project ini agak lama karena kita harus buat desaign viewnya atau GUI-nya atau Visualnya.. Artikel ini meneruskan artikel sebelumnya..bisa dibaca disini
dibawah ini screenshot projectnya :

client

setelah itu buat tablemodel dulu dengan nama TableModelPerson.java (class ini yang mengatur table yang ada di visualnya client)

package stta.bahrie.client.model;

import api.bahrie.entity.Person;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
/**
 * @author bahrie
 */
public class TableModelPerson extends AbstractTableModel {
    List list = new ArrayList();
    public TableModelPerson() {
    }
    public Person get(int row){
        return list.get(row);
    }
    public int getRowCount() {
        return list.size();
    }
    public int getColumnCount() {
        return 4;
    }
    public void insert(Person person) {
        list.add(person);
        fireTableDataChanged();
    }
    public void update(int row, Person person) {
        list.set(row, person);
        fireTableDataChanged();
    }
    public void delete(int row) {
        list.remove(row);
        fireTableDataChanged();
    }
    public void setData(List list) {
        this.list = list;
        fireTableDataChanged();
    }
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0: return list.get(rowIndex).getId();   
            case 1:return list.get(rowIndex).getNama_depan();
            case 2: return list.get(rowIndex).getNama_belakang();
            case 3:return list.get(rowIndex).getTanggal_lahir();              
            default: return null;        
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:return "id";
            case 1:return "Nama Depan";
            case 2: return "Nama Belakang";
            case 3: return "Tanggal Lahir";
            default:   return null;
        }
    }
}

setelah itu buat desaign visualnya atau dlam istilah java adalah viewnya..

dibawah ini adlah screenshot desaing yg saya buat.
desaing
dan dibawah ini inspectornya atau nama variable tiap componen yang ad di visualnya..
variable

dan dibawah ini adalah sourcecode yang ada dibelakang view tersebut

package stta.bahrie.client.form;

import api.bahrie.entity.Person;
import api.bahrie.service.PersonSevice;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import stta.bahrie.client.model.TableModelPerson;

/**
 *
 * @author bahrie
 */
public class FormClient extends javax.swing.JFrame {

    private TableModelPerson tableModelPerson = new TableModelPerson();
    private PersonSevice personSevice;

    /** Creates new form FormClient */
    public FormClient(PersonSevice personSevice) {

        this.personSevice = personSevice;

        try{
            tableModelPerson.setData(this.personSevice.getPerson());
        }catch(RemoteException exception){
            exception.printStackTrace();
        }

        initComponents();

        TablePerson.setModel(tableModelPerson);

        TablePerson.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                int row = TablePerson.getSelectedRow();
                if (row != -1) {
                    Person person = tableModelPerson.get(row);
                    textId.setValue(person.getId());
                    textNdepan.setText(person.getNama_depan());
                    textNbelakang.setText(person.getNama_belakang());
                    textTanggalLahir.setValue(person.getTanggal_lahir());
                }
            }
        });
    }

    void kosongkanText(){
        textId.setText("");
        textNdepan.setText("");
        textNbelakang.setText("");
        textTanggalLahir.setText("");
    }

    private void refreshButtomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtomActionPerformed
        // TODO add your handling code here:
        try {
            List list = personSevice.getPerson();
            tableModelPerson.setData(list);
            this.kosongkanText();
        } catch (RemoteException exception) {
            exception.printStackTrace();
        }

    }//GEN-LAST:event_refreshButtomActionPerformed

    private void insertButtomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertButtomActionPerformed
        // TODO add your handling code here:
        try {
            Person person = new Person();
            person.setNama_depan(textNdepan.getText());
            person.setNama_belakang(textNbelakang.getText());
            person.setTanggal_lahir((Date) textTanggalLahir.getValue());

            Person person1=personSevice.insertPerson(person);
            tableModelPerson.insert(person1);
            this.kosongkanText();
        } catch (RemoteException exception) {
            exception.printStackTrace();
        }
    }//GEN-LAST:event_insertButtomActionPerformed

    private void updateButtomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtomActionPerformed
        // TODO add your handling code here:
        try {

            int row = TablePerson.getSelectedRow();

            if (row == -1) {
                return;
            }

            Person person = tableModelPerson.get(row);

            person.setNama_depan(textNdepan.getText());
            person.setNama_belakang(textNbelakang.getText());
            person.setTanggal_lahir((Date) textTanggalLahir.getValue());

            personSevice.updatePerson(person);

            tableModelPerson.update(row, person);

            this.kosongkanText();

        } catch (RemoteException exception) {
            exception.printStackTrace();
        }
    }//GEN-LAST:event_updateButtomActionPerformed

    private void deleteButtomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtomActionPerformed
        // TODO add your handling code here:
        try{
            int row=TablePerson.getSelectedRow();
            if(row==-1){
                return ;
            }
            Long id=tableModelPerson.get(row).getId();
            personSevice.deletePerson(id);

            tableModelPerson.delete(row);

        }catch(RemoteException exception){
            exception.printStackTrace();
        }
    }//GEN-LAST:event_deleteButtomActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable TablePerson;
    private javax.swing.JButton deleteButtom;
    private javax.swing.JButton insertButtom;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton refreshButtom;
    private javax.swing.JFormattedTextField textId;
    private javax.swing.JTextField textNbelakang;
    private javax.swing.JTextField textNdepan;
    private javax.swing.JFormattedTextField textTanggalLahir;
    private javax.swing.JButton updateButtom;
    // End of variables declaration//GEN-END:variables
}

sekarang tinggal buat Main.java nya untuk konek ke server

package stta.bahrie.sisterclient;

import api.bahrie.service.PersonSevice;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import javax.swing.SwingUtilities;
import stta.bahrie.client.form.FormClient;

/**
 *
 * @author bahrie
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws RemoteException, NotBoundException {
        // TODO code application logic here
        Registry registry=LocateRegistry.getRegistry("localhost", 1234);

        final PersonSevice service=(PersonSevice) registry.lookup("service");

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
         FormClient form=new FormClient(service);
         form.setLocationRelativeTo(null);
         form.setVisible(true);
            }
        });
    }
}

Alhamdulillah sampe disini sudah selesai tinggal testing..
dan dibawah ini adalah screenshoot testingnya…
testing

Selamat mencoba semoga tidak ada kesulitan.. bila ingin tanya-tanya atau kasih kritik dan saran bisa lewat ym! : bahrie127 maupun Email : bahrie271@yahoo.com

:):):):)

21 Komentar (+add yours?)

  1. ipank
    Jun 13, 2011 @ 11:54:35

    mas aq mau tnya soal client server menggunakan RMI
    untuk membuat menu utama di client tu gmna?supaya jinternalframe nya biasa di panggil ni menu utama nya…
    ym nya aq add…

    Balas

    • bahrie-___-
      Jun 13, 2011 @ 14:36:27

      dikodingnya ada.. dan juga saya sudah buatkan postingan soal itu, soal membuat form didalam form utama.😀 dicari aja di sini..

      Balas

  2. ipank
    Jun 24, 2011 @ 10:41:08

    mas Q tnya lgi ya…
    untuk update data pada RMI gmna??
    ni coding ygQ pake
    ————————
    try {
    Pelajaran pela = records.get(Tpela.getSelectedRow());
    pela.setKd_pelajaran(txtkd.getText());
    pela.setNm_pelajaran(txtmata.getText());
    pela.setId_pelajaran(txtid.getText());
    pela.setStatus_aktif(“1”);
    System.out.println(“Update Berhasil”);
    pelaService.update(pela);
    this.statusAwal();
    } catch (RemoteException ex) {
    Logger.getLogger(PelajaranUI.class.getName()).log(Level.SEVERE, null, ex);
    System.out.println(“Update Gagal”);
    }
    ————————————————-
    yg keluar tu Update berhasil, tetapi data gak berubah,alisa masih sama kyk sebelum di update… itu solusi nya bagaimna???

    Balas

    • bahrie-___-
      Jun 25, 2011 @ 05:36:42

      mo ngupdate ke table database ato table pada form?? eh ga ke updatenya ada laporan error ndak?? kalo ada coba di copas disini…

      Balas

      • ipank
        Jun 25, 2011 @ 11:35:59

        mo nya update di jtable nya…kn secra otomatis kn jg update db nya…gk ada lporn eror sma sekali…malah yg tmpil update berhasil…tp data di jtabel gak ada yg brbah sma sX….trus itu gmna solusinya….soal nya yg krng update data aj yg blum bsa…

      • bahrie-___-
        Jun 27, 2011 @ 12:01:34

        coba baca di kodinganku.. kaya nya udah ada ..hehee ato kirim satu file .java nya kesaya..
        😀

  3. Bayu
    Jul 21, 2011 @ 01:52:46

    Bang, kok saya eror ya? ini tulisannya

    Exception in thread “main” java.rmi.server.ExportException: Port already in use: 1234; nested exception is:
    java.net.BindException: Address already in use: JVM_Bind
    at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:310)
    at sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:218)
    at sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:393)
    at sun.rmi.transport.LiveRef.exportObject(LiveRef.java:129)
    at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:190)
    at sun.rmi.registry.RegistryImpl.setup(RegistryImpl.java:92)
    at sun.rmi.registry.RegistryImpl.(RegistryImpl.java:78)
    at java.rmi.registry.LocateRegistry.createRegistry(LocateRegistry.java:186)
    at stta.bahrie.sisterserver.Main.main(Main.java:24)
    Caused by: java.net.BindException: Address already in use: JVM_Bind
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:365)
    at java.net.ServerSocket.bind(ServerSocket.java:328)
    at java.net.ServerSocket.(ServerSocket.java:194)
    at java.net.ServerSocket.(ServerSocket.java:106)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createServerSocket(RMIDirectSocketFactory.java:27)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createServerSocket(RMIMasterSocketFactory.java:333)
    at sun.rmi.transport.tcp.TCPEndpoint.newServerSocket(TCPEndpoint.java:649)
    at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:299)
    … 8 more
    Java Result: 1

    Balas

  4. blooming18g
    Agu 14, 2011 @ 12:02:21

    mas mau tnya lagi aq…
    tentang pencarian data dengan RemoteException

    void loadtryout() throws RemoteException {
    trlist = trService.getByNis(sisPerson.getNis());
    for (int a = 0; a < trlist.size(); a++) {
    Siswa Sis = sisService.getByNis(trlist.get(a).getNis());
    sislist.add(Sis);
    }
    System.out.println(trlist.size());
    }

    private void BcariActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    try {
    trlist.clear();
    tryoutPerson.setNis(txtnis.getText());
    loadtryout();
    if(trlist.size()==0){
    JOptionPane.showMessageDialog(null, "Nilai Belum Keluar", "Kosong", JOptionPane.INFORMATION_MESSAGE);
    txtnis.setText("");
    }
    isitabeltryout();
    txtnis.setText("");
    } catch (RemoteException ex) {
    Logger.getLogger(TryoutUI.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

    //———– sourcecode milik anda ———————-
    /**
    void loadKrs() throws RemoteException {
    detailList = detailService.getByNim(mhsPerson.getNim());
    for (int a = 0; a < detailList.size(); a++) {
    Mk mk = mkService.getByMk(detailList.get(a).getKd_mk());

    mk2List.add(mk);
    }
    System.out.println(detailList.size());
    }
    */
    —————————-

    pas di Run n ngisi text nya untuk nyari data kok error

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at stmik.jouhar.com.gui.TryoutUI.loadtryout(TryoutUI.java:127)
    at stmik.jouhar.com.gui.TryoutUI.BcariActionPerformed(TryoutUI.java:309)
    at stmik.jouhar.com.gui.TryoutUI.access$100(TryoutUI.java:38)
    at stmik.jouhar.com.gui.TryoutUI$2.actionPerformed(TryoutUI.java:224)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
    at java.awt.Component.processMouseEvent(Component.java:6288)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6053)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4651)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:616)
    at java.awt.EventQueue$2.run(EventQueue.java:614)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

    padahal udah sama kayak punya e pean…
    mohon solusi nya…

    Balas

    • bahrie-___-
      Agu 14, 2011 @ 13:56:36

      at stmik.jouhar.com.gui.TryoutUI.loadtryout(TryoutUI.java:127)
      TryoutUI baris 127 isinya apa?? coba dicopi disni.. baris itu nilainya null, ga dapet data itu maksudnya.. jadi nanti bisa di tres mau dari mana datanya gak jalan..hehhe .. kalo lama nunggunya bisa chat aja ma saya di gtalk : bahrie172

      Balas

  5. timooty
    Sep 05, 2011 @ 09:07:37

    can anyone help me ??
    ————————–
    these troubles..

    void loadtryout() throws RemoteException {
    trlist = trService.getByNis(sisPerson.getNis());
    for (int a = 0; a < trlist.size(); a++) {
    Siswa Sis = sisService.getByNis(trlist.get(a).getNis());
    sislist.add(Sis);
    }
    System.out.println(trlist.size());
    }
    —-
    try {
    trlist.clear();
    tryoutPerson.setNis(txtnis.getText());
    loadtryout();
    if(trlist.size()==0){
    JOptionPane.showMessageDialog(null, "Nilai Belum Keluar", "Kosong", JOptionPane.INFORMATION_MESSAGE);
    txtnis.setText("");
    }
    isitabeltryout();
    txtnis.setText("");
    } catch (RemoteException ex) {
    Logger.getLogger(TryoutUI.class.getName()).log(Level.SEVERE, null, ex);
    }
    —- if run
    Client Siswa Berjalan
    0
    0
    0

    the desired data should appear, but only 0 and "Nilai Belum Keluar" ..
    please help

    Balas

  6. timooty
    Sep 06, 2011 @ 12:21:39

    Balas

  7. adnanbunst
    Jan 07, 2012 @ 11:53:32

    Exception in thread “main” java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is:
    java.net.SocketTimeoutException: Read timed out
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:286)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at stta.bahrie.sisterclient.Main.main(Main.java:29)

    Balas

  8. Pandot
    Jul 10, 2012 @ 14:00:10

    Mau tanya gan, kalo implementasi DAOnya pake JPA itu gmn yah??

    thx.before.

    Balas

    • bahrie
      Jul 13, 2012 @ 09:21:22

      entitynya taruh di project api, dan implement dan yg manggil jpa nya taruh di project server.

      Balas

      • Pandot
        Jul 21, 2012 @ 11:20:13

        Halo gan, saya dah coba tapi keluar error kayak gini :

        Error unmarshaling return; nested exception is:
        java.lang.ClassNotFoundException: javax.persistence.RollbackException (no security manager: RMI class loader disabled)

        itu apanya yang salah ya??

        thx.before.

      • bahrie
        Jul 27, 2012 @ 04:45:17

        itu ada classnotfound, jadi ada klass yang ke panggil.. di cek lagi..

  9. Heri Hermawan
    Mei 07, 2013 @ 07:59:44

    mas, saya mau tanya. aplikasinya sudah berjalan di satu komputer yang saya jalankan ( saya anggap server). pertanyaan saya bagaimana menjalankan aplikasi ini di komputer lain (saya anggap client) ,(komputer sudah terhubung jaringan). terima kasih

    Balas

  10. Paradita Umbara
    Agu 21, 2013 @ 20:06:39

    kalau database ditaruh dihosting gimana mas ? semacam arsitektur three tier gitu…

    Balas

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: