CodesJava

Easy learning with example program codes

Store file in database using JDBC

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

PreparedStatement methods to store file:

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.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import com.codesjava.util.JDBCUtil;
 
/**
 * This class is used to store a file in DB.
 * @author codesjava
 */
public class JDBCTest {
	public static void main(String args[]){
		Connection conn = null;
		PreparedStatement preparedStatement = null;
 
		String createTableQuery = "create table FILESTORE("
			+ "FILE_ID NUMBER(5) NOT NULL, "
			+ "NAME CLOB NOT NULL, "
			+ "PRIMARY KEY (FILE_ID) )";
 
		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.");
 
			String storeFileQuery = "insert into FILESTORE "
				+ "values (?,?)";
			preparedStatement = 
				conn.prepareStatement(storeFileQuery);
 
			//Read source file
			File file = new File("F:\\test.txt");  
			FileReader fileReader = new FileReader(file);  
 
			preparedStatement.setInt(1,2);  
			preparedStatement.setCharacterStream(2,
				       fileReader,(int)file.length());  
 
			preparedStatement.executeUpdate(); 
		      System.out.println("File 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 codesjava
 */
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.
File stored successfully.

Download this example.
 
Next Topic: JDBC retrieve file example.
Previous Topic: JDBC batch processing.

 

Please follow and like us:
error
Posted in JDBC   








Copyright © 2019 CodesJava DMCA.com Protection Status