Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unable to insert big blob data into table

Posted on 2003-10-30
11
Medium Priority
?
3,808 Views
Last Modified: 2008-03-03
Dear Experts,
I'm trying to select a list of record from one table and trying to insert them into another table.
During the process, all big blob size can not be inserted, and the following error came out :
Data size bigger than max size for this type: 1653752

The following is my java code.

/*
 * blob.java
 *
 * Created on October 22, 2003, 4:06 PM
 */

/**
 *
 * @author  ffn6dq
 */
import java.sql.*;
import java.io.*;
import java.lang.*;
import oracle.sql.*;

public class blob {
    Connection conn;
    Connection connActionRe;
    String oracleConnString;
    int i = 0;
    byte[] allBytesInBlob;
    /** Creates a new instance of blob */
    public blob() {
        byte[] BytesInBlob = runGetBLOB();
    }
   
    public byte[] runGetBLOB()
    {
         try
         {  
             byte[] allBytesInBlob;
             dbConnect();
             // Prepare a Statement:
             PreparedStatement stmnt = conn.prepareStatement("select attachment_file,ATTACHMENT_ID from qa_attachment");

             // Execute
             ResultSet rs = stmnt.executeQuery();
             System.out.println("test");
             while(rs.next())
             {
                try
                {
                   i = i + 1;
                   // Get as a BLOB
                   String s = rs.getString(2);
                   System.out.println("AtthID : " + s);
                   Blob aBlob = rs.getBlob(1);
                   
                   System.out.println("length : " + aBlob.length());
                   allBytesInBlob = aBlob.getBytes(1, (int) aBlob.length());
                   
                   System.out.println("bytes length : " + allBytesInBlob);
                   insertBlob(allBytesInBlob);
                   
                }
                catch(Exception ex)
                {
                   // The driver could not handle this as a BLOB...
                   // Fallback to default (and slower) byte[] handling
                   byte[] bytes = rs.getBytes(1);
                }
             }

           // Close resources
           rs.close();
           stmnt.close();
           dbClose();
           
         }
         catch(Exception ex)
         {
           //this.log("Error when trying to read BLOB: " + ex.getMessage());
             System.out.println("Error when trying to read BLOB: " + ex.getMessage());
         }
         return allBytesInBlob;
    }
   
    public void insertBlob(byte[] my_byte_array){
        try{
           
            String strSQL = "";
            strSQL = "INSERT INTO QA_ATTACHMENT (ATTACHMENT_ID, ATTACHMENT_FILE, ACTION_NUMBER) " +
                     "VALUES (?,?,?)";
            dbConnectARe();
            PreparedStatement pstmt = connActionRe.prepareStatement(strSQL);
            String stri = Integer.toString(i);
            pstmt.setString(1,stri);
            pstmt.setBytes(2,my_byte_array);
            pstmt.setString(3,"1");
            pstmt.executeUpdate();
            dbCloseARe();
        }catch(Exception ex){
            System.out.println("Error : " + ex.getMessage());
        }
    }

   
    public void dbConnect() throws SQLException, IOException {
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        conn = DriverManager.getConnection("jdbc:oracle:thin:@onimqs2.onsemi.com:1521:dbonimqs","actionre","actionre");
    }
   
    public void dbConnectARe() throws SQLException, IOException {
        oracleConnString = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.242.65.222)(PORT=1521))(CONNECT_DATA=(SID=myondb1)))";
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        connActionRe = DriverManager.getConnection(oracleConnString ,"actionre", "actionre");
    }
   
    public void dbClose() throws SQLException, IOException {
        conn.close();
    }
   
     public void dbCloseARe() throws SQLException, IOException {
        connActionRe.close();
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws SQLException, IOException {
        blob myBlob = new blob();
    }
   
}


I hope experts could help me.
Thanks,
Nasa
0
Comment
Question by:nasarudin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 9

Expert Comment

by:yongsing
ID: 9655219
>> Data size bigger than max size for this type: 1653752

That's the problem. The data that you are trying to insert is bigger than the maximum size of the column. You will have to alter the column to make it bigger.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 9655535
I don't know what are the different blob objects in oracle are available. But you need to use object similar to longblob(MySQL) for your column.

blob and and shortblob of MySQL has limitations. and i think the same situation may be3 there for other database softwares also. so check the oracle help and identify the object similar to longbob
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 9655573
And also by default blob,clob and bfile of oracle supports upto 4 gb. please check your column type in your second table first. 4Gb means it is more than exception value.

Regards
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 35

Expert Comment

by:TimYates
ID: 9656531
This is the age old problem with Oracle...

Blobs and JDBC aren't really transparent...

You have to follow these steps:

http://www.wamoz.com/JDBC_and_Oracle_LOB.asp

basically, you do 2 calls, one to set your non-blob data, and set the blob to empty_blob()

then another one to get a handle back from this empty blob

you then stream your data into this handle...

Hope this gets you in the right direction :-)

Tim.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 9656546
Hi Tim,
I think the article is removed from this http://www.wamoz.com/JDBC_and_Oracle_LOB.asp link. Raising page not found error


do you have info related to that

Regards,
Sudha
0
 
LVL 35

Accepted Solution

by:
TimYates earned 300 total points
ID: 9656561
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 9656569
Thank You:)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9656571
:-)
0
 

Author Comment

by:nasarudin
ID: 9668508
Tim,
I change my code and follow the artikel like what you said, but I got :
ORA-22920 : row containing the LOB value is not locked

Below is my code :

/*
 * blob.java
 *
 * Created on October 22, 2003, 4:06 PM
 */

/**
 *
 * @author  ffn6dq
 */
import java.sql.*;
import java.io.*;
import java.lang.*;
import oracle.sql.*;

public class blob {
    Connection conn;
    Connection connActionRe;
    String oracleConnString;
    int i = 0;
    byte[] allBytesInBlob;
    /** Creates a new instance of blob */
    public blob() {
        byte[] BytesInBlob = runGetBLOB();
    }
   
    public byte[] runGetBLOB()
    {
         try
         {  
             byte[] allBytesInBlob;
             dbConnect();
             // Prepare a Statement:
             PreparedStatement stmnt = conn.prepareStatement("select attachment_file,ATTACHMENT_ID from qa_attachment1 where rownum=1");

             // Execute
             ResultSet rs = stmnt.executeQuery();
             System.out.println("test");
             while(rs.next())
             {
                try
                {
                   i = i + 1;
                   // Get as a BLOB
                   String s = rs.getString(2);
                   System.out.println("AtthID : " + s);
                   Blob aBlob = rs.getBlob(1);
                   
                   System.out.println("length : " + aBlob.length());
                   allBytesInBlob = aBlob.getBytes(1, (int) aBlob.length());
                   
                   System.out.println("bytes length : " + allBytesInBlob);
                   //insertBlob(allBytesInBlob);
                   OracleBlob(aBlob);
                   
                }
                catch(Exception ex)
                {
                   // The driver could not handle this as a BLOB...
                   // Fallback to default (and slower) byte[] handling
                   byte[] bytes = rs.getBytes(1);
                }
             }

           // Close resources
           rs.close();
           stmnt.close();
           dbClose();
           
         }
         catch(Exception ex)
         {
           //this.log("Error when trying to read BLOB: " + ex.getMessage());
             System.out.println("Error when trying to read BLOB: " + ex.getMessage());
         }
         return allBytesInBlob;
    }
   
    public void insertBlob(byte[] my_byte_array){
        try{
           
            String strSQL = "";
            strSQL = "INSERT INTO QA_ATTACHMENT1 (ATTACHMENT_ID, ATTACHMENT_FILE, ACTION_NUMBER) " +
                     "VALUES (?,?,?)";
            dbConnectARe();
            PreparedStatement pstmt = connActionRe.prepareStatement(strSQL);
            String stri = Integer.toString(i);
            pstmt.setString(1,stri);
            pstmt.setBytes(2,my_byte_array);
            pstmt.setString(3,"1");
            pstmt.executeUpdate();
            dbCloseARe();
        }catch(Exception ex){
            System.out.println("Error : " + ex.getMessage());
        }
    }
   
