• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

how to store an image into the DataBase using oracle database  RSS feed

 
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i store image into the database
 
Ranch Hand
Posts: 448
1
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can use a BLOB column.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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();




 
Sheriff
Posts: 21461
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gursewak Singh
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir i am using similar method but it shows exception
ArrayIndexOutOfBound


 
Sheriff
Posts: 23714
50
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Sir, you are Right.it was my mistake.
Now code is running
Thank you>...................................
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi also tell me how to set the image path in my program for example "d:/image.jpg" is this correct way???, thank you
 
author & internet detective
Sheriff
Posts: 38571
660
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i m gettng an error whle insering image in database. error says"expected no. got blob".
I used above given code only..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!