skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Friday, October 5, 2012

Blob Reader

Posted by Raju Gupta at 11:12 PM – 0 comments
 
The BLOB content can be read using ObjectInputStream of JAVA. This code is useful in reading the content of a BLOB type column of the database and writes its contents to a text file.

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;





public class BlobReader {

 /**
  * @param args
  */
 
 public static String INITIAL_DETAILS_QUERY = "select VNDR_ERR_REC_ID, REC_CONTENT from EDD.VNDR_ERR_REC WHERE FILE_TYPE = 'URF' AND EDD_CUST_ID = ? ORDER BY EDD_CUST_ID  DESC";
 
 private static boolean DEBUG = false;
 
 public static void main(String[] args) {
  
  getInitialDetails(new Integer(args[0]));
  System.out.println(args[0]); 
 }
 
  
 private static void  writeFile(StringBuffer BC) throws IOException{
  String file_name = "D:\\Raj\\blobContent.txt";
  System.out.println(BC);
   BufferedWriter bufwri = new BufferedWriter(new FileWriter(file_name));

    int str_len = BC.length();
    System.out.println(BC.length());
    for (int i = 0; i < str_len; i++) {
   
     bufwri.write(BC.charAt(i));
    }
    bufwri.close();    
 }
 
 
 
 private static void getInitialDetails(Integer integerEddCustId) {
  Connection eddConnection = null;
  PreparedStatement eddPstmt = null;
  PreparedStatement eddLobPstmt = null;
  ResultSet eddResultSet = null;
  ResultSet eddLobResultSet = null;
  
  String l_str_lob = null;
  
  System.out.println("Connected to DB");
  try{
   Class.forName("oracle.jdbc.driver.OracleDriver");
   if(DEBUG){
    eddConnection = DriverManager.getConnection(
    "Connection",
    "username",
          "password");

   }
   else {
    eddConnection = DriverManager.getConnection(
    "Connection",
    "Username",
    "Password");
   }
   eddPstmt = eddConnection.prepareStatement(INITIAL_DETAILS_QUERY);
   System.out.println(INITIAL_DETAILS_QUERY.replaceAll("\\?",String.valueOf(integerEddCustId)));
   eddPstmt.setInt(1, integerEddCustId.intValue());
   eddResultSet = eddPstmt.executeQuery();
   StringBuffer blCn = new StringBuffer();
   ArrayList lst = readLongRaw(eddResultSet);
   Iterator itr = lst.iterator();
   String tmpp = null;   
   while(itr.hasNext()) {
    tmpp = (String)itr.next();    
    blCn.append(tmpp);
   }
   
   writeFile(blCn);
  }
  catch (SQLException sqlex) {
   sqlex.printStackTrace();
   
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  finally{
   if(eddPstmt != null) {
    try {
     eddPstmt.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if(eddLobPstmt != null) {
    try {
     eddLobPstmt.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   
  }
 }
 
 private static ArrayList readLongRaw(ResultSet rs) throws SQLException, IOException, ClassNotFoundException {
  InputStream inputStream = null;
  BufferedInputStream bis = null;
  ArrayList lst = new ArrayList();
  ObjectInputStream ois = null;
    
  while(rs.next()) {
  
   inputStream = rs.getBinaryStream("rec_content");
   String tempdata = rs.getString("VNDR_ERR_REC_ID");
   
   try  {
   ois = new ObjectInputStream(inputStream);
   tempdata = tempdata + (String)ois.readObject();
   System.out.println("The data for this record::"+tempdata);
   }
   catch(Exception ex) {
    ex.printStackTrace();
   }
   finally {
    if(ois != null) {
     ois.close();
    }
   }
   
   lst.add(tempdata);
   if(inputStream != null) {
    inputStream.close();
   }
  }
  
  return lst;
 }

}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger