Code Java - RMI Lab Programming - JDBC

[Mã code 27572]
  1 Đánh giá    Viết đánh giá
 38      426      4
Phí tải: Miễn phí
Danh mục
Thể loại
Nhóm code
Ngày đăng
11-1-2021
Loại file
Full code
Dung lượng
46 KB

Share code Java RMI Lab Phone Directory Client - Server JDBC + CSDL +INSERT +UPDATE +DELETE +SEARCH


MÔ TẢ CHI TIẾT

ServerPhone.java

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
 
/**
 *
 * @author SINHVIEN
 */
public class ServerPhone extends  UnicastRemoteObject implements InterfacePhone {
    
    static final String DB_URL = "jdbc:mysql://localhost:3306/phonedir";
    static final String USER_NAME = "root";
    static final String PASSWORD = "";
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs;
    PreparedStatement prst;
    
    public ServerPhone() throws RemoteException{
        
    }
 
    /**
     *
     * @param name
     * @param phone
     * @return
     * @throws RemoteException
     */
    @Override
    public String themSDT(String name, String phone) throws RemoteException {
       String phoneNumber = "", val = "";
        try {
            // connnect to database 
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, "root", "");
            // crate statement
            stmt = conn.createStatement();
            // get data from table 
            String select = "SELECT * FROM contact WHERE phoneno = '"+ phone+"'";
           
            rs = stmt.executeQuery(select);
            // show data
            while (rs.next()) {
                phoneNumber = rs.getString("phone");
            }
            if(phone.intern().equals(phoneNumber)) { 
                val="0"; 
            } else if(!phone.intern().equals(phoneNumber)) {
               stmt = conn.createStatement();
               String insert = "INSERT INTO contact(contactname,phoneno) VALUES('"+name+"','"+phone+"')";
               stmt.executeUpdate(insert);
               val = "1";
            }
            
        } catch (Exception ex) {
        //catch (ClassNotFoundException | SQLException ex) {
        } 
        return val;
        
    }
    public ArrayList searchContact(String PhoneNo) throws RemoteException{
        ArrayList array = new ArrayList();
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, "root", "");
            // crate statement
            stmt = conn.createStatement();
            // get data from table 'student'
            String select = "SELECT * FROM contact WHERE phoneno = '"+ PhoneNo+"'";
           
            rs = stmt.executeQuery(select);
            // show data
            while (rs.next()) {
                for(int i=0;i<2; i++){
                    array.add(rs.getString(i+1));
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return array;
    }
    
    public ArrayList viewAllContacts(int i) throws RemoteException{
        ArrayList array = new ArrayList();
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, "root", "");
            // crate statement
            stmt = conn.createStatement();
            //
            String sql = "SELECT contactname, phoneno FROM contact ORDER BY contactname DESC";
            stmt.execute(sql);
            rs = stmt.getResultSet();
            while(rs.next()){
                array.add(rs.getObject(i+1));
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServerPhone.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ServerPhone.class.getName()).log(Level.SEVERE, null, ex);
        }
        return array;
    }
    
    public String updateContact(String contactName, String phoneNo) throws RemoteException{
        String val="", CPHONE="";
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, "root", "");
            // crate statement
            stmt = conn.createStatement();
            String sql = "SELECT *FROM contact WHERE phoneno= "+phoneNo+ "";
            rs = stmt.executeQuery(sql);
            while(rs.next()){
                CPHONE = rs.getString("phoneno");
            }
            if(!phoneNo.intern().equals(CPHONE)){
                val="0";
            }else if(phoneNo.intern().equals(CPHONE)){
                prst = conn.prepareStatement("UPDATE contact SET contactname = '" + contactName + "' WHERE phoneno ='" + phoneNo + "'");
                prst.executeUpdate();
                val="1";
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServerPhone.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ServerPhone.class.getName()).log(Level.SEVERE, null, ex);
        }
        return val;
    }
    
    public String deleteContact(String phoneNo) throws RemoteException{
        String val="";
        String CPHONE="";
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, "root", "");
            // crate statement
            stmt = conn.createStatement();
            String sql = "SELECT *FROM contact WHERE phoneno= '"+phoneNo+ "'";
            rs = stmt.executeQuery(sql);
            while(rs.next()){
                CPHONE = rs.getString("phoneno");
            }
            if(!phoneNo.intern().equals(CPHONE)){
                val = "0";
            }else
                if(phoneNo.intern().equals(CPHONE)){
                    stmt = conn.createStatement();
                    String sql2 = "DELETE FROM contact WHERE phoneno = '" +phoneNo +"'";
                    stmt.execute(sql2);
                    val = "1";
                }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServerPhone.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ServerPhone.class.getName()).log(Level.SEVERE, null, ex);
        }
        return val;
    } 
    
    public static void main(String[] args) {
        try {
            LocateRegistry.createRegistry(1999);
            ServerPhone ob = new ServerPhone();
            Naming.rebind("rmi://localhost:1999/abc", ob);
            System.out.println("Server Ready");
        } catch (Exception e) {
            e.printStackTrace();
        }
    } 
}
 
