找不到类异常:com.mysql.jdbc.Driver(RMI程序无法正常工作)

时间:2014-02-18 05:45:51

标签: java mysql jdbc

文件 SelectServerIntf.java

import java.rmi.*;
import java.util.*;
public interface SelectServerIntf extends Remote{
   HashMap executeSelect() throws RemoteException;
}

文件 SelectServerImpl.java

import java.rmi.*;
import java.sql.*;
import java.rmi.server.*;
import java.util.*;
public class SelectServerImpl extends UnicastRemoteObject implements SelectServerIntf {
    public SelectServerImpl() throws RemoteException
    {
    }   
    public HashMap executeSelect() throws RemoteException
    {
        String DRIVER = "com.mysql.jdbc.Driver"; 
        String url ="jdbc:mysql://localhost:3306/abc2";
        String Query="select * from student";
        Connection con=null;
        Statement stat=null;
        HashMap hm=null; 
        try
        {
            Class.forName(DRIVER);       
        }
        catch(ClassNotFoundException cn)
        {
            System.out.println("ClassNotFound"+cn);
        } 
        try
        {
            con= DriverManager.getConnection(url,"root","root");
            stat=con.createStatement();
            ResultSet rs=stat.executeQuery(Query);
            hm=new HashMap();            
            while(rs.next())
            {
                int rno=rs.getInt(1);
                String name=rs.getString(2);
                hm.put(new Integer(rno),name);
            }
            con.close();
        }
        catch(SQLException se)
        {
            System.out.println("SQLException"+se);
        }
        return(hm);
    }
}

文件 SelectServer.java

import java.rmi.*;
import java.net.*;
public class SelectServer {
    public static void main(String args[])
    {
        try
        {
            SelectServerImpl sip=new SelectServerImpl();
            Naming.rebind("SELECT-SERVER", sip);
        }
        catch(Exception e)
        {
            System.out.println("Exception:"+e);
        }
    }
}

文件 SelectClient.java

import java.rmi.*;
import java.util.*;
import java.net.*;
public class SelectClient {
    public static void main(String args[])
    {
        String rmiurl="rmi://"+args[0]+"/SELECT-SERVER";
        try
        {
            SelectServerIntf sit=(SelectServerIntf)Naming.lookup(rmiurl);
            HashMap hm2=sit.executeSelect();

            int sz=hm2.size();

            for(int i=1;i<sz;i++)
            {
                if(hm2.containsKey(new Integer(i)))
                    System.out.println(i+":"+hm2.get(new Integer(i)));
            }
        }
        catch(Exception e)
        {
          System.out.println("Exception"+e);
        }
    }
}

执行上述RMI程序

"Class not found Exception:com.mysql.jdbc.Driver"
SQLException:No Suitable Driver found for jdbc:mysql://localhost:3306/abc2

其中abc2是我的数据库,包含各自的表。

上面给出的连接url和驱动程序适用于每个代码但是接受此rmi。

专家你找到任何修改??

1 个答案:

答案 0 :(得分:2)

您需要将mysql jdbc驱动程序jar放在服务器的类路径中。它将使您的服务器在调用executeSelect()

时加载驱动程序类