2014-02-18 44 views
-1

文件SelectServerIntf.java類未發現異常:com.mysql.jdbc.Driver(RMI程序不工作)

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。

專家你是否找到任何修改?

+0

mysql jdbc驅動程序jar文件在應用程序的類路徑中? – Thihara

+0

將驅動程序jar文件添加到classpath或application/lib中? – VinayVeluri

+0

我又添加了它 – user3219417

回答

2

您需要將mysql jdbc驅動程序罐放入服務器的類路徑中。它將使您的服務器加載驅動程序類,同時調用executeSelect()