Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Queries and Discussion > Programming > Java

Reply  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More
 
Bookmarks Thread Tools Search this Thread Display Modes
Old 06-05-2006, 11:14 AM   #1
Contributor
 
Join Date: Oct 2005
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 5
prashantSum is on a distinguished road
Question

Inserting BLOB data through JDBC


Hi all,
I am trying to insert data into oracle BLOB through JDBC. I am able to insert the data using oracle.sql.BLOB which have methods like putBytes() and setBytes().

I have two concerns:
First one: I am inserting the data using the following code

ps = conn.prepareStatement(sql);
oracle.sql.BLOB newBlob = oracle.sql.BLOB.createTemporary(conn,false, oracle.sql.BLOB.DURATION_SESSION);

newBlob.putBytes(1,docout.toString().getBytes());
ps.setBlob(1,newBlob);
ps.execute();

sql statement contains one ? (i.e. bind variable), and docout is a stringBuffer which contains desired length of user (ex: 40,000).

Question: Is this the right way to insert blob data through JDBC or is there is any other better way? actually I want to insert Word documents into BLOB.
Second One: I have experimented this code with Oracle 9i JDBC and Oracle 10g JDBC.
Oracle 9i: In Oracle 9i jdbc driver putBytes() method is working, setBytes() is not working (error: unsupported feature).
Oracle 10g: In Oracle 10g jdbc driver putBytes() is working but it says putBytes is a deprecated method, setBytes() is working properly.

We still did not tested with Oracle 8i.

My concern is I have to write a program which can be used with any version of Oracle JDBC like 8i, 9i or 10g where I can use a single method
prashantSum is offline   Reply With Quote
Old 06-05-2006, 12:41 PM   #2
Contributor
 
Amit Ray's Avatar
 
Join Date: Jul 2004
Posts: 75
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 7
Amit Ray is on a distinguished road
Send a message via Yahoo to Amit Ray
Thumbs up

Re: Inserting BLOB data through JDBC


Another way to would be:

Inserting a BLOB

// CREATE TABLE t1 (c1 INT PRIMARY KEY NOT NULL, c2 BLOB(5M));

PreparedStatement pstmt = conn.prepareStatement ("INSERT INTO t1 VALUES (?,?)");
pstmt.setInt (1, 100);
File fBlob = new File ( "image1.gif" );
FileInputStream is = new FileInputStream ( fBlob );
pstmt.setBinaryStream (2, is, (int) fBlob.length() );
pstmt.execute ();
...


Retrieving a BLOB

// CREATE TABLE t1 (c1 INT PRIMARY KEY NOT NULL, c2 BLOB(5M));

Statement stmt = conn.createStatement ();
ResultSet rs= stmt.executeQuery("SELECT * FROM t1");
while(rs.next()) {
int val1 = rs.getInt(1);
InputStream val2 = rs.getBinaryStream(2);
...
} rs.close();
...


The good thing about this approach is - The function InputStreams encapsulates the logic to chunk the data in pieces.

Also check out the MSDN Link.

All I can tell you is, there are a lot of ways.. and the one you are using is certainly not the best.

For the protability part, are you using the JDBC Implementation (classes12.jar for example) corresponding to the Oracle version you are using ? Java just provides the interface in JDBC .. it is completely upto Oracle how they have implemented that in the different versions. But generally speaking, if a method is not depricated, it should work for all versions.

Cheers,
Amit Ray.
Amit Ray is offline   Reply With Quote
Old 06-06-2006, 11:16 AM   #3
Contributor
 
Join Date: Oct 2005
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 5
prashantSum is on a distinguished road
Question

Re: Inserting BLOB data through JDBC


hi amit,
thanks for the reply and good guidance.

Ya, I was using JDBC Implementation (classes12.jar) of different versions of Oracle, but this is solved by implementing your suggestion.

But I have some more doubts.

Actually our issue is, we are unable to insert records where column width is more than 4000 chars when using Statement, i.e. no bind variables (?) in the statement, we are directly taking a string and concatinting it to sql query.

Is this really a restriction with Oracle or is it a type 4 driver problem, can we try inserting more than 4000 chars in Statement using type 2 driver
prashantSum is offline   Reply With Quote
Old 06-07-2006, 10:15 AM   #4
Contributor
 
Join Date: Oct 2005
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 5
prashantSum is on a distinguished road

Re: Inserting BLOB data through JDBC


If any one knows the answer for the above question please reply me early, I am waiting for it.
prashantSum is offline   Reply With Quote
Old 06-07-2006, 12:34 PM   #5
Contributor
 
Amit Ray's Avatar
 
Join Date: Jul 2004
Posts: 75
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 7
Amit Ray is on a distinguished road
Send a message via Yahoo to Amit Ray
Thumbs up

Re: Inserting BLOB data through JDBC


Nothing to do with the driver. Oracle has a 4000 character limitation on VARCHAR2 fields.

The Oracle SQL parser can only handle string literals up to 4000 characters in length. SQL*Plus is even more restricted. It can handle strings up to 2500 characters in length. From most Oracle clients, there will in fact be no way to insert data longer than 4000 characters long into a table. A statement/system that works perfectly with a 4000-character string will fail completely with a 4001-character string. You have to completely redesign the way you're doing things to work with strings that might be long.

The Oracle 10g JDBC driver claims to have a work around. Check out this link.

Also you can take a look at the following links:

Loading Large XMLs.

Limits in Oracle.

Hope that helps.

Amit Ray.
Amit Ray is offline   Reply With Quote
Old 06-07-2006, 05:26 PM   #6
Contributor
 
Join Date: Oct 2005
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 5
prashantSum is on a distinguished road

Re: Inserting BLOB data through JDBC


Thanks for reply Amit.
prashantSum is offline   Reply With Quote
Reply  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
Create RTF File with Template and XML mikunos Web Development 7 07-15-2009 04:41 PM
XML, Platform-Independent and Well-Supported Technology pradeep Web Development 3 02-16-2009 05:45 PM
SSL in brief pradeep Engineering concepts 0 05-05-2005 11:50 PM
What's the buzz on GSM? pradeep Engineering concepts 0 05-05-2005 11:45 PM

 

All times are GMT +5.5. The time now is 04:07 AM.