Discussions

EJB design: How to work with Blob using Swing and BC4J/ADF

  1. How to work with Blob using Swing and BC4J/ADF (2 messages)

    We are using swing clients and BC4J/ADF framework in the middle-tire.
    We need to store Images of employees and photos of the machinary in the database, for which we defined the column of blob type.
    can someone help me in understanding how to map the file in the client to blob in the database?
  2. I had to do exactly that in the project I'm working on.
    What I did was treat the file as any other data: serialize it (read the file into a byte array) tranfer to server (using a normal EJB call + Value Object) and insert in the DB. The only tricky part was that some BD-specific code was needed to perform the INSERT of the BLOB field. In oracle, I had to INSERT with EMPTY_BLOB as the value of the field, SELECT the record and UPDATE the field. Since we use DAOs for persistence, this hideous code was propperly hidden from everyone except my guilty, inner, purist programmer.

    Cheers and happy coding,
    Martin
  3. It's still not working[ Go to top ]

    after doing what you tell in the previous post.

    I have the same problem or one very similar.

    I've develop a Swing App and I want to connect to my ORACLE DB an insert a BLOB.

    if my sql sentence include "Select.... FOR UPDATE" or ".... FOR UPDATE OF [BlobFieldName]" I got the error
    ORA-01002: fetch out of sequence

    And if I remove the "FOR UPDATE" I got:
    ORA-22920: row containing the LOB value is not locked

    I have also tried to run before teh sql sentence:
    "UPDATE PER_DATOS_PERSONALES SET FOTO=EMPTY_BLOB() WHERE PK_Field='keyFieldValue'"

    but I got the same erros.

    What I'm doing wrong ?

    I paste below the code I'm running

    Thanx in advance

    ...

    Statement stmt = this.conexion.createStatement();
    //Statement stmtPREV = this.conexionPREV.createStatement();


    //Execute the query and lock the blob row
       String sCondicion = "DNI = 'myValueField'";
       String sTabla= new String("PER_DATOS_PERSONALES");
       String sCampoClave= new String("FOTO");

    cmd = " SELECT " + sCampoClave + " FROM " + sTabla + " WHERE " + sCondicion+ " FOR UPDATE";// OF FOTO ";
    ResultSet rset = stmt.executeQuery(cmd);
    rset.next();

    //Get blob locator
    blob = ((OracleResultSet)rset).getBLOB(1);

    binFile = new File("E:\\FOTOS_GIF
    fileName.GIF");
    instream = new FileInputStream(binFile);

    //Insert to the BLOB from an output stream
    outstream = blob.getBinaryOutputStream();

    //Read the input stream and write the output stream by chunks
    byte[] chunk = new byte[blob.getChunkSize()];
    int i = -1;

    while((i = instream.read(chunk))!=-1)
    {
    outstream.write(chunk, 0, i);
    }

    //Close the input and output stream.
    instream.close();
    outstream.close();

    this.conexion.commit();

    //Close the statement.
    stmt.close();

    }catch(Exception e1)
    {
      System.out.println("(*) ERROR: "+e1.getMessage());
    }