DBAsupport.com Forums - Powered by vBulletin
Results 1 to 3 of 3

Thread: again images

  1. #1
    Join Date
    Jan 2001
    Posts
    153
    SQLWKS> create table abc
    2> (testfile blob)
    Statement Processed

    Now..from sql or pl/sql how do i insert an image...
    Vijay.s

  2. #2
    Join Date
    Aug 2001
    Location
    Waterloo, On
    Posts
    547
    You have to convert image to a binary format before it can be inserted and then reconverted to original form for retrieval.

    Here is an Example using PHP:

    http://www.phpbuilder.com/forum/arch...001/7/3/120558

    Raminder Singh

    Oracle Certified DBA: Oracle 8i, 9i


    Mail me at [email protected].

  3. #3
    Join Date
    Aug 2001
    Location
    Waterloo, On
    Posts
    547
    Following is an example from OTN FAQs:

    Code:
    This sample writes the GIF file john.gif to a BLOB. 
    
    */ 
    
    import java.sql.*;
    import java.io.*;
    import java.util.*;
    
    // Importing the Oracle Jdbc driver package makes the code more readable
    import oracle.jdbc.driver.*;
    
    //needed for new CLOB and BLOB classes
    import oracle.sql.*;
    
    public class LobExample
    {
      public static void main (String args [])
           throws Exception
      {
        // Register the Oracle JDBC driver
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    
        // Connect to the database
        // You can put a database name after the @ sign in the connection URL.
        Connection conn =
          DriverManager.getConnection ("jdbc:oracle:oci8:@", "scott", "tiger");
    
        // It's faster when auto commit is off
        conn.setAutoCommit (false);
    
        // Create a Statement
        Statement stmt = conn.createStatement ();
    
        try
        {
          stmt.execute ("drop table persons");
        }
        catch (SQLException e)
        {
          // An exception could be raised here if the table did not exist already.
        }
    
        // Create a table containing a BLOB and a CLOB
        stmt.execute ("create table persons (name varchar2 (30), picture blob)");
        
        // Populate the table
        stmt.execute ("insert into persons values ('John', EMPTY_BLOB())");
        
        // Select the BLOB 
        ResultSet rset = stmt.executeQuery ("select picture from persons where name 
    = 'John'");
        if (rset.next ())
        {
          // Get the BLOB locator from the table
          BLOB blob = ((OracleResultSet)rset).getBLOB (1);
    
          // Declare a file handler for the john.gif file
          File binaryFile = new File ("john.gif");
    
          // Create a FileInputStream object to read the contents of the GIF file
          FileInputStream istream = new FileInputStream (binaryFile);
    
          // Create an OutputStram object to write the BLOB as a stream
          OutputStream ostream = blob.getBinaryOutputStream ();
    
          // Create a tempory buffer  
          byte[] buffer = new byte[1024];
          int length = 0;
    
          // Use the read() method to read the GIF file to the byte 
          // array buffer, then use the write() method to write it to 
          // the BLOB.
          while ((length = istream.read(buffer)) != -1)
            ostream.write(buffer, 0, length);
    
          // Close the inputstream and outputstream
          istream.close();
          ostream.close();
    
          // Check the BLOB size
          System.out.println ("Number of bytes written = "+blob.length());
        }
    
        // Close all resources
        rset.close();
        stmt.close();
        conn.close(); 
      }
    }
    
    
    Note that you'll get even better performance if you use DBMS_LOB.LOADFROMFILE() instead of using DBMS_LOB.WRITE(). 
    
    In order to be able to use DBMS_LOB.LOADFROMFILE(), the data to be written into the LOB must be in a server-side file.

    If there is something easier, I am interested !!


    Raminder Singh

    Oracle Certified DBA: Oracle 8i, 9i


    Mail me at [email protected].

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width