Storing file into Oracle database

Storing file into Oracle database

To store a large volume of data as well as text file into a table, we can take the help of CLOB (Character Large Object) datatype of SQL. Using CLOB, it is possible to store an entire file into a column of the table.
create table filetab(col1 clob, col2 int);

Example
import java.io.*;
import java.sql.*;
public class test
{
public static void main(String[] args)
{
try{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
Connection con=DriverManager.getConnection( “jdbc:oracle:thin:@localhost:1521:xe”,”system”,”system”);
PreparedStatement ps=con.prepareStatement( “insert into filetab 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();
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *