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

Développement Web en Java Discussion :

probleme Insert Id auto-increment


Sujet :

Développement Web en Java

  1. #1
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2012
    Messages
    277
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2012
    Messages : 277
    Points : 126
    Points
    126
    Par défaut probleme Insert Id auto-increment
    Bonjour,

    j'essai de faire un INSERT INTO :

    lorsque j'exécute ce code :

    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
    	private static final String SQL_INSERT = "INSERT INTO categorie (nom_categorie) VALUES ('?')";
    	@Override
    	public void creer(Categorie categorie) throws DAOException {
    		// TODO Auto-generated method stub
     
    		    Connection connexion = null;
    		    PreparedStatement preparedStatement = null;
    		    ResultSet valeursAutoGenerees = null;
     
    		    try {
    		    	System.out.println("connection et ajout de la catégorie");
    		        /* Récupération d'une connexion depuis la Factory */
    		        connexion = daoFactory.getConnection();
    		        preparedStatement = initialisationRequetePreparee( connexion, SQL_INSERT, true, categorie.getNom_categorie() );
    		        int statut = preparedStatement.executeUpdate();
    		        /* Analyse du statut retourné par la requête d'insertion */
    		        if ( statut == 0 ) {
    		            throw new DAOException( "Échec de la création de la categorie, aucune ligne ajoutée dans la table." );
    		        }
    		        /* Récupération de l'id auto-généré par la requête d'insertion */
    		        valeursAutoGenerees = preparedStatement.getGeneratedKeys();
    		        if ( valeursAutoGenerees.next() ) {
    		            /* Puis initialisation de la propriété id du bean categorie avec sa valeur */
    		            categorie.setId_categorie( valeursAutoGenerees.getInt("id_categorie") );
    		        } else {
    		            throw new DAOException( "Échec de la création de la categorie en base, aucun ID auto-généré retourné." );
    		        }
    		    } catch ( SQLException e ) {
    		    	 System.out.println("erreur"+e.getMessage());
    		    	throw new DAOException( e );
     
    		    } finally {
    		        fermeturesSilencieuses( valeursAutoGenerees, preparedStatement, connexion );
    		    }
    		}
    j'ai l'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
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    connection et ajout de la catégorie
    erreurParameter index out of range (1 > number of parameters, which is 0).
    fr.xxx.dao.DAOException: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
    	at fr.xxx.dao.CategorieDaoImpl.creer(CategorieDaoImpl.java:50)
    	at fr.xxx.form.CategorieForm.ajouterCategorie(CategorieForm.java:42)
    	at fr.xxx.servlet.Ajouter_categorie.doPost(Ajouter_categorie.java:52)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
    	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
    	at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3813)
    	at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3795)
    	at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3840)
    	at com.mysql.jdbc.PreparedStatement.setNull(PreparedStatement.java:3880)
    	at com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:4041)
    	at fr.xxx.dao.DAOUtilitaire.initialisationRequetePreparee(DAOUtilitaire.java:73)
    	at fr.xxx.dao.CategorieDaoImpl.creer(CategorieDaoImpl.java:34)
    	... 20 more
    Pourriez vous m'aider ? merci

  2. #2
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2012
    Messages
    277
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2012
    Messages : 277
    Points : 126
    Points
    126
    Par défaut
    C'est bon j'ai trouvé, ne pas mettre de " ' " lors d'un LIKE en MySQL...

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

Discussions similaires

  1. INSERT avec auto-increment
    Par sly60 dans le forum Paradox
    Réponses: 1
    Dernier message: 26/07/2011, 14h49
  2. Insert : id auto-incrementé et utilisation de cet id
    Par bého32 dans le forum Requêtes
    Réponses: 4
    Dernier message: 18/08/2009, 17h02
  3. probleme avec une auto-incremente
    Par tchimou dans le forum Bases de données
    Réponses: 1
    Dernier message: 30/05/2007, 14h28
  4. [JDBC] Insertion dans Access auto Increment
    Par sg-40 dans le forum JDBC
    Réponses: 4
    Dernier message: 09/11/2005, 22h14
  5. Réponses: 2
    Dernier message: 05/01/2004, 11h23

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