<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">// testBlob.java
//
// This class demonstrates inserting a new Blob value into an Oracle 8 database and
// updating an existing Blob value in the database.
//
// The test() method is a simple test that compares Blob values written to the database 
// to values read back.

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

class testBlob
    {
    testBlob(java.sql.Connection conn)
        {
        m_conn = conn;
        }
    // final int m_testlength = 0x100000;      // one meg
    int m_testlength = 1024* 3;      // 3k

    public void addData() throws SQLException
        {
        try
            {
            m_testlength = 1048576;      // one meg ( 1048576)

            byte[]val= new byte[m_testlength];
            String idx = String.valueOf(m_testlength);
            generateData(idx, val); // generate test data 
            java.sql.PreparedStatement stmt = m_conn.prepareStatement("insert into TESTBLOB values(?, Empty_Blob())");
            stmt.setString(1, idx);
            stmt.executeUpdate();
            m_conn.commit();
            stmt.close();
            // reselect the same row so we can update it
            java.sql.Statement select = m_conn.createStatement();
            java.sql.ResultSet rs = select.executeQuery("select val from TESTBLOB where ind = '1048576' for update");
            if(rs.next())       // should find the row
                {
                ByteArrayInputStream instr = new ByteArrayInputStream(val);
                rs.updateBinaryStream(1,instr , m_testlength);
                rs.updateRow();
                m_conn.commit();
                select.close();
                }         
            else {
                System.out.println("addData: failed to find row");
            }

            }
        catch (SQLException sch_ex) 
            {
            System.err.print ("SQLException: ");
            System.err.print (sch_ex.getMessage());
            }
        catch (Exception e) 
            {
            e.printStackTrace();
            }
        }
    
    public void createTable() throws SQLException
        {
        java.sql.Statement stmt = m_conn.createStatement();

        try
            {
            stmt.executeUpdate("drop table TESTBLOB");
            }
        catch (SQLException sch_ex) 
            {
            System.err.print ("SQLException: ");
            System.err.print (sch_ex.getMessage());
            }
        catch (Exception e) 
            {
            e.printStackTrace();
            }
        try
            {
            stmt.executeUpdate("create table TESTBLOB(ind varchar2(30), val blob)");
            }
        catch (SQLException sch_ex) 
            {
            System.err.print ("SQLException: ");
            System.err.print (sch_ex.getMessage());
            }
        catch (Exception e) 
            {
            e.printStackTrace();
            }
        }

    // generate test data 
    private void generateData(String indexkey, byte[] val)
        {
        int idx = 0;
        while(idx &lt; m_testlength - 100)
            {
            val[idx++] = (byte)idx;       // add 10 characters values
            }
        while(idx &lt; m_testlength)
            val[idx++] = (byte)'x';
        }

    public void test()
        {
        try
            {
            java.sql.Statement select = m_conn.createStatement();
            java.sql.ResultSet rs = select.executeQuery("select ind, val from TESTBLOB");
            // Class1.printHeaders(rs);
            while(rs.next())
                {
                String str = rs.getString(1);
                Integer len_val = new Integer(str);
                int len_shouldbe = len_val.intValue();
                java.sql.Blob blb   = rs.getBlob(2);
                long len = blb.length();
                if(len != len_shouldbe)
                    {
                    System.out.println("blob test failure, len should of been " + str + " but was " + len);
                    }

                byte[] bval= blb.getBytes(1, (int)len);
                
                String strSub = new String(bval);
                m_testlength = len_shouldbe;
                byte[] testval= new byte[m_testlength];
                generateData(str, testval); // generate test data 
                String strTestVal = new String(testval);
                if(strTestVal.length() != strSub.length())
                    {
                    System.out.println("blob test failure, actual length compare should of been " + strTestVal.length() + " but was " + strSub.length());
                    }

                int icmp = strSub.compareTo(strTestVal);
                if(  icmp == 0)
                    {
                    System.out.println("blob test succeeded for length = " + str);
                    }
                else
                    {
                    System.out.println("blob test failed for length = " + str);
                    }
                }
            }
        catch (SQLException sch_ex) 
            {
            System.err.print ("SQLException: ");
            System.err.print (sch_ex.getMessage());
            }
        catch (Exception e) 
            {
            e.printStackTrace();
            }
        }

    java.sql.Connection m_conn;
    }
</pre></body></html>