Tn this tutorial, we are going to see how to read Blob images stored in a db2 database table using java code. Let us assume that large objects like images or audio file (eg. Employee photos or Mp3 file) are stored in a db2 database table using BLOB fields. For storing images in db2 table, you can visit Store Images in BLOB. The images or audio files are stored as bytes in DB2. Db2 table stores only descriptor (locator or pointer) which points the external location where the actual bytes are stored.
JDBC 2.0 provides methods like getBytes , getBlob to read Binary Data or BLOB data from the table. To get the actual images (jpeg , jpg, gif,..) or audio files (mp3,mp4, ..) , we have to read the bytes from the database and write into a external file. Now let us see how the following code works?.
In our example Employee table has the fields empid, empname, empphoto,filepath where as empphoto is the blob field which is used to store employee photos. filepath contains the external file path including file name. The bytes read from the blob field is written into the external file in the location using filepath. To read the bytes from the blob field, we can either use
byte[] image = rs.getBytes("empphoto")
OR
Blob blob_temp =rs.getBlob("empphoto"),
byte[]image=blob_temp.getBytes(1,(int)blob_temp.length()).
Static method writeToFile (byte[] image, String fileName) accepts image bytes and fileName. Bytes are written to file using the write method of FileOutputStream. Now the employee photo is ready in the location given in the filePath field.
package com.javaonline;
import java.sql.*;
import java.io.*;
public class Db2BlobRead {
public static void main(String[] args) throws SQLException
{
try {
//Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
Class.forName("com.ibm.db2.jcc.DB2Driver");
} catch (Exception e) {
System.out.println(e);
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:db2:test");
Statement stmt=null;
ResultSet rs = null;
try {
stmt = con.createStatement();
rs = stmt.executeQuery("select empid , empname, empphoto, filepath from Employee");
while (rs.next())
{
if ((rs != null) ) {
String empcode =rs.getString("empid");
String empname =rs.getString("empname");
// Blob blob_temp =rs.getBlob("empphoto");
// byte[] image=blob_temp.getBytes(1,(int)blob_temp.length());
byte[] image = rs.getBytes("empphoto");
String fileName = rs.getString("filepath");
writeToFile(image, fileName);
System.out.println("Employee Code => " + empcode);
System.out.println("Employee Name => " + empname);
System.out.println("Employee Photo read and stored at : " + fileName);
System.out.println();
}
}
} catch (SQLException e)
{
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
// to write bytes into physical file.
private static void writeToFile(byte[] image, String fileName) {
FileOutputStream file = null;
try {
file = new FileOutputStream(fileName);
file.write(image);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (file != null) file.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
Output of above program :JDBC 2.0 provides methods like getBytes , getBlob to read Binary Data or BLOB data from the table. To get the actual images (jpeg , jpg, gif,..) or audio files (mp3,mp4, ..) , we have to read the bytes from the database and write into a external file. Now let us see how the following code works?.
In our example Employee table has the fields empid, empname, empphoto,filepath where as empphoto is the blob field which is used to store employee photos. filepath contains the external file path including file name. The bytes read from the blob field is written into the external file in the location using filepath. To read the bytes from the blob field, we can either use
byte[] image = rs.getBytes("empphoto")
OR
Blob blob_temp =rs.getBlob("empphoto"),
byte[]image=blob_temp.getBytes(1,(int)blob_temp.length()).
Static method writeToFile (byte[] image, String fileName) accepts image bytes and fileName. Bytes are written to file using the write method of FileOutputStream. Now the employee photo is ready in the location given in the filePath field.
package com.javaonline;
import java.sql.*;
import java.io.*;
public class Db2BlobRead {
public static void main(String[] args) throws SQLException
{
try {
//Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
Class.forName("com.ibm.db2.jcc.DB2Driver");
} catch (Exception e) {
System.out.println(e);
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:db2:test");
Statement stmt=null;
ResultSet rs = null;
try {
stmt = con.createStatement();
rs = stmt.executeQuery("select empid , empname, empphoto, filepath from Employee");
while (rs.next())
{
if ((rs != null) ) {
String empcode =rs.getString("empid");
String empname =rs.getString("empname");
// Blob blob_temp =rs.getBlob("empphoto");
// byte[] image=blob_temp.getBytes(1,(int)blob_temp.length());
byte[] image = rs.getBytes("empphoto");
String fileName = rs.getString("filepath");
writeToFile(image, fileName);
System.out.println("Employee Code => " + empcode);
System.out.println("Employee Name => " + empname);
System.out.println("Employee Photo read and stored at : " + fileName);
System.out.println();
}
}
} catch (SQLException e)
{
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
// to write bytes into physical file.
private static void writeToFile(byte[] image, String fileName) {
FileOutputStream file = null;
try {
file = new FileOutputStream(fileName);
file.write(image);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (file != null) file.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
0 comments:
Post a Comment