Probleme d'insertion dans la base de donnée

Résolu
cs_redbar Messages postés 112 Date d'inscription mardi 26 janvier 2010 Statut Membre Dernière intervention 26 mai 2014 - 7 nov. 2011 à 18:54
cs_GodConan Messages postés 2113 Date d'inscription samedi 8 novembre 2003 Statut Contributeur Dernière intervention 6 octobre 2012 - 8 nov. 2011 à 18:55
Bonjour à tous

J'ai besoin d'aide je cale sur un probleme.
En fait, j'ai une table facture dans laquelle j'insere des données. quand la table est vide et que je commence à inserrer des données il n'y a pas de problème. Mais il arrive à un moment de l'execution une erreur que je ne n'arrive pas à comprendre.
Dont voici:

org.postgresql.util.PSQLException: ERREUR: la valeur d'une clé dupliquée rompt la contrainte unique « facture_pkey »
  Détail : La clé « (id_fact)=(18) » existe déjà.
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2062)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1795)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:479)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:353)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:299)
at mti.SelectionBD.insertionFacture(SelectionBD.java:448)
at mti.Vente.(Vente.java:91)
at mti.RechercheClient$3.actionPerformed(RechercheClient.java:181)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.Dialog$1.run(Unknown Source)
at java.awt.Dialog$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Dialog.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at java.awt.Dialog.setVisible(Unknown Source)
at mti.RechercheClient.(RechercheClient.java:55)
at mti.MenuPrincipal$19.actionPerformed(MenuPrincipal.java:430)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)


Quand je regarde le code qui lève cette exception je ne comprend pas où ça ne marche pas .
Voici le code de l'insertion:

public void insertionFacture(String dateFacture, int numClient){

try{
String requete ="INSERT INTO facture(id_fact,datfact,id_client)VALUES('"+(afficherNumeroFactureCourante()+1)+"','"+dateFacture+"','"+numClient+"')";
Statement state = Maconnexion.getInstance().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
resInsFacture = state.executeUpdate(requete);
state.close();
}catch(SQLException er){
er.printStackTrace();
JOptionPane.showMessageDialog(null, er.getMessage(), getTitle(), JOptionPane.ERROR_MESSAGE);
}
}


j'explique comment marche cette partie du code. Mais bien avant je rappelle que j'utilise postgresql comme moteur de base de données.

Avant d’insérer une nouvelle facture dans la table, j'ai écris un code qui va récupérer le numéro de la dernière facture insérée.
A ce numéro j'ajoute 1 pour en faire le numéro de la nouvelle facture.
c'est à dire si bien avant la base contenait 10 factures insérées, je récupère ce 10 et je fais 10+1 = 11 et c'est le 11 qui devient le numéro de la nouvelle facture.

Mais après un certain nombre de fois d’exécution du programme au lieu que ce code qui me permet de recuperer le numéro de la dernière facture ne marche plus bien car il ne renvoie plus le numéro de la dernière facture;
voici le code de recupération du numéro de la dernière facture;

public int afficherNumeroFactureCourante(){
int numFac = 0;
try{
Statement stat = Maconnexion.getInstance().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet resultat = stat.executeQuery("SELECT * FROM facture");
resultat.last();
numFac = resultat.getRow();

stat.close();
resultat.close();
}catch(SQLException er){
er.printStackTrace();
}
return numFac;
}


Voici la structure de la table facture de ma base de données;

CREATE TABLE facture
(
  id_fact integer NOT NULL,
  datfact date NOT NULL,
  netapayer integer NOT NULL DEFAULT 0,
  id_client integer,
  restapayer integer DEFAULT 0,
  remise integer DEFAULT 0,
  montantpayer integer DEFAULT 0,
  montant_total integer DEFAULT 0,
  CONSTRAINT facture_pkey PRIMARY KEY (id_fact),
  CONSTRAINT facture_id_client_fkey FOREIGN KEY (id_client)
      REFERENCES client (id_client) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=TRUE
);
ALTER TABLE facture OWNER TO postgres;
COMMENT ON TABLE facture IS 'Table contenant la liste des factures';


Merci pour votre aide

10 réponses

cs_GodConan Messages postés 2113 Date d'inscription samedi 8 novembre 2003 Statut Contributeur Dernière intervention 6 octobre 2012 12
8 nov. 2011 à 06:37
sinon si ton id est numérique n as tu pas avec postgress une fonction du style max(champ) pour obtenir ton id le plus grand? dans une requete qui resemblerait à "select max( id_fact ) from client;" ???
avec ca tu serai sur d avoir le plus grand ;o) ... quand à stocker es id dispo si ton prog est le seul qui puisse inserrer des données dans cette table ;o) tu as tout plein de moyen en java ;o) de le faire ;o) ...

GodConan ;o)
3