    public void OracleBlob(Blob myBlob){
        try{
        PreparedStatement ps;
           
            String
        sqlNewRow = "INSERT INTO QA_ATTACHMENT1 (ATTACHMENT_ID,ATTACHMENT_FILE, ACTION_NUMBER) VALUES (?,EMPTY_BLOB(),?)",
        sqlLockRow = "SELECT ATTACHMENT_FILE FROM QA_ATTACHMENT1 WHERE ATTACHMENT_ID = ? FOR UPDATE",
        sqlSetBlob = "UPDATE QA_ATTACHMENT1 SET ATTACHMENT_FILE = ? WHERE ATTACHMENT_ID = ?";
       
           
        dbConnectARe();
       
        connActionRe.setAutoCommit(false);
      //make new row
      String stri = Integer.toString(i);
      String acno = "nasa_" + stri;
      ps = connActionRe.prepareStatement(sqlNewRow);
      ps.setString(1,stri);
      ps.setString(2,acno);
      ps.executeUpdate();
      //lock new row
      ps = connActionRe.prepareStatement(sqlLockRow);
      ps.setString(1,"1");
      ResultSet rs = ps.executeQuery();
      rs.next();
     
      oracle.sql.BLOB dbBlob = (oracle.sql.BLOB)myBlob;
     
       //update blob
      ps = connActionRe.prepareStatement(sqlSetBlob);
      ps.setString(2,stri);
      dbBlob.putBytes(1,myBlob.getBytes(1,(int) myBlob.length()));

      ps.setBlob(1,dbBlob);
      connActionRe.commit();
      System.out.print("Rows affected:");
      System.out.println(ps.executeUpdate());
      dbCloseARe();  
 
        }catch(Exception ex){
            System.out.println("Error : " + ex.getMessage());
        }
    }
   
   

   
    public void dbConnect() throws SQLException, IOException {
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        conn = DriverManager.getConnection("jdbc:oracle:thin:@onimqs2.onsemi.com:1521:dbonimqs","actionre","actionre");
    }
   
    public void dbConnectARe() throws SQLException, IOException {
        oracleConnString = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.242.65.222)(PORT=1521))(CONNECT_DATA=(SID=myondb1)))";
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        connActionRe = DriverManager.getConnection(oracleConnString ,"actionre", "actionre");
    }
   
    public void dbClose() throws SQLException, IOException {
        conn.close();
    }
   
     public void dbCloseARe() throws SQLException, IOException {
        connActionRe.close();
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws SQLException, IOException {
        blob myBlob = new blob();
    }
   
}



Please help.
Nasa.
0
 

Author Comment

by:nasarudin
ID: 9669693
Thanks everybody for helping me.
I found a code from metalink that solve my problems.
Below is the after some chages.

import java.util.*;
import java.io.*;
import java.lang.*;
import java.sql.*;
import oracle.sql.*;
import oracle.jdbc.*;

public class test {
    public static void main (String args[]) {
        Connection conn = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            System.out.println("Connecting to database");
            conn = DriverManager.getConnection(dburl,user,pass);
            //byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

            ResultSet rs;
            BLOB blob;
            Statement st;

            /*** Select blob record ***/
            st = conn.createStatement();
            ResultSet rs2 = st.executeQuery("select attachment_file from qa_attachment1 where attachment_id = 396");
            rs2.next();
            // Get as a BLOB
            Blob aBlob = rs2.getBlob(1);
            byte[] data = aBlob.getBytes(1, (int) aBlob.length());
            st.close();
            /*** End select blob ***/
           
            st = conn.createStatement();
            st.executeUpdate("insert into TT_DATA values ('1', empty_blob())");
            st.close();

            System.out.println("Setting autocommit to false");
            conn.setAutoCommit(false);
            st = conn.createStatement();
            System.out.println("Executing query");
            rs = st.executeQuery("SELECT * FROM TT_DATA WHERE ID='1' FOR UPDATE OF DATA");
            System.out.println("Setting autocommit to true");
            conn.setAutoCommit(true);
            rs.next();
            System.out.println("Getting BLOB");
            blob = ((OracleResultSet)rs).getBLOB(2);
            System.out.println("Getting Stream");
            OutputStream outstream = blob.getBinaryOutputStream();
            System.out.println("Writing to stream");
            outstream.write(data);
            System.out.println("Closing stream");
            outstream.close();
            System.out.println("Closing statement");
            st.close();


            System.out.println("Closing connection");
            conn.close();
            System.out.println("Done");
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}


I hope this could help others.
I'll accept tims answer because it helps me a lot.
Hope others don't mind.
;)
 nasa
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9669762
Cool!  You got it working!! :-)

Good luck with it!

Tim.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question