Retrieve File JDBC


You have seen earlier how to store a file in database table office. Let us retrieve the file (Retrieve File JDBC), "Expenditure.txt" from table and write into another file "Liabilities.txt".
Example in Retrieve File JDBC
import java.io.*;  
import java.sql.*;  
  
public class RetrieveFile 
{  
  public static void main(String[] args) throws Exception
  { 
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:snrao", "scott", "tiger");
              
    PreparedStatement pst = con.prepareStatement("select * from office");  
    ResultSet res = pst.executeQuery();  
    res.next();                              // move the pointer first row
              
    Clob c1 = res.getClob(2);                // 2 indicates the second field in office table
    Reader r1 = c1.getCharacterStream();              
              
    FileWriter fw = new FileWriter("C:\\Liabilities.txt");  
              
    int k;  
    while((k = r1.read()) != -1)  
    {
      fw.write((char)k);  
    }

    System.out.println("File is retrieved");                
    fw.close();  
    r1.close();
    res.close();
    pst.close();
    con.close();  
  }  
}  

1 thought on “Retrieve File JDBC”

  1. Logeshwari babu

    please provide an example to download image from an url and from a database.
    also for these concepts provide step by step explanation as before for better understanding.

Leave a Comment

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