aspose file tools*
The moose likes JDBC and the fly likes how to store an image into the DataBase using oracle  database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to store an image into the DataBase using oracle  database" Watch "how to store an image into the DataBase using oracle  database" New topic
Author

how to store an image into the DataBase using oracle database

Gursewak Singh
Ranch Hand

Joined: Jan 09, 2011
Posts: 79

How can i store image into the database
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

I think you can use a BLOB column.
Arun Chidam
Ranch Hand

Joined: May 08, 2006
Posts: 79
Gursewak Singh wrote:How can i store image into the database


Declare a column in your table as Image eg. CREATE TABLE [dbo].[REPORTIMAGES]([XID] [int] NOT NULL,[xIMAGE] [image] NULL)

In Java use the below code to retrieve and store images

#########Store#############
File file = new File(img);
FileInputStream fis = new FileInputStream(file);
len = (int)file.length();
query = ("insert into REPORTIMAGESVALUES(?,?)");
pstmt = conn.prepareStatement(query);
pstmt.setInt(1, len);
// Method used to insert a stream of bytes
pstmt.setBinaryStream(2, fis, len);
pstmt.executeUpdate();

#########Retrieve#########

FileOutputStream fos = new FileOuputStream("C:/image.jpg");
Resultset rs = conn.executeQuery(Select XImage from ReportImages where xId = ?);
fos.write(rs.readBytes(1));
fos.close();





SCJP 1.4, SCWCD 1.4
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19791
    
  20

I have two comments on that:
1) setBinaryStream has been overloaded in Java 6 / JDBC 4 to also accept a long for the length, or even to read all. The driver must of course support this.

2) You definitely shouldn't use the file length as the image ID. Instead use an auto-increment field for the ID, and store the file length in a third column. But yeah, using PreparedStatement and its setBinaryStream, setBytes or setBlob methods for storing and ResultSet and its getBinaryStream, getBytes or getBlob methods for retrieving is the way to go.

I'll move this thread to our JDBC forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gursewak Singh
Ranch Hand

Joined: Jan 09, 2011
Posts: 79

Sir i am using similar method but it shows exception
ArrayIndexOutOfBound


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

And that happens at this line of code, right?

That would be because your SQL only contains one question mark, and therefore only one field which you can set that way.
Gursewak Singh
Ranch Hand

Joined: Jan 09, 2011
Posts: 79

Yes Sir, you are Right.it was my mistake.
Now code is running
Thank you>...................................
Sidhu roy
Greenhorn

Joined: Aug 11, 2011
Posts: 15
import java.io.*;
import java.sql.*;
import java.util.*;
class Pic3
{
public static void main(String args[]) throws Exception
{
Statement s;
Connection c;
FileInputStream fis;
PreparedStatement ps;
File file;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c=DriverManager.getConnection("Jdbc:Odbc:sidhu","system","system");
s=c.createStatement();
s.execute("Create table Img1(Image_No number(5),Photo blob)");
}
catch(Exception e1)
{
e1.printStackTrace();
}
try
{
file=new File("f:/image.jpeg");
fis=new FileInputStream(file);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c=DriverManager.getConnection("Jdbc:Odbc:sidhu","system","system");
String str="insert into Img1 values(?,?)";
ps=c.prepareStatement(str);
ps.setInt(1,(int)file.length());
ps.setBinaryStream(2,fis,(int)file.length());
//System.out.println("success");
ps.execute();
ps.close();
c.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}

hi i am using this code for storing an image into oracle database but am getting a runtime error i.e., "unimplemented or unreasonable conversion requested"
at the line ps.execute(); plzzzzz help me
Sidhu roy
Greenhorn

Joined: Aug 11, 2011
Posts: 15
hi also tell me how to set the image path in my program for example "d:/image.jpg" is this correct way???, thank you
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

sidhug sid,
Your post was moved to a new topic.
this sounds like a new question. let's create a new thread so you get more attention


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
priya mathpal
Greenhorn

Joined: Apr 03, 2013
Posts: 1
i m gettng an error whle insering image in database. error says"expected no. got blob".
I used above given code only..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to store an image into the DataBase using oracle database