RMI MySQL NETBEANS

hajhag Messages postés 7 Date d'inscription vendredi 22 février 2008 Statut Membre Dernière intervention 16 août 2012 - 13 août 2012 à 19:58
hajhag Messages postés 7 Date d'inscription vendredi 22 février 2008 Statut Membre Dernière intervention 16 août 2012 - 16 août 2012 à 15:45
HAGOUGUI

Bonjour

Merci de prêter votre attention à ma question,je viens de réaliser un petit
exemple RMI avec Une Base de Données MYSQL sous netbeans dans la même machine JVM.
ceci dans le but de se familiariser avec ce type d'application distribuée
j'ai créer toutes les classes nécessaires pour faire le tourner ,mais le problème quand j'exécute le client rien n'est affiché,j'ai beaucoup cherché ça fait déjà un bon nombre d'heures si non de jours .


Ma question est ce que j'ai omis une étape qui empêche l'affichage dans le fram Client

Mecri d'avoir donner une idée

Les classes Sont :
InterfaceBdRmi,ImplementBdRmi,ClientBdRmi Et ServerBdRmi

Bien sûr avant d’exécuter la Classe ClientBdRmi je passe par les étapes
suivantes:

dans le chemin class
rmic agenda.ImplementBdRmi
rmiregitry
Run via Netbeans ServeurBdRmi

Ma base Agendarmi contient une seule table TESTRMI
avec user Client password 123456

ID int 10
Nom Varchar 30
Email varchar 30
..................................................
Classe InterfaceBdRMI
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package agenda;


import java.rmi.Remote;
import java.rmi.RemoteException;

public interface InterfaceBdRmi extends Remote {
public String consultation(int sid) throws RemoteException;
public String consultationemail(int sid) throws RemoteException;

}



.........................
Classe ImplementBdrmi
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package agenda;


import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.sql.*;

public class ImplementBdRmi extends UnicastRemoteObject implements InterfaceBdRmi{

public ImplementBdRmi()throws RemoteException{
super();
}
public Connection connect()throws RemoteException{

// String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
// String url ="jdbc:odbc:base_station";



String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/agendarmi";

String nom = "client";
String passe = "123456";




try{
Class.forName(driver).newInstance();
Connection con=DriverManager.getConnection(url,nom,passe);
System.out.println("Connexion réussite");
return(con);
}
catch(SQLException ex){
// System.out.println(ex.getMessage());
ex.printStackTrace();
System.out.println("Connexion échouée!!!");
return null;
}
catch(Exception e){

return null;
}
}


public String consultation(int sid) throws RemoteException {
String s = null;
try{
System.out.println("HAGGAGAGAG ImplementBdRmi");
Connection minconnect = connect();
Statement st = minconnect.createStatement();
ResultSet rs st.executeQuery("select * from testrmi where id "+sid);
while (rs.next()){
s=(rs.getString(2)) ;
}
minconnect.close();
st.close();
rs.close();
return s;
}
catch(SQLException ex){
System.out.println(ex.getMessage());
return null;
}
catch(Exception e){
System.out.println(e.getMessage());
return null;
}

}


public String consultationemail(int sid) throws RemoteException {
String s = null;
try{

Connection minconnect = connect();
Statement st = minconnect.createStatement();
ResultSet rs st.executeQuery("select * from testrmi where id "+sid);
while (rs.next()){
s=(rs.getString(3)) ;
}
minconnect.close();
st.close();
rs.close();
return s;
}
catch(SQLException ex){
System.out.println(ex.getMessage());
return null;
}
catch(Exception e){
System.out.println(e.getMessage());
return null;
}
}


public String consultation() throws RemoteException {
throw new UnsupportedOperationException("Not supported yet.");
}


public String consultationemail() throws RemoteException {
throw new UnsupportedOperationException("Not supported yet.");
}
}
// End OF FILE
......................


Classe ServerBdRMI

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package agenda;


import agenda.ImplementBdRmi;
import java.rmi.Naming;
public class ServerBdRmi {
public ServerBdRmi(){
try{

ImplementBdRmi obj = new ImplementBdRmi();
Naming.rebind("rmi://"+java.net.InetAddress.getLocalHost()+"/serveur",obj);
System.out.println("Serveur lancé");
}
catch(Exception e){
System.out.println("Dans ServerBdRmi: Exception capturée: " + e.getMessage());

}
}
public static void main(String[] args) {
new ServerBdRmi();
}

}
// END OF FILE

.......................

Classes ClientBdRmi
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* ClientBdRmi.java
*
* Created on 10 août 2012, 18:46:41
*/
package agenda;

import java.rmi.Naming;


public class ClientBdRmi extends javax.swing.JFrame {

/** Creates new form ClientBdRmi */
public ClientBdRmi() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

panel1 = new java.awt.Panel();
txt_id = new java.awt.TextField();
b_consultation = new java.awt.Button();
txt_nom = new javax.swing.JTextField();
txt_email = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

txt_id.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txt_idActionPerformed(evt);
}
});

b_consultation.setActionCommand("Consultation");
b_consultation.setLabel("Consultation");
b_consultation.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_consultationActionPerformed(evt);
}
});

txt_nom.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txt_nomActionPerformed(evt);
}
});

javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
panel1.setLayout(panel1Layout);
panel1Layout.setHorizontalGroup(
panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panel1Layout.createSequentialGroup()
.addGap(41, 41, 41)
.addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txt_email, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txt_id, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b_consultation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txt_nom, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE))
.addContainerGap(71, Short.MAX_VALUE))
);
panel1Layout.setVerticalGroup(
panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panel1Layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(txt_id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(b_consultation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(21, 21, 21)
.addComponent(txt_nom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txt_email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(105, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(50, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);

pack();
}// </editor-fold>

private void txt_idActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void txt_nomActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void b_consultationActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

int id=0;
String nom;
String email;
String ids;
InterfaceBdRmi obj = null;
try {
System.out.println("1111 ClientBdRMI ");
obj = (InterfaceBdRmi) Naming.lookup("rmi://localhost/serveur");
// obj = (InterfaceBdRmi) Naming.lookup("rmi://"+java.net.InetAddress.getLocalHost()+"/seveur");


System.out.println("222222222 ClientBdRMI");
ids = txt_id.getText();
id = Integer.parseInt(ids);
nom = obj.consultation(id);
email = obj.consultationemail(id);
txt_nom.setText(nom);
txt_email.setText(email);
}
catch(Exception e) {

}

}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ClientBdRmi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ClientBdRmi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ClientBdRmi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ClientBdRmi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new ClientBdRmi().setVisible(true);
}
});
}
// Variables declaration - do not modify
private java.awt.Button b_consultation;
private java.awt.Panel panel1;
private javax.swing.JTextField txt_email;
private java.awt.TextField txt_id;
private javax.swing.JTextField txt_nom;
// End of variables declaration
}
// END OF FILE

2 réponses

hajhag Messages postés 7 Date d'inscription vendredi 22 février 2008 Statut Membre Dernière intervention 16 août 2012
14 août 2012 à 19:51
personne n'a une idée

HAGOUGUI
0
hajhag Messages postés 7 Date d'inscription vendredi 22 février 2008 Statut Membre Dernière intervention 16 août 2012
16 août 2012 à 15:45
voila j'ai trouvé la solution si quelqu'un a besoin d'un exemple
rmi avec base de données mysql



la solution dans même machine
corrigez dans la classe ServerBdRmi remplaces ces lignes suivantes

InterfaceBdRmi obj = new ImplementBdRmi();

Naming.rebind("rmi://localhost:1099/serveur",obj);


HAGOUGUI
0
Rejoignez-nous