Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1

    Lỗi không tìm được đối tượng khi tìm kiếm đối tượng trong ứng dụng RMI?

    Mình làm 1 ứng dụng RMI truy xuất CSDL nhưng ở bước naming.lookup ở client thì toàn không tìm được đối tượng. Mình đã biên dịch các file .java ra .class cũng như đã chạy server rồi. Code của mình đây. Mong các bác pro vào chỉ bảo giúp ạ:
    Mã:
    /Lớp Interface
    package csdl;
    
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    
    /**
    *
    * @author admin
    */
    public interface CategoriesITF extends Remote{
        public boolean insertUser(String User, String Pass) throws RemoteException;
        public boolean deleteUser(String User) throws RemoteException;
        public boolean updateUser(String User, String Pass) throws RemoteException;
        public Categories seclectUser(String User) throws RemoteException;
        public Categories[] selectAllUser() throws RemoteException;
    }
     
    //Lớp Implements interface
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package csdl;
    
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
    import java.rmi.*;
    import java.sql.ResultSet;
    import javax.naming.spi.DirStateFactory;
    
    /**
    *
    * @author admin
    */
    public class CategoriesImp implements CategoriesITF {
    
        private Connection con;
        private Statement stm;
    
        public void CategoriesImp() throws RemoteException {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
            }
            try {
                con = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
            } catch (Exception e) {
            }
        }
    
        @Override
        public boolean insertUser(String User, String Pass) throws RemoteException {
            String sql = "INSERT INTO Categories VALUES (\"" + User + "\",\"" + Pass + "\")";
            try {
                con.createStatement().executeUpdate(sql);
                return true;
            } catch (Exception e) {
                return false;
            }
        }
    
        @Override
        public boolean deleteUser(String User) throws RemoteException {
            String sql = "delete from Categories where User = \"" + User + "\"";
            try {
                con.createStatement().executeUpdate(sql);
                return true;
            } catch (Exception e) {
                return false;
            }
        }
    
        @Override
        public boolean updateUser(String User, String Pass) throws RemoteException {
            String sql = "update Categories set User = \"" + User + "\" where Pass=\"" + Pass + "\"";
            try {
                con.createStatement().executeUpdate(sql);
                return true;
            } catch (Exception e) {
                return false;
            }
        }
    
        @Override
        public Categories seclectUser(String User) throws RemoteException {
            Categories x = null;
            int i = 0;
            String sql = "SELECT * FROM Categories WHERE User = \"" + User + "\"";
            ResultSet rs = null;
            try {
                rs = con.createStatement().executeQuery(sql);
                while (rs.next()) {
                    String user = rs.getString("User");
                    String pass = rs.getString("Pass");
                    x.setUser(user);
                    x.setPass(pass);
                }
            } catch (Exception e) {
            }
            return x;
        }
    
        @Override
        public Categories[] selectAllUser() throws RemoteException {
            Categories[] all = null;
            int i = 0;
            String sql = "SELECT * FROM Categories";
            ResultSet rs = null;
            try {
                rs = con.createStatement().executeQuery(sql);
                while (rs.next()) {
                    String user = rs.getString("User");
                    String pass = rs.getString("Pass");
                    all[i].setUser(user);
                    all[i].setPass(pass);
                    i++;
                }
            } catch (Exception e) {
            }
            return all;
        }
    }
     
    //Lớp chạy ở server
    
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package csdl;
    
    import java.net.MalformedURLException;
    import java.rmi.Naming;
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    
    /**
    *
    * @author admin
    */
    public class Runserver {
        public static void main(String[] args) throws RemoteException, MalformedURLException{
            System.out.println("Server started");
            CategoriesITF cat = null;
            cat = new CategoriesImp();
            UnicastRemoteObject.exportObject(cat);
            Naming.rebind("rmi://localhost/cat", cat);
            System.out.println("Object 'cat' bound in server...");
        }
    }
     
    //lớp chạy ở client
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package csdl;
    
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.net.MalformedURLException;
    import java.rmi.RemoteException;
    import java.rmi.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    import javax.swing.JOptionPane;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    
    /**
    *
    * @author admin
    */
    public class clientGUI extends JFrame implements ActionListener {
    
        public int value;
        private JTextField textField1; // text field with set size
        private JPasswordField passwordField; // password field with text
        private JButton button;
        private String tendn = null;
        private String pass = null;
        private boolean dn = true;
    
        public clientGUI() {
            setLayout(new FlowLayout()); // set frame layout
    
            // construct textfield with 10 columns
            textField1 = new JTextField(10);
            add(textField1); // add textField1 to JFrame
            textField1.addActionListener(this);
    
            // construct passwordfield with default text
            passwordField = new JPasswordField(10);
            add(passwordField); // add passwordField to JFrame
            passwordField.addActionListener(this);
    
            button = new JButton("Dang nhap");
            add(button);
            button.addActionListener(this);
        }
    
        @Override
        public void actionPerformed(ActionEvent event) {
            Categories x = null;
            tendn = textField1.getText();
            pass = passwordField.getText();
            String url = "rmi://localhost/cat";
            CategoriesITF lg;
            try {
                lg = (CategoriesITF) Naming.lookup(url);
                x = lg.seclectUser(tendn);
                if (event.getSource() == button) {
                    if (tendn.equals(x.getUser())) {
                        if (pass.equals(x.getPass())) {
                            JOptionPane.showMessageDialog(null, "Đăng nhập thành công", "Thông báo", JOptionPane.PLAIN_MESSAGE);
                        } else {
                            JOptionPane.showMessageDialog(null, "Đăng nhập thất bại", "Thông báo", JOptionPane.PLAIN_MESSAGE);
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Đăng nhập thất bại", "Thông báo", JOptionPane.PLAIN_MESSAGE);
                    }
                }
            } catch (NotBoundException ex) {
                Logger.getLogger(clientGUI.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MalformedURLException ex) {
                Logger.getLogger(clientGUI.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RemoteException ex) {
                Logger.getLogger(clientGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        } // end method actionPerformed
        public static void main(String[] args) throws RemoteException {
            clientGUI client = new clientGUI();
            client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            client.setSize(325, 100);
            client.setVisible(true);
        }
    }

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Mình đâu có nhìn thấy đối tượng nào tên là cat? Mình nhìn còn không thấy thì làm sao máy tính nhìn thấy được chứ?

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •