Example to store file in Oracle database:

The setCharacterStream() method of PreparedStatement is used to set character information into the parameterIndex.

Syntax:

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

For storing file into the database, CLOB (Character Large Object) datatype is used in the table. For example:

Example
snippet
CREATE TABLE  "FILETABLE" 
   (	"ID" NUMBER, 
	"NAME" CLOB
   )
/

Java Example to store file in database

Example
snippet
import java.io.*;
import java.sql.*;

public class StoreFile {
public static void main(String[] args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
			
PreparedStatement ps=con.prepareStatement(
"insert into filetable values(?,?)");
			
File f=new File("d:\\myfile.txt");
FileReader fr=new FileReader(f);
			
ps.setInt(1,101);
ps.setCharacterStream(2,fr,(int)f.length());
int i=ps.executeUpdate();
System.out.println(i+" records affected");
			
con.close();
			
}catch (Exception e) {e.printStackTrace();}
}
}
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +