All for Joomla All for Webmasters

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:

  1. CREATE TABLE  “FILETABLE”
  2.    (    “ID” NUMBER,
  3.     “NAME” CLOB
  4.    )
  5. /
  1. import java.io.*;
  2. import java.sql.*;
  3. public class StoreFile {
  4. public static void main(String[] args) {
  5. try{
  6. Class.forName(“oracle.jdbc.driver.OracleDriver”);
  7. Connection con=DriverManager.getConnection(
  8. “jdbc:oracle:thin:@localhost:1521:xe”,“system”,“oracle”);
  9. PreparedStatement ps=con.prepareStatement(
  10. “insert into filetable values(?,?)”);
  11. File f=new File(“d:\\myfile.txt”);
  12. FileReader fr=new FileReader(f);
  13. ps.setInt(1,101);
  14. ps.setCharacterStream(2,fr,(int)f.length());
  15. int i=ps.executeUpdate();
  16. System.out.println(i+” records affected”);
  17. con.close();
  18. }catch (Exception e) {e.printStackTrace();}
  19. }
  20. }
PinIt
submit to reddit

Leave a Reply

Top