EJB STATFULL

aya2007 Messages postés 11 Date d'inscription vendredi 1 juin 2007 Statut Membre Dernière intervention 10 janvier 2011 - 2 sept. 2010 à 16:59
aya2007 Messages postés 11 Date d'inscription vendredi 1 juin 2007 Statut Membre Dernière intervention 10 janvier 2011 - 3 sept. 2010 à 15:38
Salut a tous
Je me suis bloqué avec mon EJB statefull qui traite une table Entreprise

package beanStatFul;

import javax.annotation.PostConstruct;
import javax.ejb.Stateful;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Stateful(name="Entreprise")
public class EntrepriseBean implements Entreprise {

//Partie Declaration des attributs

private String nomination;
private String nomResponsable;
private String adresse;
private String dateCreation;
private int nombreEmployee;
private String activitePrincipale;

private java.sql.Connection con = null;
private java.sql.Statement stmt = null;

/*	
 * *Constructeur 
 */
@PostConstruct 
public void initialise() {
        nomination = "";
        nomResponsable = "";
        adresse = "";
        dateCreation = "";
        activitePrincipale = "";
        nombreEmployee = 0;
    }

public EntrepriseBean() 
{
/*methode initialisant  la connexion a la base de données
 Chargement du pilote
*/
    try {
              Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception e) {
              e.printStackTrace();
              System.exit(99);
        }
// Connexion à la base de données MySQL "mtpbdd" 
        try {
            String DBurl = "jdbc:mysql://localhost:3306/mtpbdd";
        	  con = DriverManager.getConnection(DBurl,"root","");
              stmt = con.createStatement();
        } catch (SQLException e) {
        	System.out.println("erreur de connexion");
            e.printStackTrace();
        }
}


//Accesseurs et mutateurs

public String getNomination() {
return nomination;
}
public void setNomination(String nomination) {
this.nomination = nomination;
}
public String getNomResponsable() {
return nomResponsable;
}
public void setNomResponsable(String nomResponsable) {
this.nomResponsable = nomResponsable;
}
public String getAdresse() {
return adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public String getDateCreation() {
return dateCreation;
}
public void setDateCreation(String dateCreation) {
this.dateCreation = dateCreation;
}
public int getNombreEmployee() {
return nombreEmployee;
}

public void setNombreEmployee(int nombreEmployee) {
this.nombreEmployee = nombreEmployee;
}
public String getActivitePrincipale() {
return activitePrincipale;
}
public void setActivitePrincipale(String activitePrincipale) {
this.activitePrincipale = activitePrincipale;
}

/*Mise a jour d'une ligne de la table 
*/
public boolean updateEntreprise (){
boolean var=false;
try{
String requete = "UPDATE Entreprise SET Nomination='"+this.nomination+"' " +
"and NomResponsable='"+this.nomResponsable+"' and Adresse='"+this.adresse+"'and" +
" dateCreation='"+this.dateCreation+"'and ActivitePrincipale='"+this.activitePrincipale+"'" +
" and Nombremployee='"+this.nombreEmployee+"'";

var=true;
   }

 catch (Exception e) {
System.out.println("Erreur pendant la suppression");
}
return var;
}
 
/*Insertion ligne dans la table
 * */ 	
 
public int insertEntreprise(){
        ResultSet resultats = null;
        int idGenere = -1;
        try {
        stmt.executeUpdate("INSERT INTO Entreprise (Nomination,NomResponsable,Adresse,DateCreation" +
        		",NombreEmployee,activitePrincipale) values('"+this.getNomination()+"'," +
            		" '"+  this.getNomResponsable()+"', '"+this.getAdresse()+"', '"+ this.getDateCreation()+
            		"','"+this.getNombreEmployee()+"','"+this.getActivitePrincipale()+"')",Statement.RETURN_GENERATED_KEYS);
            resultats = stmt.getGeneratedKeys();
            if (resultats.next()) {
               idGenere = resultats.getInt(1);
            }
            resultats.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return idGenere;
    }


/*suppression d'une ligne de la table
*/
public Boolean deleteEntreprise(){
Boolean var=false;
try {
stmt.executeUpdate("Delete From Entreprise Where Nomination='"+this.nomination+"' " +
"and NomResponsable='"+this.nomResponsable+"' and Adresse='"+this.adresse+"'and" +
" dateCreation='"+this.dateCreation+"'and Nombremployee='"+this.nombreEmployee+"'");
var=true;


} catch (SQLException e) {
e.printStackTrace();
}
return var;
}
     

/* Affichage d'une ligne
 * @see statefullBean.Entreprise#loadEntreprise(int)
 */
public void loadEntreprise (int key){
    ResultSet resultats = null;
    try {
        boolean encore = false;
        resultats = stmt.executeQuery("Select Nomination, NomResponsable,Adresse,DateCreation," +
        		"NombreEmployee,activitePrincipale from Entreprise Where Nomination="+key);
        if (resultats != null) 
          encore = resultats.next();

        if (encore)
        {
            this.setNomination(resultats.getString(1)) ;
            this.setNomResponsable(resultats.getString(2));
            this.setAdresse(resultats.getString(3));
            this.setDateCreation(resultats.getString(4));
            this.setNombreEmployee(resultats.getInt(5));
            this.setActivitePrincipale(resultats.getString(6));
        }
        else
          System.out.println ("Il n'y a aucun enregistrement avec cet paramètre !");
        resultats.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

//
/* Affichage de toutes les ligne
 * @see statefullBean.Entreprise#loadEntreprise(int)
 */
public void getAllEntreprise (){
    ResultSet resultats = null;
    try {
        resultats = stmt.executeQuery("Select Nomination, NomResponsable,Adresse,DateCreation," +
        		"NombreEmployee,activitePrincipale from Entreprise");
        boolean encore = resultats.next();

        while (encore) {
        	 this.setNomination(resultats.getString(1)) ;
             this.setNomResponsable(resultats.getString(2));
             this.setAdresse(resultats.getString(3));
             this.setDateCreation(resultats.getString(4));
             this.setNombreEmployee(resultats.getInt(5));
             this.setActivitePrincipale(resultats.getString(6));
             encore = resultats.next();
        }
        resultats.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}








}




j'ai une interface remote et une classe de test
 args) 
{


 
    Properties props = System.getProperties();
   Context ctx;
        Entreprise TestEntreprise= null;
        
try {
ctx = new InitialContext(props);
TestEntreprise = (Entreprise) ctx.lookup("EntrepriseBean/remote");
} catch (NamingException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
TestEntreprise.insertEntreprise();

}

}



J'ai un message d'erreur suivant :

[b]javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at beanManaged.Entrepriseaction.main(Entrepriseaction.java:29)
Exception in thread "main" java.lang.NullPointerException
at beanManaged.Entrepriseaction.main(Entrepriseaction.java:34)/b

4 réponses

super_toinou Messages postés 764 Date d'inscription mardi 25 mai 2004 Statut Membre Dernière intervention 8 mars 2011 6
2 sept. 2010 à 18:34
Hello,

c'est ton client qui est incapable d'appeler le serveur.
Soit tu mets tout ce qu'il faut dans les variables d'environnement.
Soit tu mets (et c'est plus la norme) un fichier jndi.properties dans ton classpath contenant au moins les infos suivantes:

java.naming.factory.initial=la factory pour ton serveur
java.naming.factory.url.pkgs=les urls de packages pour ton serveur
java.naming.provider.url=host:port

et après tu fais
Context ctx = new InitialContext();

pour les propriétés du fichier jndi.properties, cherche sur le net des s exemples en fonction de ton serveur d'app

++
0
aya2007 Messages postés 11 Date d'inscription vendredi 1 juin 2007 Statut Membre Dernière intervention 10 janvier 2011
3 sept. 2010 à 00:40
Merçi pour ta réponse
J'ai esséyé avec la 2emme solution que tu m'a proposé
voila mon fichier jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

le problem est le suivant :
quand je sauvegarde ce fichier dans la racine il affiche toujour la meme erreur precedente et si je le sauvegarde dans la racine src
j'ai cette erreur
log4j:[b]WARN No appenders could be found for logger (org.jnp.interfaces.NamingContext).
log4j:WARN Please initialize the log4j system properly.
javax.naming.CommunicationException: Could not obtain connection to any of these urls: localhost:1099 and discovery failed with error: javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out] [Root exception is javax.naming.CommunicationException: Failed to connect to server localhost:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.ConnectException: Connection refused: connect]]]
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1414)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:594)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
at javax.naming.InitialContext.lookup(Unknown Source)
at beanManaged.Entrepriseaction.main(Entrepriseaction.java:32)
Caused by: javax.naming.CommunicationException: Failed to connect to server localhost:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.ConnectException: Connection refused: connect]]
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:269)
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1385)
... 4 more
Caused by: javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.ConnectException: Connection refused: connect]
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:243)
... 5 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.(Unknown Source)
at java.net.Socket.(Unknown Source)
at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory.java:84)
at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory.java:77)
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:239)
... 5 more
Exception in thread "main" java.lang.NullPointerException
at beanManaged.Entrepriseaction.main(Entrepriseaction.java:37)/b



Samia BENABI
0
super_toinou Messages postés 764 Date d'inscription mardi 25 mai 2004 Statut Membre Dernière intervention 8 mars 2011 6
3 sept. 2010 à 10:55
Hello,

si t'es sur JBoss regardes ce tutorial :
http://www.eclipsetotale.com/articles/Introduction_EJB3_avec_Eclipse.html

Ca prend 15 min et c'est super bien expliqué.
0
aya2007 Messages postés 11 Date d'inscription vendredi 1 juin 2007 Statut Membre Dernière intervention 10 janvier 2011
3 sept. 2010 à 15:38
Merçi beaucoup je vais ésséyer avec jboss car j'utilise tomcat
je veux savoir maitenant comment faire le lien entre mon ejb statefull et un formulaire JSF qui fait le traitement d'une table des entreprises "affichage ajout suppression.." es ce que je declare ma classe de test EJB comme manager bean ??



Samia BENABI
0
Rejoignez-nous