Файл: 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 моя база данных, содержащая соответствующие таблицы.
Приведенный выше адрес подключения и драйверы работают правильно для каждого кода, но соответствуют этому rmi.
Эксперты вы найдете какие-либо изменения?
Является ли jar-файл драйвера mysql jdbc в пути к классам приложения? – Thihara
Файл драйвера jar, добавленный в classpath или application/lib? – VinayVeluri
yess я добавил – user3219417