• September 28, 2014
  • JDBC
  • Comments Off on JDBC retrieve image example

JDBC retrieve image example

PreparedStatement provides the facility to store and retrieve the images in the database using JDBC.

PreparedStatement methods to retrieve image:

public Blob getBlob(int columnIndex) throws SQLException 

Example:

JDBCTest.java

import java.io.FileOutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.javawithease.util.JDBCUtil;
 
/**
 * This class is used to retrieve a image from DB.
 * @author javawithease
 */
public class JDBCTest {
	public static void main(String args[]){
		Connection conn = null;
		PreparedStatement preparedStatement = null;
 
		String query = "select * from IMAGESTORE " +
		"where IMAGE_ID = 1";
 
		try{			
			//get connection
			conn = JDBCUtil.getConnection();
 
			//create preparedStatement
			preparedStatement = 
				conn.prepareStatement(query);
 
			//execute query
			ResultSet resultSet = 
				preparedStatement.executeQuery();
			resultSet.next();  
 
			Blob clob = resultSet.getBlob(2);
			byte[] byteArr =  
				clob.getBytes(1,(int)clob.length());
 
			FileOutputStream fileOutputStream = 
			   new FileOutputStream("F:\\savedImage.jpg");
			fileOutputStream.write(byteArr);  
 
		     System.out.println("Image retrieved successfully.");
 
			//close connection
			fileOutputStream.close();
			preparedStatement.close();
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}	
}

JDBCUtil.java

import java.sql.Connection;
import java.sql.DriverManager;
 
/**
 * This is a utility class for JDBC connection.
 * @author jawithease
 */
public class JDBCUtil {
	//JDBC and database properties.
	private static final String DB_DRIVER = 
		           "oracle.jdbc.driver.OracleDriver";
	private static final String DB_URL = 
		        "jdbc:oracle:thin:@localhost:1521:XE";
	private static final String DB_USERNAME = "system";
	private static final String DB_PASSWORD = "oracle";
 
	public static Connection getConnection(){
		Connection conn = null;
		try{
			//Register the JDBC driver
			Class.forName(DB_DRIVER);
 
			//Open the connection
			conn = DriverManager.
			getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
 
			if(conn != null){
			   System.out.println("Successfully connected.");
			}else{
			   System.out.println("Failed to connect.");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}	
}

Output:

Successfully connected.
Image retrieved successfully.

Download this example.
 
Next Topic: JDBC ResultSetMetaData interface.
Previous Topic: JDBC store image example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 


Comments are closed.