• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to store an image into the DataBase using oracle database

 
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can i store image into the database
 
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can use a BLOB column.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir i am using similar method but it shows exception
ArrayIndexOutOfBound


 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic