[JAVA]Problema interfaccia MySql Client/Server tramite RMI

Linguaggi di programmazione: php, perl, python, C, bash e tutti gli altri.
Scrivi risposta
mx92
Prode Principiante
Messaggi: 218
Iscrizione: domenica 7 ottobre 2007, 9:08
Località: Molfetta (Ba)

[JAVA]Problema interfaccia MySql Client/Server tramite RMI

Messaggio da mx92 »

Salve a tutti per un progetto di Ingegneria del Software ho bisogno di creare una connessione Client/Server tra un'applicazione client ed il server dove risiede il DB MySql.
Ho pensato quindi di utilizzare la comunicazione RMI e permettere al client di utilizzare i metodi select(String query) e modificaDatabase(String query)
entrambi i quali sono metodi di un oggetto memorizzato nel server

I problemi li ho all'avvio del programma server e quindi anche all'avvio del client (il quale non trova il bind della classe sul server)

Innanzitutto da terminale do:

Codice: Seleziona tutto

rmiregistry
All'avvio da eclipse del server invece ho questo errore dato dal printStackTrace (metto solo le prime righe)

Codice: Seleziona tutto

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
	java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
	java.lang.ClassNotFoundException: util.server.InterfacciaServer
Dato che non sono molto pratico di questo protocollo C/S vorrei chiedere a qualche esperto se possa darmi una mano nel capire l'errore
Ora posto tutte le classi:
Main(Server)

Codice: Seleziona tutto

    
try
{
	InterfacciaServer server = new MySqlServer("password","agroludos");
	Naming.rebind("//localhost/MySql", server);
}
catch (RemoteException e){e.printStackTrace( );}
catch (MalformedURLException e) {e.printStackTrace( );}
catch (ClassNotFoundException e)
{
	System.out.println("Driver mysql non trovato");
}
catch(SQLException e)
{
	System.out.println("Errore con il database");
}
MySqlServer

Codice: Seleziona tutto

public class MySqlServer extends UnicastRemoteObject implements InterfacciaServer 
{
	private String user;
	private String pwd;
	private String nomeDB;
	private String indirizzoIP;
	
	private Connection conn;
	
	public MySqlServer(String user,String pwd, String nomeDB, String indirizzoIP)
		throws ClassNotFoundException, SQLException, RemoteException
	{
		this.user = user;
		this.pwd = pwd;
		this.nomeDB = nomeDB;
		this.indirizzoIP = indirizzoIP;
		
		Class.forName("com.mysql.jdbc.Driver");
		
		conn = DriverManager.getConnection("jdbc:mysql://" + indirizzoIP +"/"+ 
							nomeDB+"?user="+user+"&password="+pwd);
	}
	
	public MySqlServer(String pwd, String nomeDB) 
			throws ClassNotFoundException, SQLException, RemoteException
	{
		this("root", pwd, nomeDB, "127.0.0.1");
	}
	
	public RisultatoQuery select(String query) 
			throws SQLException, MySQLSyntaxErrorException, RemoteException
	{
		LinkedList<LinkedList<String>> matriceRisultati = new LinkedList<LinkedList<String>>();
		LinkedList<String> record;
		RisultatoQuery risultatoQuery = new RisultatoQuery();
		
		Statement stmt = conn.createStatement();
		ResultSet resultSet;
		ResultSetMetaData rsmt; 
		
		stmt.executeQuery(validaStringaQuery(query));
		resultSet = stmt.getResultSet();
		rsmt = resultSet.getMetaData();
		
		while(resultSet.next())
		{
			record = new LinkedList<String>();
			
			for(int i = 0; i < rsmt.getColumnCount(); i++)
				record.add(resultSet.getString(i+1));
			
			matriceRisultati.add(record);
		}
		
		stmt.close();
		
		risultatoQuery.assegnaRisultato(matriceRisultati);
		
		return risultatoQuery;
	}
	
	public synchronized void modificaDatabase(String query) 
			throws SQLException, MySQLSyntaxErrorException, RemoteException
	{
		Statement stmt = conn.createStatement();
		
		stmt.executeUpdate(validaStringaQuery(query));
		
		stmt.close();
	}
	
	private String validaStringaQuery(String query)
	{
		if((query.charAt(query.length() -1)) != ';')
			query += ';';
		
		return query;
	}
}

InterfacciaServer

Codice: Seleziona tutto

public interface InterfacciaServer extends Remote
{
	RisultatoQuery select(String query) 
			throws RemoteException, SQLException, MySQLSyntaxErrorException;
	
	void modificaDatabase(String query) 
			throws RemoteException, SQLException, MySQLSyntaxErrorException;
}
Main(Client)

Codice: Seleziona tutto

public static void main(String[] args)
{
	try
	{
		InterfacciaServer mySqlServer = 
				(InterfacciaServer)Naming.lookup("rmi://192.168.0.5/MySql");
			
		RisultatoQuery rq = mySqlServer.select("SELECT* FROM prova;");

		for(int i = 0; i < rq.getRows(); i++)
		{
			for(int j = 0; j < rq.getColumns(); j++)
				System.out.print(rq.getElement(i, j) + " ");
			System.out.println();
		}
	}
	catch(RemoteException e)
	{
		e.printStackTrace();
	}
	catch(MySQLSyntaxErrorException e)
	{
		System.out.println(e.getMessage());
	}
	catch(SQLException e)
	{
		System.out.println("Errore con il database...");
	}
	catch(MalformedURLException e)
	{
		System.out.println("Indirizzo ip errato...");
	} 
	catch (NotBoundException e) 
	{
		e.printStackTrace();
	}
}
Scrivi risposta

Ritorna a “Programmazione”

Chi c’è in linea

Visualizzano questa sezione: 0 utenti iscritti e 2 ospiti