Client.java
 
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
 
/**
 *
 * @author SINHVIEN
 */
public class Client extends javax.swing.JFrame {
   
    /**
     * Creates new form Client
     */
    public Client() {
        initComponents();
    }
 
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
 
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txt_name = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txt_phoneNumber = new javax.swing.JTextField();
        btn_register = new javax.swing.JButton();
        btn_delete = new javax.swing.JButton();
        btn_update = new javax.swing.JButton();
        btn_Search = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txtSearchP = new javax.swing.JTextField();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        jPanel1.setBackground(new java.awt.Color(204, 204, 255));
        jPanel1.setToolTipText("");
        jPanel1.setName(""); // NOI18N
 
        jLabel1.setFont(new java.awt.Font("Tempus Sans ITC", 3, 24)); // NOI18N
        jLabel1.setText("PHONE DIRECTORY");
 
        jPanel2.setBackground(new java.awt.Color(204, 204, 204));
        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
 
        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Contact Name:");
 
        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Phone Number:");
 
        btn_register.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btn_register.setText("Register");
        btn_register.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_registerActionPerformed(evt);
            }
        });
 
        btn_delete.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btn_delete.setText("Delete");
        btn_delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_deleteActionPerformed(evt);
            }
        });
 
        btn_update.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btn_update.setText("Update");
        btn_update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_updateActionPerformed(evt);
            }
        });
 
        btn_Search.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btn_Search.setText("Search");
        btn_Search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_SearchActionPerformed(evt);
            }
        });
 
        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Search Phone:");
 
        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(1, 1, 1)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btn_register)
                        .addGap(18, 18, 18)
                        .addComponent(btn_update)
                        .addGap(18, 18, 18)
                        .addComponent(btn_delete)
                        .addGap(18, 18, 18)
                        .addComponent(btn_Search))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(21, 21, 21))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(37, 37, 37)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txt_phoneNumber)
                            .addComponent(txt_name, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                            .addComponent(txtSearchP))))
                .addContainerGap(55, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtSearchP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txt_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txt_phoneNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btn_register, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btn_update)
                        .addComponent(btn_delete)
                        .addComponent(btn_Search)))
                .addContainerGap())
        );
 
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(137, 137, 137))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(33, Short.MAX_VALUE))
        );
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
 
        jPanel1.getAccessibleContext().setAccessibleName("");
 
        pack();
    }// </editor-fold>                        
 
    private void btn_registerActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
        String name = txt_name.getText();
        String phoneNumber = txt_phoneNumber.getText();
 
        String name1 = "rmi://localhost:1999/abc";
 
        try {
            LocateRegistry.getRegistry(name1);
 
            InterfacePhone ip = (InterfacePhone) Naming.lookup(name1);
            String res = ip.themSDT(name,phoneNumber);
 
            if(res.equals("1")) {
                JOptionPane.showMessageDialog(this, "Add Contact successfull","Notify",JOptionPane.INFORMATION_MESSAGE);
            } else if(res.equals("0")) {
                JOptionPane.showMessageDialog(this, "Contact already exist","Notify",JOptionPane.INFORMATION_MESSAGE);
 
            }
        } catch (NotBoundException | MalformedURLException | RemoteException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                            
 
    private void btn_SearchActionPerformed(java.awt.event.ActionEvent evt) {                                           
        ArrayList array = new ArrayList();
        try{
            String PhoneNo = txtSearchP.getText();
            InterfacePhone ri = (InterfacePhone)Naming.lookup("rmi://localhost:1999/abc");
            for(int i=0;i<2;i++){
                array=ri.searchContact(PhoneNo);
            }
            if(!array.isEmpty()){
                txt_name.setText(array.get(0).toString());
                txt_phoneNumber.setText(array.get(1).toString());
            }
            else if(array.isEmpty()){
               JOptionPane.showMessageDialog(this, "Not found","Notify",JOptionPane.INFORMATION_MESSAGE);
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
        // TODO add your handling code here:
    }                                          
 
    private void btn_deleteActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        try{
            String phoneNo = txt_phoneNumber.getText();
            InterfacePhone ri = (InterfacePhone)Naming.lookup("rmi://localhost:1999/abc");
            String result = ri.deleteContact(phoneNo);
            
            if(result.equals("1")){
                JOptionPane.showMessageDialog(this, "Contact Delete Successfully!", "Tele Dir: Contact Delete.", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }                                          
 
    private void btn_updateActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        try{
            String contactName = txt_name.getText();
            String phoneNo = txt_phoneNumber.getText();
            InterfacePhone ri = (InterfacePhone)Naming.lookup("rmi://localhost:1999/abc");
            String result = ri.updateContact(contactName, phoneNo);
            if(result.equals("1")){
                JOptionPane.showMessageDialog(this, "Contact Update Successfully!", "Tele Dir: Contact Update.", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }                                          
 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
 
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Client().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify                     
    private javax.swing.JButton btn_Search;
    private javax.swing.JButton btn_delete;
    private javax.swing.JButton btn_register;
    private javax.swing.JButton btn_update;
    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.JTextField txtSearchP;
    private javax.swing.JTextField txt_name;
    private javax.swing.JTextField txt_phoneNumber;
    // End of variables declaration                   
}
 
InterfacePhone.java
 
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
 
/**
 *
 * @author SINHVIEN
 */
public interface InterfacePhone extends Remote {
    public  String themSDT(String name, String phone) throws RemoteException;
    public ArrayList searchContact(String PhoneNo) throws RemoteException;
    public ArrayList viewAllContacts(int i) throws RemoteException;
    public String updateContact(String  contacName, String phoneNo) throws RemoteException;
    public String deleteContact(String phoneNo) throws RemoteException;
}
 
AllContacs.java
 
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
 
 
/**
 *
 * @author DELL
 */
public class AllContacs extends javax.swing.JFrame {
 
    /**
     * Creates new form RMIClient
     */
    public AllContacs() {
        initComponents();
    }
 
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
 
        jFrame1 = new javax.swing.JFrame();
        jFrame2 = new javax.swing.JFrame();
        jFrame3 = new javax.swing.JFrame();
        jLabel1 = new javax.swing.JLabel();
        btnViewAll = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblContacts = new javax.swing.JTable();
 
        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
 
        javax.swing.GroupLayout jFrame2Layout = new javax.swing.GroupLayout(jFrame2.getContentPane());
        jFrame2.getContentPane().setLayout(jFrame2Layout);
        jFrame2Layout.setHorizontalGroup(
            jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame2Layout.setVerticalGroup(
            jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
 
        javax.swing.GroupLayout jFrame3Layout = new javax.swing.GroupLayout(jFrame3.getContentPane());
        jFrame3.getContentPane().setLayout(jFrame3Layout);
        jFrame3Layout.setHorizontalGroup(
            jFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame3Layout.setVerticalGroup(
            jFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        jLabel1.setFont(new java.awt.Font("Tempus Sans ITC", 1, 36)); // NOI18N
        jLabel1.setText("All Contacts");
 
        btnViewAll.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnViewAll.setText("View All");
        btnViewAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewAllActionPerformed(evt);
            }
        });
 
        tblContacts.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N
        tblContacts.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Contact Name", "Phone Number"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class
            };
 
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tblContacts.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jScrollPane1.setViewportView(tblContacts);
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(42, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnViewAll)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31))
            .addGroup(layout.createSequentialGroup()
                .addGap(102, 102, 102)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnViewAll)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
        );
 
        getAccessibleContext().setAccessibleDescription("");
 
        pack();
    }// </editor-fold>                        
 
    private void btnViewAllActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        DefaultTableModel model = (DefaultTableModel) tblContacts.getModel();
        ArrayList array = new ArrayList();
        try{
            InterfacePhone ri = (InterfacePhone)Naming.lookup("rmi://localhost:1999/abc");
            int total=0;
            for(int i=0; i<2; i++){
                array = ri.viewAllContacts(i);
                for(int j=0;  j<array.size(); j++){
                    model.setValueAt(array.get(j), j, i);
                }
                total = array.size();
            }
            if(total==0){
                JOptionPane.showMessageDialog(this, "No Contact Found!", "Tele Directory: No Contact Exist.", JOptionPane.INFORMATION_MESSAGE);
            }
            
        } catch (NotBoundException ex) {
            Logger.getLogger(AllContacs.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(AllContacs.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(AllContacs.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                          
 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AllContacs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AllContacs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AllContacs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AllContacs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
 
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AllContacs().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify                     
    private javax.swing.JButton btnViewAll;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JFrame jFrame2;
    private javax.swing.JFrame jFrame3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblContacts;
    // End of variables declaration                   
}
 


XEM THÊM ==> Hướng dẫn cài đặt chi tiết

 

HÌNH ẢNH DEMO

phonedir,phone directory,Code Java RMI Lab Programming,bai tap he phan tan

phonedir,phone directory,Code Java RMI Lab Programming,bai tap he phan tan

Nguồn: Sharecode.vn



HƯỚNG DẪN CÀI ĐẶT

Kết nối csdl jdbc  phonedir.sql

Run ServerPhone.java

Run Client.java

Run AllContacs.java

 
 
LINK DOWNLOAD

PhoneDir.rar [46 KB]

File đã kiểm duyệt
     Báo vi phạm bản quyền
Pass giải nén (Nếu có):
sharecode.vn
DOWNLOAD
(Miễn phí)

CODE GẦN GIỐNG


BÌNH LUẬN



ĐÁNH GIÁ


ĐIỂM TRUNG BÌNH

5
1 Đánh giá
Code rất tốt (1)
Code tốt (0)
Code rất hay (0)
Code hay (0)
Bình thường (0)
Thành viên
Nội dung đánh giá
23:37 - 11/1/2021
Code rất tốt
Code rất tốt và phù hợp để phát triển

 HỖ TRỢ TRỰC TUYẾN