The Artima Developer Community
Sponsored Link

Java Answers Forum
Retrieving Images from Database

1 reply on 1 page. Most recent reply: Sep 10, 2002 7:20 AM by Don Hill

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
siddhartha

Posts: 15
Nickname: saddysan
Registered: Jul, 2002

Retrieving Images from Database Posted: Sep 9, 2002 5:57 AM
Reply to this message Reply
Advertisement
Hello Friend
I am trying to retrieve image from a Database(MS Access here), but i am not getting desired results. Many more unwanted bytes are coming along with the data.

Sql query of the table is
create table ImageData ( MyImage Memo , Name varchar(50))

The program is
import java.io.*;
import java.sql.*;

class MultiMediaDatabase
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:saddy","","");
PreparedStatement stmt = con.prepareStatement("select * from ImageData");
ResultSet rs= stmt.executeQuery();
while (rs.next())
{
InputStream in =rs.getBinaryStream("MyImage");
String fName = rs.getString("Name");
System.out.println(fName);
OutputStream out = new FileOutputStream(fName);
int i;
while ((i=in.read()) != -1)
{
out.write(i);
}
out.close();
} // end of while
} // end of main
}



Please help me as i am not able to find out where i am wrong

Saddy


Don Hill

Posts: 70
Nickname: ssswdon
Registered: Jul, 2002

Re: Retrieving Images from Database Posted: Sep 10, 2002 7:20 AM
Reply to this message Reply
Since what you really are looking for is the bytes why don't you just do a

byte[] bytes = rs.getBytes(n);

FileOutputStream out = new FileOutputStream( fName );
out.write( bytes);
out.flush();
out.close()

HTH



> Hello Friend
> I am trying to retrieve image from a Database(MS
> Access here), but i am not getting desired results.
> Many more unwanted bytes are coming along with the
> data.
>
> Sql query of the table is
> create table ImageData ( MyImage Memo , Name
> varchar(50))
>
> The program is
> import java.io.*;
> import java.sql.*;
>
> class MultiMediaDatabase
> {
> public static void main(String args[]) throws
> ws Exception
> {
>
>
> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
> Connection con =
> n con =
> DriverManager.getConnection("jdbc:odbc:saddy","","");
> PreparedStatement stmt =
> con.prepareStatement("select * from ImageData");
> ResultSet rs= stmt.executeQuery();
> while (rs.next())
> {
> InputStream in =rs.getBinaryStream("MyImage");
> String fName = rs.getString("Name");
> System.out.println(fName);
> OutputStream out = new
> new FileOutputStream(fName);
> int i;
> while ((i=in.read()) != -1)
> {
> out.write(i);
> }
> out.close();
> } // end of while
> } // end of main
> }
>
>
>
> Please help me as i am not able to find out where i
> am wrong
>
> Saddy

Flat View: This topic has 1 reply on 1 page
Topic: Change 3D look of a window border? Previous Topic   Next Topic Topic: error says missing braces, I think otherwise??

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use