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

JDBC store image example

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

PreparedStatement methods to store image:

1. public void setBinaryStream(int paramIndex,InputStream stream)  throws SQLException 

2. public void setBinaryStream(int paramIndex,InputStream stream,long length) 
throws SQLException 

Example:

JDBCTest.java

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import com.javawithease.util.JDBCUtil;
 
/**
 * This class is used to store a image in DB.
 * @author javawithease
 */
public class JDBCTest {
	public static void main(String args[]){
		Connection conn = null;
		PreparedStatement preparedStatement = null;
 
		String createTableQuery = "create table IMAGESTORE("
			+ "IMAGE_ID NUMBER(5) NOT NULL, "
			+ "NAME BLOB NOT NULL, "
			+ "PRIMARY KEY (IMAGE_ID) )";
 
                String storeImageQuery ="insert into IMAGESTORE "
				                + "values (?,?)";
 
		try{			
			//get connection
			conn = JDBCUtil.getConnection();
 
			//create preparedStatement
			preparedStatement = 
				conn.prepareStatement(createTableQuery);
 
			//execute query for create table
			preparedStatement.execute();
			System.out.println("Table created successfully.");
 
			preparedStatement = 
				conn.prepareStatement(storeImageQuery);
 
			//Read source image
			FileInputStream fileInputStream = 
				new FileInputStream("F:\\test.jpg");
 
                        preparedStatement.setInt(1,1);  
			preparedStatement.setBinaryStream(2,
			  fileInputStream,fileInputStream.available());
 
			//execute update
			preparedStatement.executeUpdate();
		     System.out.println("Image stored successfully.");
 
			//close connection
			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.
Table created successfully.
Image stored successfully.

Download this example.
 
Next Topic: JDBC retrieve image example.
Previous Topic: JDBC retrieve file 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.