All for Joomla All for Webmasters

Example to retrieve file from Oracle database

The getClob() method of PreparedStatement is used to get file information from the database.

Syntax of getClob method

  1. public Clob getClob(int columnIndex){}

Let’s see the table structure of this example to retrieve the file.

  1. CREATE TABLE  “FILETABLE”
  2.    (    “ID” NUMBER,
  3.     “NAME” CLOB
  4.    )
  5. /

The example to retrieve the file from the Oracle database is given below.

  1. import java.io.*;
  2. import java.sql.*;
  3. public class RetrieveFile {
  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(“select * from filetable”);
  10. ResultSet rs=ps.executeQuery();
  11. rs.next();//now on 1st row
  12. Clob c=rs.getClob(2);
  13. Reader r=c.getCharacterStream();
  14. FileWriter fw=new FileWriter(“d:\\retrivefile.txt”);
  15. int i;
  16. while((i=r.read())!=-1)
  17. fw.write((char)i);
  18. fw.close();
  19. con.close();
  20. System.out.println(“success”);
  21. }catch (Exception e) {e.printStackTrace();  }
  22. }
  23. }
PinIt
submit to reddit

Leave a Reply

Top