IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Hibernate Java Discussion :

ajouter des informations dans la base de données


Sujet :

Hibernate Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    172
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 172
    Points : 60
    Points
    60
    Par défaut ajouter des informations dans la base de données
    Bonjour tout le monde,

    Je développe une petite application avec hibernate et struts mais lorsque je saisie des informations à partir du formulaire les informations sont affichées mais ne sont pas enregistrées dans la base de données.


    Je vous remercie de votre aide

  2. #2
    Membre expérimenté Avatar de willoi
    Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2006
    Messages
    1 355
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2006
    Messages : 1 355
    Points : 1 639
    Points
    1 639
    Par défaut
    C'est que certainement tu as une erreur quelque part!!!
    Mais sans donner plus de précisions, ca me parait difficile de t'en dire plus.
    Fais voir ton code!

  3. #3
    Membre expérimenté Avatar de maxf1
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    1 229
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 229
    Points : 1 371
    Points
    1 371
    Par défaut
    Euh juste comme ca, si tu saisies les informations. Il faut que dans ton action tu les sauvegardes. Sinon il ne peut rien se passer.

    Voici le lien qu'il te fallait (mais tu pouvais tout aussi bien le chercher par toi meme) :

    http://java.developpez.com/faq/hiber...uvelleInstance

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    172
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 172
    Points : 60
    Points
    60
    Par défaut
    Je te remercie de ton aide.

    Je travaille avec différents fichiers: configuration.xml,TContact.hbm.xml,index.jsp, le beanaction et enfin le TContactDAO.

    Voici le fichier de configuration mais là je pense qu'il est bon:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
    		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost/base1</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <mapping resource="TContact.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>
    Voici le fichier mapping je pense qu'il est bon aussi:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    	<class name="com.minosis.hibernate.TContact" table="t_contact">
    	<id name="Id" type="integer">
    	<column name="id" sql-type="int(4)"/>
    	<generator class="increment"/>
    	</id>
    	<property name="Nom" type="string">
    	<column name="nom" sql-type="char(50)" not-null="true"/>
    	</property>
    	<property name="Age" type="integer">
    	<column name="age" sql-type="int(11)" not-null="true"/>
    	</property>
    	</class>
    </hibernate-mapping>
    Le fichier jsp, il a peut être une erreur car quand je compile il me génére cette erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     <table>
    12:   	<tr>
    13:   		<td><bean:message key="index.nom" /></td>
    14:   		<td><html:text property="nom" name="nom"/></td>
    15:   	</tr>
    16:   	<tr>
    17:   		<td><bean:message key="index.prenom"/></td>
     
     
    Stacktrace:
    	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
     
     
    cause mère
    voici ma jsp:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    <%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>
    <%@ taglib uri="/WEB-INF/c" prefix="c" %>
    <html:html>
    <head>
    	<title></title>
    </head>
    <body>
     <html:form action="/doaction.do">
      <table>
      	<tr>
      		<td><bean:message key="index.nom" /></td>
      		<td><html:text property="nom" name="nom"/></td>
      	</tr>
      	<tr>
      		<td><bean:message key="index.prenom"/></td>
      		<td><html:text property="prenom" name="prenom"/></td>
      </tr>
      <tr>
      		<td><bean:message key="index.age"/></td>
      		<td><html:text property="age" name="age"/></td>
      </tr>
      <tr>
      		<td><html:submit property="envoyer" value="envoyer"/></td>
      		<td><html:reset property="annuler" value="annuler"/></td>
      </tr>
      </table>
     
     </html:form>
     
    	<bean:write name="formulaire"  property="nom"/>
    	<bean:write  name="formulaire" property="prenom"/>
    	<bean:write name="formulaire" property="age"/>
    	   <html:errors property="Nom"/>
    </body>
    </html:html>

    Voici le bean action
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    package bean;
     
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import com.minosis.hibernate.dao.*;
    import net.sf.hibernate.*;
     
    public class Formaction extends Action{
     
    	public ActionForward execute (ActionMapping mapping,ActionForm form, HttpServletRequest Request, HttpServletResponse response) throws HibernateException
    	{
     
    		try
    		{
    			TContactDAO tcontactdao=new TContactDAO();
    			tcontactdao.ajoutpersonne(Request.getParameter("nom"),Request.getParameter("prenom"),Integer.parseInt(Request.getParameter("age")));
    		}
    		catch(Exception ex)
    		{
    			ex.printStackTrace();
    		}
     
    			return mapping.getInputForward();
    	}
     
    }
    Voici mon TContactDAO:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
     
    package com.minosis.hibernate.dao;
     
    import net.sf.hibernate.HibernateException;
    import net.sf.hibernate.Session;
    import net.sf.hibernate.Transaction;
     
    import com.minosis.bdd.*;
    import com.minosis.hibernate.TContact;
    import com.minosis.hibernate.base.BaseTContactDAO;
     
     
    /**
     * This class has been automatically generated by Hibernate Synchronizer.
     * For more information or documentation, visit The Hibernate Synchronizer page
     * at http://www.binamics.com/hibernatesync or contact Joe Hudson at joe@binamics.com.
     *
     * This is the object class that relates to the t_contact table.
     * Any customizations belong here.
     */
    public class TContactDAO extends BaseTContactDAO {
     
    	public void ajoutpersonne(String nom,String prenom,Integer age) throws HibernateException
    	{
    		try
    		{
    			Session session=HibernateUtil.currentSession();
    			Transaction tx=session.beginTransaction();
     
    			TContact contact=new TContact();
    			contact.setNom(nom);
    			contact.setPrenom(prenom);
    			contact.setAge(age);
    			session.save(contact);
     
    			tx.commit();
    			HibernateUtil.closeSession();
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
     
    	}
    }
    Je vous remercie de votre aide

  5. #5
    Membre expérimenté Avatar de maxf1
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    1 229
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 229
    Points : 1 371
    Points
    1 371
    Par défaut
    Citation Envoyé par gentil
    Le fichier jsp, il a peut être une erreur car quand je compile il me génére cette erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     <table>
    12:   	<tr>
    13:   		<td><bean:message key="index.nom" /></td>
    14:   		<td><html:text property="nom" name="nom"/></td>
    15:   	</tr>
    16:   	<tr>
    17:   		<td><bean:message key="index.prenom"/></td>
     
     
    Stacktrace:
    	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
     
     
    cause mère
    Si tu as une erreur ce n'est pas la peine d'aller plus loin, on n'essaye pas de faire fonctionner quelque chose s'il y a une erreur avant.
    De plus si tu donnais la cause mere (ou bien tu la lis p'etre que tu devineras tout seul ton probleme sur la JSP) ca pourrait p'etre nous aider

    Citation Envoyé par gentil
    Voici le bean action
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    package bean;
     
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import com.minosis.hibernate.dao.*;
    import net.sf.hibernate.*;
     
    public class Formaction extends Action{
     
    	public ActionForward execute (ActionMapping mapping,ActionForm form, HttpServletRequest Request, HttpServletResponse response) throws HibernateException
    	{
     
    		try
    		{
    			TContactDAO tcontactdao=new TContactDAO();
    			tcontactdao.ajoutpersonne(Request.getParameter("nom"),Request.getParameter("prenom"),Integer.parseInt(Request.getParameter("age")));
    		}
    		catch(Exception ex)
    		{
    			ex.printStackTrace();
    		}
     
    			return mapping.getInputForward();
    	}
     
    }
    Ca ne te dirais pas d'essayer de comprendre ce que tu fais. Tu utilises struts pour rien dans ton cas, tu fais un formulaire mais ne reprend pas les données du formulaire (pas très intéligent)
    *************************************************
    *************************************************
    IMPORTANT
    ====> Lit le avant toute chose :::::

    http://tahe.developpez.com/java/struts/

    Et s'il te plait essaye de comprendre un minimum car depuis 1 mois tu fais n'importe quoi et pose des questions inutiles car tout est écrit dans les faqs et tuto!!!


    ==> Si tu ne prends pas le temps de lire et de comprendres un minimum je ne vois pas pourquoi on prendrais du temps pour faire ton travail.
    *************************************************
    *************************************************

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    172
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 172
    Points : 60
    Points
    60
    Par défaut
    Je vous remercie j'ai trouvé en partie la solution dans ma jsp il fallait pas mettre le name.

    Il me rentre bien les informations pour le nom et l'age mais pas pour le prénom, je comprend pas bien pourquoi .

    J'ai réalisé certaine modification pour la jsp et le beanaction.

    voici ma derniere jsp:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    <%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>
    <%@ taglib uri="/WEB-INF/c" prefix="c" %>
    <html:html>
    <head>
    	<title></title>
    </head>
    <body>
     <html:form action="/doaction.do">
      <table>
      	<tr>
      		<td><bean:message key="index.nom" /></td>
      		<td><html:text property="nom"/></td>
      	</tr>
      	<tr>
      		<td><bean:message key="index.prenom"/></td>
      		<td><html:text property="prenom"/></td>
      </tr>
      <tr>
      		<td><bean:message key="index.age"/></td>
      		<td><html:text property="age"/></td>
      </tr>
      <tr>
      		<td><html:submit property="envoyer" value="envoyer"/></td>
      		<td><html:reset property="annuler" value="annuler"/></td>
      </tr>
      </table>
     
     </html:form>
     
    	<bean:write name="formulaire"  property="nom"/>
    	<bean:write  name="formulaire" property="prenom"/>
    	<bean:write name="formulaire" property="age"/>
    	   <html:errors property="Nom"/>
    </body>
    </html:html>
    voici mon beanaction:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    package bean;
     
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import com.minosis.hibernate.dao.*;
    import net.sf.hibernate.*;
     
    public class Formaction extends Action{
     
    	public ActionForward execute (ActionMapping mapping,ActionForm form, HttpServletRequest Request, HttpServletResponse response) throws HibernateException
    	{
     
    		try
    		{
    			String nom=((Form)form).getNom();
    			String prenom=((Form)form).getPrenom();
    			Integer age=((Form)form).getAge();
    			TContactDAO tcontactdao=new TContactDAO();
    			tcontactdao.ajoutpersonne(nom,prenom,age);
    		}
    		catch(Exception ex)
    		{
    			ex.printStackTrace();
    		}
     
    			return mapping.getInputForward();
    	}
     
    }
    Je vous remercie de votre aide

  7. #7
    Membre expérimenté Avatar de maxf1
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    1 229
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 229
    Points : 1 371
    Points
    1 371
    Par défaut
    Et bien voilà quand tu veux t'y arrives. La ca ressemble deja plus à quelque chose.

    Sinon pour le prenom c'est simple tu ne l'as pas indiqué dans le fichier de mapping.... Quand tu l'auras défini tout roulera comme sur des roulettes


    Ah oui dans ta jsp ta mis reset, fait gaffe reset != cancel

    Reset est cencé mettre ton formulaire à 0
    Annuler retourne en général à la page précédent la saisie

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    172
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 172
    Points : 60
    Points
    60
    Par défaut
    Merci de votre aide

    Le problème est résolu

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [MySQL] ajouter des fichiers dans une base de données phpmyadmin
    Par turki chaima dans le forum PHP & Base de données
    Réponses: 4
    Dernier message: 03/03/2014, 19h36
  2. Rechercher des informations dans une base de donnée en excel
    Par simonasjdp dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 13/07/2009, 19h10
  3. [Spip] Ajouter et lire des champs dans la base de données Spip
    Par tim1789 dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 4
    Dernier message: 20/12/2008, 23h21
  4. Ajouter des éléments dans une base de données Access
    Par bidule123456 dans le forum ASP
    Réponses: 74
    Dernier message: 07/09/2007, 14h00

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo