Click to See Complete Forum and Search --> : Reading GIF file from the database


Surendra
October 12th, 1999, 06:36 AM
Hello All,

I want to create a new gif file by reading the data stored in the database that data is actually a gif file which is stored in a field which is of LONG RAW data type. How can i do this.
Thanks in advance.

Surendra

unicman
October 13th, 1999, 02:05 AM
Have u tried giving the data to ImageIcon. It has a constructor which takes byte array.

- UnicMan
http://members.tripod.com/unicman

Tonetheman
October 13th, 1999, 11:41 AM
If you accessing an Oracle database here some sample code that you can use. This code assumes you know how to get on an Oracle DB with JDBC already. Notice that this example uses the LONG RAW datatype. This is COMPLETELY different if you are using BLOB (Oracle 8+). If you ever intend to use Oracle replication the LONG RAW will not work ... oh well


public String getLogo(String p_lcode){
String l_name=null;
InputStream l_gifdata=null;
try{
//Query the logo of the selected airline
String l_query="select name,airline_logo from airline_long_raw_table where code="
+ "'" +p_lcode+ "'";
// Create a statement object for executing the query
Statement l_stmt = m_connection.createStatement();

// Obtain the result-set for the selected airline record
ResultSet l_result=l_stmt.executeQuery(l_query);

if(l_result.next()){

// Fetch column values
l_name=l_result.getString(1); // Obtain the airline code

// LONGRAW data can be accessed in two ways:
// 1) By retrieving all the data at once (using getBytes method)
// 2) By using streams. The LONGRAW data is made available to the program
// as a stream, and the data can be retrieved chunk by chunk, which is
// more eficient in terms of memory usage
// In this sample we illustrate retrieval using streams method.
l_gifdata=l_result.getBinaryStream(2);

// Write the byte array into a local file
FileOutputStream l_file=new FileOutputStream(l_name+".gif");

int l_chunk=0;
while((l_chunk = l_gifdata.read()) != -1)
l_file.write(l_chunk);
l_file.flush();
l_file.close();
l_result.close();//closing the resultset
l_stmt.close(); //closing the statement object
m_GUI.putStatus("Image obtained from database");
}
}catch(Exception ex){ //Trap IO Errors
m_GUI.putStatus(ex.toString());
}
return l_name;
}