SEARCH HERE

Tuesday, December 27, 2022

Example to retrieve file from Oracle database

 

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.   
  4. public class RetrieveFile {  
  5. public static void main(String[] args) {  
  6. try{  
  7. Class.forName("oracle.jdbc.driver.OracleDriver");  
  8. Connection con=DriverManager.getConnection(  
  9. "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
  10.               
  11. PreparedStatement ps=con.prepareStatement("select * from filetable");  
  12. ResultSet rs=ps.executeQuery();  
  13. rs.next();//now on 1st row  
  14.               
  15. Clob c=rs.getClob(2);  
  16. Reader r=c.getCharacterStream();              
  17.               
  18. FileWriter fw=new FileWriter("d:\\retrivefile.txt");  
  19.               
  20. int i;  
  21. while((i=r.read())!=-1)  
  22. fw.write((char)i);  
  23.               
  24. fw.close();  
  25. con.close();  
  26.               
  27. System.out.println("success");  
  28. }catch (Exception e) {e.printStackTrace();  }  
  29. }  
  30. }  

0 comments:

Post a Comment

C++

AJAVA

C

E-RESOURCES

LKG, UKG Live Worksheets

Top