Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: Inserting data into LONG RAW fields

Re: Inserting data into LONG RAW fields

From: Thomas Kyte <tkyte_at_us.oracle.com>
Date: Wed, 10 Mar 1999 19:06:03 GMT
Message-ID: <36f1c256.22689425@192.86.155.100>


A copy of this was sent to Theodore Williams <twilliam_at_leaf.gsfc.nasa.gov> (if that email address didn't require changing) On Wed, 10 Mar 1999 11:32:19 -0500, you wrote:

>I plan to use java. Perl is my second choice. So any java examples would be
>helpful.

assuming you have a table such as:

create table blob_test( name varchar2(30), data long raw );

then a java program like:

import java.io.*;
import java.sql.*;
import java.util.*;

class BlobTest
{

public static void main(String args[])
{

    Statement statement = null;
    Connection connection = null;

    if ( args.length != 4 )
    {

        System.out.println(
            "Usage: java BlobTest <size> <username> <password>
<connection>" );
        return;

    }

    try
    {
    Properties props = new Properties();

    props.put( "user", args[1] );
    props.put( "password", args[2] );

    Class.forName( "oracle.jdbc.driver.OracleDriver" );

    String url = args[3];

    System.out.println( "url = '" + url + "'" );

    connection = DriverManager.getConnection( url, props );

    DatabaseMetaData dmd = connection.getMetaData();

    System.out.println( "database = " + dmd.getDatabaseProductVersion() );     System.out.println( "driver = " + dmd.getDriverName() + " " +

                         dmd.getDriverVersion() );
    System.out.println( "Connect = " + url );

    statement = connection.createStatement();

    int size = ( new Integer( args[0] )).intValue();     System.out.println( "size = " + size );

    byte[] buffer1 = new byte[size];

    for( int i = 0; i < size; i++ ) buffer1[i] = (byte)(i+'A');

    PreparedStatement ps = connection.prepareStatement(

        "insert into blob_test ( name, data ) values ( ?, ? )" );

    ps.setString( 1, "test" );
    ps.setBytes( 2, buffer1 );

    int rows = ps.executeUpdate();
    ps.close();

    statement.execute( "select data from blob_test where name = 'test'" );     //statement.execute( "select
dbms_lob.substr(data,dbms_lob.getlength(data),1)"

                        //+ " from blob_test where name = 'test'" );

    ResultSet resultSet = statement.getResultSet();     resultSet.next();

    byte[] buffer2 = resultSet.getBytes(1);

    resultSet.close();

    System.out.println( "put in " + buffer1.length + " bytes got back " +

                         buffer2.length + " bytes" );

    boolean buffersMatch = true;
    int i = 0;

    if ( buffer1.length == buffer2.length )     {

        System.out.println( "Got out as much as we put in" );

        for( i = 0; i < buffer1.length; i++ )
            if ( buffer1[i] != buffer2[i] ) break;

        if ( i < buffer1.length ) System.out.println( "mismatch at byte " +
i);
        else System.out.println( "Ok, match" );
    }
    else System.out.println( "don't match, different lengths" );

    }
    catch( Exception e )
    {

        e.printStackTrace();
    }

}

}

will do it. I used the thin jdbc drivers and run it as:

$ java BlobTest 1000000 scott tiger jdbc:oracle:thin:@slackdog:1521:oracle8

>Also is it only possible to enter image files into a long raw field
>through a language interface? Are there no Oracle tools which allow this?
>

sqlldr can do this, but only file (easily) at a time. If you need to know how, let me know. Its tricky. I use a small C program that looks at the file, gets the byte size and creates a control file for THAT file ( a unique control file for each file must be used if you want to use sqlldr ).

If you want to see a C example, i have that as well.

>Thanks,
>
>Theo
>
>Thomas Kyte wrote:
>
>> A copy of this was sent to Theodore Williams <twilliam_at_leaf.gsfc.nasa.gov>
>> (if that email address didn't require changing)
>> On Tue, 09 Mar 1999 14:30:19 -0500, you wrote:
>>
>> >How do you insert data from an image file into a long raw field? Where
>> >can I find examples? This seems like a simple question, but I have not
>> >been able to find the answer. I know it would be easier to do this with
>> >lobs in Oracle 8, but we want to do this with an Oracle 7 database. I
>> >have seen one reference to the LONG API (Is there a LONG RAW API?), but
>> >have not seen it.
>> >
>> >Theo
>>
>> What language are you using?
>>
 

Thomas Kyte
tkyte_at_us.oracle.com
Oracle Service Industries
Reston, VA USA

--
http://govt.us.oracle.com/ -- downloadable utilities  



Opinions are mine and do not necessarily reflect those of Oracle Corporation Received on Wed Mar 10 1999 - 13:06:03 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US