begin process at 2012 02 15 19:36:44
  Trouver un code source :
 
dans
 
Accueil > Forum > 

JAVA / J2EE / J2ME

 > 

Système

 > 

JDBC

 > 

EJB STATFULL


Derniers messages déposésPoser une question dans le forum ou lancer une discussion

EJB STATFULL

jeudi 2 septembre 2010 à 16:59:06 | EJB STATFULL

aya2007

Salut a tous
Je me suis bloqué avec mon EJB statefull qui traite une table Entreprise

Code Java :
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
Code :
 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 :

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)
jeudi 2 septembre 2010 à 18:34:50 | Re : EJB STATFULL

super_toinou

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

++
vendredi 3 septembre 2010 à 00:40:08 | Re : EJB STATFULL

aya2007


Merçi pour ta réponse
J'ai esséyé avec la 2emme solution que tu m'a proposé
voila mon fichier jndi.properties
Code Java :
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
Code Java :
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.<init>(Unknown Source)
	at java.net.Socket.<init>(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
vendredi 3 septembre 2010 à 10:55:22 | Re : EJB STATFULL

super_toinou

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é.





vendredi 3 septembre 2010 à 15:38:49 | Re : EJB STATFULL

aya2007

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


Cette discussion est classée dans : public, string, resultats, nomination, nomresponsable


Répondre à ce message

Sujets en rapport avec ce message

Serveur multiple qui redistribue les données à TOUT ses clients... [ par Hellway ] Voilà, mon problème est assez compliqué et je n'ai trouvé en aucun endroit de la toile réponse à ma question. Je tente donc le coup ici.Je bosse actue recuperer contenu de balise xml avec sax [ par lolofx ] salut, voila, je voudrais recuprer le contenu d'une balise xml choisi voila la sourcepour info nom correspond au nom de la balisefichier au nom du fic java, aidez moi je craque :'( [ par yasminexp ] slt voila ja deux fichier un Livre.java qui creer des livre avec nom auteur nbpage ... dedans un fonction compare2 qui est apler dans TestLivre.java, précision exo java sur implémentation [ par so250581 ] Bonjour, j'ai réalisé un sujet d'exercice sur les implémentations en java. J'ai mis les réponses que je pensais mais je n'en suis pas du tout sûr. Pou JUnit - "java.lang.StackOverflowError" au lancement de la classe de test dans Eclipse [ par biboune56 ] Bonjour à tous,Me formant aux tests unitaires, j'ai écrit 2 classes :la 1re classe (pour le test) : ----------------------------------import junit.fra filtre [ par sheorogath ] slt tout le monde monde voila mon prob:j'ai creer un programme qui lit la premiere ligne d'un fichier avec un certaine extension mais je voudrais que applet print [ par neo1260 ] Salut, c encore moi avec mon probléme d'applet, j'ai laissé tombé la classe interne mais il me met une erreude compilation au niveau du book.append (p Communication entre 2 classes [ par cpraud ] Bonjour je cherche à faire communiquer 2 classesla 1ere extends Appletla 2ième extends la 1ere, et doit me renvoyer un paramètre de la page HTMLmais j de l'aide pour Vector [ par tocotodo ] Bonjour je une class Radio.java et autre TestRadio.pour mon vector , toutes champs d'information donc brandName, model , option et prix, je les optien passer de fichier texte a des classes java [ par anneli ] alo,je travaille avec des fichiers textes contenant des données sous la forme suivante:class Voiture extends class java.lang.Object{ public stat


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 2,980 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales