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 48 49 50 51 52 53 54 55 56
   |  
  // read a BLOB column from the database
  public static Image Lecture( int iDentifiant )
       throws Exception
  {
    // Register the Oracle JDBC driver
    DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
    Connection conn = null ;
    String     req = "" ;
    byte[]     buffer = null ;
    int        iLength = 0 ;
    Image      img = null ;
    // Connect to the database
     log("Lecture() : Connect to database") ;
    try {
      conn = DriverManager.getConnection (sConn, sUser, sPwd);
    } catch (Exception e) {
     log( "error connextion database : " + e.toString()) ;
    }
    conn.setAutoCommit (false);
    // Create a Statement
    Statement stmt = conn.createStatement ();
    req  = "select " + sColumn + ", LENGTH(" + sColumn + ") " ;
    req += "from " + sTable + " where " + sLookup + "=" + iDentifiant ;
    log("Lecture() : execute query="+req) ;
    ResultSet rset = stmt.executeQuery ( req );
    while (rset.next ())
    {
      // Get the lob
      blobImage = ((OracleResultSet)rset).getBLOB (1);
      iLength = rset.getInt(2) ;
    }
    // Close all resources
    rset.close();
    stmt.close();
    log("Lecture() : transform the BLOB length="+iLength) ;
    if( iLength > 0 )
    {
      InputStream instream = blobImage.getBinaryStream();
      // Create temporary buffer for read
      buffer = new byte[iLength];
      int length = 0;
      // Fetch data  
      while ((length = instream.read(buffer)) != -1)
      {
        log("Lecture() : Read " + length + " bytes: ");
      }
      // Close input stream
      instream.close();    
      conn.close(); 
      log("Lecture() : createImage()") ;
      img = Toolkit.getDefaultToolkit().createImage(buffer) ;
    }
    return img ;
 
  } | 
Partager