Store image in database in java

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.w3schools.util.JDBCUtil;
 
/**
 * This class is used to store a image in DB.
 * @author w3schools
 */
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 w3schools
 */
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.