Skip to Main Content
  • Questions
  • How to insert a string from java program to a table containing blob

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Sudip.

Asked: November 14, 2000 - 4:31 am UTC

Last updated: February 16, 2005 - 8:10 am UTC

Version: 8.1.5

Viewed 1000+ times

You Asked

hi Tom,


how r u?

I'm staing you a problem regarding storing a string content from a java program by calling procedure to a database table containg blob type .

A more detail is :

table was created as :

create table files_org (id number,content blob);
/

a procedure has been created as:

create or replace procedure ins_blob(id in number,content blob)
as
begin
insert into files_org values(id,content);
end;
/


this procedure was called from a java segment is given below....


String s = "Supal";

oracle.sql.BLOB BL = new oracle.sql.BLOB((OracleConnection)con,s.getBytes());
String sProcName = "ins_blob";
Vector vInParams = new Vector();
vInParams.addElement(new Integer(123));
vInParams.addElement(BL);
com.macmet.util.db.DatabaseUtility.executeStoredProcedure
(con, sProcName,vInParams,null);

in executeStoredProcedure()
method((OracleCallableStatement)rcProc).setBLOB(index,
(oracle.sql.BLOB)obj);




====

the class executeStoredProcedure exists...

at the time of execution of this clas the following error occurs:



Exception in thread "main" java.sql.SQLException: ORA-24812: character set conve
rsion to or from UCS2 failed
ORA-06512: at "SCOTT.INS_BLOB", line 3
ORA-06512: at line 1

at oracle.jdbc.dbaccess.DBError.throwSqlException(Compiled Code)
at oracle.jdbc.ttc7.TTIoer.processError(Compiled Code)
at oracle.jdbc.ttc7.Oall7.receive(Compiled Code)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(Compiled Code)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFetch(Compiled Code)
at oracle.jdbc.driver.OracleStatement.executeNonQuery(Compiled Code)
at oracle.jdbc.driver.OracleStatement.doExecuteOther(Compiled Code)
at oracle.jdbc.driver.OracleStatement.doExecuteWithBatch(Compiled Code)
at oracle.jdbc.driver.OracleStatement.doExecute(Compiled Code)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(Compiled Code
)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(Compiled Cod
e)
at oracle.jdbc.driver.OraclePreparedStatement.execute(Compiled Code)
at com.macmet.util.db.DatabaseUtility.executeStoredProcedure(Compiled Co
de)
at tst.main(Compiled Code)





please observe it and try to find a solution...

a typical error ORA-24812 occured .. you can notice while observing it ...

however

rest is okay,,

waiting for your reply,,

good luck

Sudip




and Tom said...

Well, if you do it in straight JDBC -- I'd be glad to look at it. I do not know exactly what com.macmet.util.db.DatabaseUtility might be doing under the covers.

I can say that if I wanted to insert a string into a blob and the string was reasonably sized (32k or less), I would use the following logic instead:


create table files_org (id number,content blob);

create or replace
procedure ins_blob(id in number,content varchar2)
as
l_blob blob;
begin
insert into files_org values( id, empty_blob() )
returning content into l_blob;

dbms_lob.writeappend
( l_blob, length(content), utl_raw.cast_to_raw(content) );
end;
/

That lets me send a string to the database and then convert it (cast it) into a RAW. then my java can look like:

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

class test
{

public static void main(String args[])
{
try
{
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());

Connection connection =
DriverManager.getConnection ("jdbc:oracle:thin:@aria-dev:1521:ora816dev",
"scott", "tiger");

PreparedStatement ps = connection.prepareStatement
( "begin ins_blob( ?, ? ); end;" );

ps.setInt( 1, 1 );
ps.setString( 2, "Test String" );

ps.executeQuery();

ps.close();


}
catch( Exception e )
{
e.printStackTrace();
}

}

}


It just deals with strings and lets the stored procedure worry about any conversions.



If you want -- you can post an example that shows your issue using the jdbc stuff straight out -- I can help figure out why you are getting what you are that way...

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

An image file

Ik, February 28, 2003 - 9:38 am UTC

Tom,

How do i insert an Image file into a BLOB column?

regards

Tom Kyte
February 28, 2003 - 10:16 am UTC

search for


load lob


on this site for many many articles and pointers on doing this

Inserting string to blob content

Sujatha, February 16, 2005 - 4:49 am UTC

Hi Tom,
I found the solution u gave rather straight forward and interesting.

Thanks for ur interest to help others...

Keep up ur good work!

Sujatha

Tom Kyte
February 16, 2005 - 8:10 am UTC

WHO is this "u" person, quite frustrating that they are constantly referred to. I want to meet this person some day.

But "u" didn't give you anything here, so I am very confused

More to Explore

CLOB/BLOB/etc

Complete documentation on Securefiles and Large Objects here