Esta es una manera de recuperar una imagen de la base de datos.
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class GeneraImagenDB2 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String driver = "COM.ibm.db2.jdbc.app.DB2Driver";
//String driver = "COM.ibm.db2.jdbc.net.DB2Driver";
//String driver = "com.ibm.db2.jcc.DB2Driver";
String url = "jdbc:db2:sample";
//url = "jdbc:db2://9.18.60.129:50000/sample";
String username = "Administrator";
String password = "informix";
try {
Class.forName(driver);
//Class.forName(driver).newInstance();
} catch (Exception exp) {
System.out.println("Failed to load JDBC driver.");
exp.printStackTrace();
}
try {
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
String query = "select noemp, mapas from mapas where noemp = 1";
System.out.println(query);
rs = stmt.executeQuery(query);
byte[] imagen = null;
if(rs.next())
{
System.out.println("NoEmp --> " + rs.getString("noemp"));
imagen = rs.getBytes("mapas");
try
{
File file = new File("img.jpg");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(imagen);
bos.flush();
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SQLException exp) {
exp.printStackTrace();
}
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException exp) {
System.out.println("Error al cerrar conexion a B.D!");
exp.printStackTrace();
}
}
}
Saludos comunidad open source