Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to store an image into the DataBase using oracle database

 
Gursewak Singh
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i store image into the database
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can use a BLOB column.
 
Arun Chidam
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();




 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
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


 
Paul Clapham
Sheriff
Posts: 21107
32
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>...................................
 
Sidhu roy
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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34372
345
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
 
priya mathpal
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic