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

JDBC Java Discussion :

[Clob]Update d'un champ Clob


Sujet :

JDBC Java

  1. #1
    Membre éprouvé Avatar de leminipouce
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2004
    Messages
    754
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2004
    Messages : 754
    Points : 1 253
    Points
    1 253
    Par défaut [Clob]Update d'un champ Clob
    Bonjour tout le monde,

    Je n'arrive désespérement pas à mettre à jour un Clob (initialement null) dans ma BD Oracle 10g.
    J'utilise le dernier driver ojdbc14.jar que j'ai téléchargé hier, mais rien à faire.

    Voici les différentes solutions que j'ai essayé.
    Avec un StringReader:
    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
                String sqlCmd = "UPDATE catalog SET modification_date = SYSDATE, status = 1, description = '"
                    + this.getDescription() + "', search_text = '?' WHERE obj_id = " + this.getId();
     
                logger.fine("CatalogItem.updateCatalog().sqlCmd: " + sqlCmd);
     
                pst = conn.prepareStatement(sqlCmd);
    //            pst.executeUpdate(sqlCmd);
     
                // Save data to store in the CLOB
                StringBuffer myClobValue = new StringBuffer("Clob non vide");
     
                // Retrieve all data from the attributes to populate the CLOB
                DataTypeAttribute[] dataTypeAttributes = getDataType().getAttributes();
                for (int i = 0; i < dataTypeAttributes.length; i++)
                {
                    logger.fine(" -> attributes[i].getName(): "+attributes[i].getName());
     
                    // Append the value to the CLOB buffer if the att is global
                    // searchable and its value is not null.
                    if (attributes[i].isGlobalSearchable()
                            && this.getAttributeValue(attributes[i].getName()) != null
                            && !this.getAttributeValue(attributes[i].getName()).equals("null"))
                    {
                        myClobValue.append(this.getAttributeValue(attributes[i].getName()));
                        myClobValue.append(";");
                    }
                }
     
                logger.fine(" -> ClobValue: " + myClobValue.toString());
     
                stringReader = new StringReader(myClobValue.toString());
                pst.setCharacterStream(1,stringReader,myClobValue.toString().length());
     
                pst.executeUpdate(sqlCmd);
    Avec un AsciiStream
    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
                String sqlCmd = "UPDATE catalog SET modification_date = SYSDATE, status = 1, description = '"
                    + this.getDescription() + "', search_text = '?' WHERE obj_id = " + this.getId();
     
                logger.fine("CatalogItem.updateCatalog().sqlCmd: " + sqlCmd);
     
                pst = conn.prepareStatement(sqlCmd);
    //            pst.executeUpdate(sqlCmd);
     
                // Save data to store in the CLOB
                StringBuffer myClobValue = new StringBuffer("Clob non vide");
     
                // Retrieve all data from the attributes to populate the CLOB
                DataTypeAttribute[] dataTypeAttributes = getDataType().getAttributes();
                for (int i = 0; i < dataTypeAttributes.length; i++)
                {
                    logger.fine(" -> attributes[i].getName(): "+attributes[i].getName());
     
                    // Append the value to the CLOB buffer if the att is global
                    // searchable and its value is not null.
                    if (attributes[i].isGlobalSearchable()
                            && this.getAttributeValue(attributes[i].getName()) != null
                            && !this.getAttributeValue(attributes[i].getName()).equals("null"))
                    {
                        myClobValue.append(this.getAttributeValue(attributes[i].getName()));
                        myClobValue.append(";");
                    }
                }
     
                logger.fine(" -> ClobValue: " + myClobValue.toString());
     
                InputStream is = new ByteArrayInputStream(myClobValue.toString().getBytes());
                pst.setAsciiStream(1, is, myClobValue.toString().length());
     
                pst.executeUpdate(sqlCmd);
    En instanciant d'abord mon clob en Empty Clob :
    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
    46
    47
                String sqlCmd = "UPDATE catalog SET modification_date = SYSDATE, status = 1, description = '"
                     + this.getDescription() + "' WHERE obj_id = " + this.getId();
     
                 logger.fine("CatalogItem.updateCatalog().sqlCmd: " + sqlCmd);
     
                 pst = conn.prepareStatement(sqlCmd);
                 pst.executeUpdate(sqlCmd);
     //Ici les données sont mise à jour correctement !
     
                 // Save data to store in the CLOB
                 StringBuffer myClobValue = new StringBuffer("Clob non vide"); //Juste pour m'assurer que dans tous les cas je mets au moins quelque chose dans les clob
     
                 // Retrieve all data from the attributes to populate the CLOB
                 DataTypeAttribute[] dataTypeAttributes = getDataType().getAttributes();
                 for (int i = 0; i < dataTypeAttributes.length; i++)
                 {
                     logger.fine(" -> attributes[i].getName(): "+attributes[i].getName());
     
                     // Append the value to the CLOB buffer if the att is global
                     // searchable and its value is not null.
                     if (attributes[i].isGlobalSearchable()
                             && this.getAttributeValue(attributes[i].getName()) != null
                             && !this.getAttributeValue(attributes[i].getName()).equals("null"))
                     {
                         myClobValue.append(this.getAttributeValue(attributes[i].getName()));
                         myClobValue.append(";");
                     }
                 }
     
                 logger.fine(" -> ClobValue: " + myClobValue.toString());
     
                             // Create the CLOB in the database (or reset it)
                stmt = conn.createStatement();
                stmt.execute("UPDATE catalog SET search_text=empty_clob() WHERE obj_id = " + this.getId());
     
                // Then retrieve its instance
                String sqlQuery = "SELECT search_text FROM catalog WHERE obj_id = " + this.getId();
                ResultSet clobRs = stmt.executeQuery(sqlQuery);
                // Only one loop since the select filters on the obj_id.
                while (clobRs.next())
                {
                    // Get the lobs
                    CLOB textStringClob = ((OracleResultSet) clobRs).getCLOB("search_text");
                    // and populate it
                    DataBaseUtil.fillClob(textStringClob, myClobValue.toString());
                }
                clobRs.close();
    Avec mon fillClob comme suit :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
         public static void fillClob(CLOB clob, String buff) throws SQLException, IOException
        {
            Writer outstream = clob.getCharacterOutputStream();
     
            outstream.write(buff, 0, buff.length());
            outstream.close();
        }
    Et rien ne marche !

    Dans les 2 premiers cas, j'ai un clob dont la longueur vaut 1 et le contenu est '?' (sans les quotes. Qu'elles y soient ou pas dans la requête), et dans le dernier cas, j'ai un clob vide ! (length = 0).
    Si , et la ont échoué mais pas nous, pensez à dire et cliquez sur . Merci !

    Ici, c'est un forum, pas une foire. Il y a de respectables règles... à respecter !

  2. #2
    Membre éprouvé Avatar de leminipouce
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2004
    Messages
    754
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2004
    Messages : 754
    Points : 1 253
    Points
    1 253
    Par défaut


    Ca mérite au moins tout ça !!!

    J'avais encore l'ancienne librairie jdbc (jdbc9iclass12) dans ma liste de librairie, en plus de la ojdbc14. Résultat il continuait d'utiliser l'ancienne. Va savoir pourquoi à la base ils avaient mis les 2 librairies !!! Bref, j'ai supprimé l'ancienne et tout marche nickel.
    Notez au passage le FOR UPDATE dans ma requête select. Indispensable.

    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
                stmt = conn.createStatement();
                stmt.execute("UPDATE catalog SET search_text=empty_clob() WHERE obj_id = " + this.getId());
                
                // Then retrieve its instance
                String sqlQuery = "SELECT search_text FROM catalog WHERE obj_id = " + this.getId()+ " FOR UPDATE";
                ResultSet clobRs = stmt.executeQuery(sqlQuery);
                // Only one loop since the select filters on the obj_id.
                while (clobRs.next())
                {
                    Clob textStringClob = clobRs.getClob("search_text");
                    
                    textStringClob.setString(1, myClobValue.toString());
                    
                    String newSqlQuery = "UPDATE maincatalog SET search_text = ? WHERE obj_id = " + this.getId();
                    pst = conn.prepareStatement(newSqlQuery);
                    pst.setClob(1, textStringClob);
                    pst.executeUpdate();
                }
                clobRs.close();
    Si , et la ont échoué mais pas nous, pensez à dire et cliquez sur . Merci !

    Ici, c'est un forum, pas une foire. Il y a de respectables règles... à respecter !

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

Discussions similaires

  1. ETL capable d'Extraire du XML depuis champ CLOB et le Parser
    Par anis1977 dans le forum Alimentation
    Réponses: 2
    Dernier message: 01/03/2024, 09h14
  2. Manipulation des champs CLOB
    Par FABFAB125 dans le forum SQL
    Réponses: 2
    Dernier message: 23/02/2007, 11h50
  3. [SQL] Comment lire un champ CLOB
    Par scorpking dans le forum PHP & Base de données
    Réponses: 6
    Dernier message: 22/05/2006, 09h47
  4. Convertir un champ CLOB
    Par nourdev dans le forum Oracle
    Réponses: 37
    Dernier message: 21/03/2006, 12h26
  5. [Oracle] Insérer le texte d'un textarea dans un champ CLOB
    Par kum dans le forum PHP & Base de données
    Réponses: 1
    Dernier message: 26/10/2005, 17h29

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