wood burning stoves 2.0*
The moose likes JDBC and the fly likes Not able to insert image more than 4K in Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Not able to insert image more than 4K in Database" Watch "Not able to insert image more than 4K in Database" New topic
Author

Not able to insert image more than 4K in Database

sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
Dear All,
I am using a Servlet to insert an image into oracle database, its working if the image is less then 4KB.

File file =new File(file1);
InputStream is = new FileInputStream(file);
ps =con.prepareStatement("insert into emp_photo VALUES(?,?,?)");
ps.setString(1,name);
ps.setString(2,type);
ps.setBinaryStream(3,is,Integer.MAX_VALUE);

Please Suggest.
Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

Please take the time to choose the correct forum for your posts. This forum is for questions on Servlets.

For more information, please read this.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Check the setup of the database table. See what the maximum number of bytes allowed in that column is.
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
Thanks.

I am using Blob as DataType. Able to insert the large images through PL/SQL, but when trying through servlet, its not. Can you suggest me where i am going wrong.

Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



Which Oracle version is it ? if PL/SQL works , you can use CallableStatement to call stored procedure and see if that works.
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
I am using Oracle 9i., DataType is Blob, Problem is that, not able to insert the images having size more then 4KB.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Okay. And does Oracle allow you to specify the maximum number of bytes for a Blob column? If so, what is the default value for that maximum number?
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
No, it dont allow us to set the size of the blob.
I tried using this but the result is same :

File f = new File(pathoffile);
byte[] bytes = new byte[(int)f.length()];
FileInputStream fs = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fs);
bis.read();

sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
Can you give me any idea how to use
BLOB.createTemporary(con, true, BLOB.DURATION_SESSION) to insert an image into database using Java/Servlet.
Thanks.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874




You are inserting data without specifying column name. I guess the data is inserted to a wrong column (which has 4K limit). add column names to your insert statement to avoid confusion. this is just a thought..
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
Thanks, but when I am inserting a file less then 4k, then its inserting into the blob column and when the file is more then 4k the blob column is empty.

Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


how do you check if its empty ? Use SQL Developer to view the BLOB.
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
If the bolb is empty u'll find the (OraBlob) else it will be in upper. i am using Toad to view the rows.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


I m not sure TOAD is right tool to view BLOB type. I couldnt view using TOAD , but could view BLOB data in SQLDeveloper.

if there are no errors during JDBC opertation , may be your data is stored , but you are not able to view it ?
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
Yeah Toad is not an appropriate tool to view Blob, but not able to view the Blob throgh SqlDeveloper too. Its just an empty column. Please provide the code you are using to insert the image to database.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



Your code seems fine to insert BLOB type.
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
Please Suggest me, what should i do?
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Cant guess why is it not working for data size above 4 KB. are you able to view if image is less than 4KB data alteast ?
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
Yes, it can be viewed.
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
Is there any alternative to do this?
Please suggest.
Thanks
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Like said earlier , if PL/SQL works, call the stored procedure using CallableStatement and store the BLOB data.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Which driver are you using? The drivers that came with Oracle 9i didn't do so well with LOBs.

Also, are you aware that a LOB will only be stored inline up to 4k? After that it will be stored outside the row. Have a read of the Oracle docs on LOB data types for both issues.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2495
    
    8

Paul Sturrock wrote:...Also, are you aware that a LOB will only be stored inline up to 4k? After that it will be stored outside the row. Have a read of the Oracle docs on LOB data types for both issues.
Maybe the data is stored (Balu Sadhasivam suggested that too), but your viewers can't show it, because, as Paul says, it is stored differently for +4K data.
Write a java application to fetch the data from the database, and validate if what you get is what you uploaded.

OCUP UML fundamental and ITIL foundation
youtube channel
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
Using classes12.jar and ojdbc14.jar.
Using thin to connect through the database. Is there any possibility that using thin can create this problem. But when i tried to use Oci it gave the following Error : java.sql.SQLException: Invalid Oracle URL specified.

