Wednesday, September 4, 2013

How to retrieve a BLOB in Java and display as XML

see also this on WLST


public class DBConn {

    public static String getBlobData(String driver, String url, String username, String password, String sqlQuery) throws Exception {
 String result = "";
 Class.forName(driver);
 Connection conn = DriverManager.getConnection(url, username, password);
 try {
     Statement stmt = conn.createStatement();
     try {
  ResultSet rset = stmt.executeQuery(sqlQuery);
  try {
      rset.next();
      java.sql.Blob blob = (java.sql.Blob) rset.getBlob(1);
      if (blob != null) {
   byte[] bdata = blob.getBytes(1, (int) blob.length());
   String text = new String(bdata);
   text = text.replace("<", "<");
   result = text;
      } else {
   result = "empty body";
      }

  } finally {
      try {
   rset.close();
      } catch (Exception e) {
   e.printStackTrace();
      }
  }
     } finally {
  try {
      stmt.close();
  } catch (Exception e) {
      e.printStackTrace();
  }
     }
 } finally {
     try {
  conn.close();
     } catch (Exception e) {
  e.printStackTrace();
     }
 }
 return result;
    }

}


No comments: