[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 870
  • Last Modified:

Read Blob from db and write to the File System

How to read a blob from the database and write into a file in the filesystem.
0
rajesh1015
Asked:
rajesh1015
3 Solutions
 
StillUnAwareCommented:
You need Java Database Connectivity (jdbc) driver for Your database and learn to use it. More info on jdbc:

http://java.sun.com/products/jdbc/overview.html
http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/getstart/GettingStartedTOC.fm.html
0
 
rajesh1015Author Commented:
The Java will run a Concurrent Progam in Oracle Apps. In that case, I do not have to think about the connectivity.
0
 
StillUnAwareCommented:
Or if You already have a Connection object and a table 't1' with column 'c' of type BLOB:

try {
    Connection conn = ...
    PreparedStatement ps = conn.prepareStatement("SELECT c FROM t1");
    ResultSet rs = ps.executeQuery();
    int i = 1;
    do {
        Blob b = rs.getBlob("c");
        byte[] bytes = b.getBytes(0, b.length());
        FileOutputStream fos = new FileOutputStream("f"+i++);
        fos.write(bytes);
        fos.close();
    } while(rs.next());
} catch(Exception e) {
    e.printStackTrace();
}
0
 
CEHJCommented:
  int i = 1;
   ResultSet rs = ps.executeQuery("select x from y");
   while (rs.hasNext()) {
     try {
         InputStream in = rs.getBinaryStream(1);
         FileOutputStream out = new FileOutputStream("f"+i++);
         int bytesRead = -1;
         byte[] buffer = new byte[ 1<<10 ]; // 1KiB buffer
         while ((bytesRead = in.read(buffer)) > -1) {
         out.write(buffer, 0, bytesRead);
         }
         out.close();
     } catch(Exception e) {
       e.printStackTrace();
     }
   }
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now