Secondly I have created a JSp page to read the Blob, but its displaying the images (all images less then 4K).
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Using classes12.jar and ojdbc14.jar.

No, you are using one or the other not both. I'd recommend using the latest driver you can get rather than relying on the de-supported Oracle 9i drivers.


Secondly I have created a JSp page to read the Blob, but its displaying the images (all images less then 4K).

Not sure what you mean by this, are you saying you can successfully view images over 4K or that you tested it with images under 4K and they are OK?
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
I am using one at a time.
while Using OCI driver i am getting the error : java.sql.SQLException: Invalid Oracle URL specified.

Secondly The JSp page is able to show all images that are in the Database and all are less then 4K. There is no Images being saved i.e. greater then 4K.
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
One more thing , when i am using the same code to insert the image through my Local system (Java Class), I am able to insert images more then 4K, but when i am using the same through a JSP page and calling a servlet, its not inserting the images more then 4K.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Is there any error thrown? Are you catching up the errors ? What is the difference in logs (SOP , logs) when using 4k and >4K
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2495
    
    8

sk mehrotra wrote:One more thing , when i am using the same code to insert the image through my Local system (Java Class), I am able to insert images more then 4K, but when i am using the same through a JSP page and calling a servlet, its not inserting the images more then 4K.
Can you post the relevant code part of both your Java ans JSP/servlet attempts?
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
Java and servlet hav the same code that was posted earlier. Just a JSP page that allows to browse a file.
Java Code :


=============================================================================
Servlet:


===============================================================================

JSP:
Jhakda Velu
Ranch Hand

Joined: Feb 26, 2008
Posts: 166
Hi
It is surprising that the standalone code is able to insert but the servlet code isn't. I suggest you first insert the mandatory fields(non blob), then get the blob by querying the record you entered and update it. It *may* work. I did that for a clob.



Jhakda


If I become filthy rich, I'll sponsor research for painless dental treatment at Harvard Medical School. Thats why,I'm learning Java.I have 32 teeth, 22 are man made.
Geneson Subramaniam
Greenhorn

Joined: May 01, 2009
Posts: 3
Hi All,

I'm trying to find a GUI tool that connects to Derby in embedded mode. Can Eclipse is capable of this? If so,
where can I start??

TQ
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Jhakda Velu wrote:Hi
It is surprising that the standalone code is able to insert but the servlet code isn't. I suggest you first insert the mandatory fields(non blob), then get the blob by querying the record you entered and update it. It *may* work. I did that for a clob.



Jhakda



This shouldn't be needed. My guess as to why the behaviour is different would be that the JSP/Servelt application is using a differernt version of the JDBC driver. There is no difference between using JDBC code from a stand alone applciation and from a web application other than the configuation of the JDBC driver and data source.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Geneson Subramaniam wrote:Hi All,

I'm trying to find a GUI tool that connects to Derby in embedded mode. Can Eclipse is capable of this? If so,
where can I start??

TQ


Please don't use someone elses topic to ask you question. Please start your own thread for this discussion.
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
Have compiled the java code in the same environment used for Servlets.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

sk mehrotra wrote:Have compiled the java code in the same environment used for Servlets.


OK. The driver jar files are not important at compile time, only runtime. What driver files are on your classpaths?
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2495
    
    8

sk mehrotra wrote:Have compiled the java code in the same environment used for Servlets.
But have you used the same jdbc jar at runtime?
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2495
    
    8

You type 10 seconds faster.
sk mehrotra
Ranch Hand

Joined: Oct 27, 2005
Posts: 34
Using the same JDBC jar at the runtime.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

sk mehrotra wrote:Using the same JDBC jar at the runtime.


This seems unlikely. The same code running in a web application or as a stand alone application with the same JDBC driver will behave the same. If it isn't then its not the JDBC code that is the problem. What size are the files you upload?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to insert image more than 4K in Database