We help IT Professionals succeed at work.

Image database

shaikchanbasha
on
Hai java guys,

I need help from you for storing the images into a

database.Can anyone provide code for insert and

retrieve the image from sql-server as well as

oracle.For insertion i implement a class as

follows by using sql-server as backend.In the

imagetable image column is image type.After

execute this it is storing the image into image

column as some sort of string.is it right if so

how to retrive that image and save it to another

directory.

import java.io.*;
import java.sql.*;

public class ImageBLOB
{
   public static void main(String[] args )
   {
      byte[] buffer1 = new byte[1024];
       try
       {
           File file1 = new File(args[0]);
            BufferedInputStream iStream1 =

new BufferedInputStream(new

FileInputStream(file1));
            int eof1 =

iStream1.read(buffer1);
       }
       catch(Exception e)
       { System.out.println(e);}

      try
       {
           

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection

con=DriverManager.getConnection("jdbc:odbc:basha",

"sa","");
            Statement st =

con.createStatement();
            st.executeUpdate("insert into

imagetable(image) values('"+buffer1+"')");
       }
       catch(Exception e)
       { System.out.println(e);}
   }
}

Let me know how to insert into oracle also.it is

very urgent..so plz help me.thanks in advance.

Basha
Comment
Watch Question

Commented:
9 questions asked, but only grade 1.

Author

Commented:
Then what i am supposed to do Mr.kotan
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
ummmm...grade some questions maybe?
Commented:

Use JDBC ODBC driver to establish the connection
and convert the file in binary byte[]
data.
simply use insert query to insert the file

/******** to conert into binary file ******/

FileInputStream fis = new FileInputStream("c:\a.gif");
int iuploadfileSize = fis.available();
byte[] byteFilesize = new byte[iuploadfileSize];

/******** end of converting into binary file ******/


take this binary data "byteFilesize" in insert query.

preparedstatement= insert into file_data values(file_id="1",file_data=?);

Author

Commented:
Dera naidubs,

Thank you for your reply,but i asked how to retrieve that image after stored into database.Thank you once again...

Basha

Commented:
hi basha,
Same as any other select statement.

rs=con.execute(select * from file_data where file_name="soANDso.gif");
Byte[] b=rs.getByte("file_data");
for(i=0;i<=b.lengh();i++){
//write the code to write into a file with extension .gif
}


Once U get the resultset use getByte() to the image data and write into a file with ur favourite name with the proper extension ( same as the uploaded document)
All the best
Naidu

Author

Commented:
thank you naidubs.it's working fine.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.