I have retrieve data from PSCONTENT table using the java connection. While i am fetching values from the table, i got the blob value for a field CONTDATA.
I used the java method

&javaResultset.getBlob("Column Name")

but i got the value in the form of java.sql.Blob . Here i unable to convert java's Blob value to string. please suggest your ideas to convert blob value to a string...!

Views: 324

Reply to This

Replies to This Discussion

This should be really easy, but it isn't. You should be able to just use:

Blob blob = rs.getBlob(1); byte[] bdata = blob.getBytes(1, (int) blob.length()); String text = new String(bdata); 



But when I did this, it would just show me the first character. So here is what I worked up as a solution. I am no expert here. I was just going off hex dumps, SQL Developer, etc. There may be a much better way to do this.

package test;   import java.io.IOException; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;   public class TestBlob {  static final String HEXES = "0123456789ABCDEF";      // getHex adapted from http://www.rgagnon.com/javadetails/java-0596.html         public static String getHex(byte[] raw) {          if (raw == null) {                  return null;          }             final StringBuilder hex = new StringBuilder(2 * raw.length);          for (final byte b : raw) {                         hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(                                     HEXES.charAt((b & 0x0F)));          }             return hex.toString();         }     /**      * @param args   * @throws ClassNotFoundException        * @throws SQLException          * @throws IOException   */      public static void main(String[] args) throws ClassNotFoundException,                       SQLException, IOException {           Class.forName("oracle.jdbc.OracleDriver");             Connection conn = DriverManager.getConnection(                          "jdbc:oracle:thin:@//my.database.server:1521/SID",                             "SYSADM", "SYSADM");          try {                        Statement stmt = conn.createStatement();                        try {                                ResultSet rs = stmt                                             //.executeQuery("SELECT CONTDATA FROM PSCONTENT WHERE CONTNAME = 'PTAL_INCLUDE_JS'");                                            .executeQuery("SELECT CONTDATA FROM PSCONTENT WHERE CONTNAME = 'PT_HNAV_JS'");                                 try {                                        int rowNumber = 0;                                                                             // larger content items (like PT_HNAV_JS) span multiple rows                                     while (rs.next()) {                                          Blob data = rs.getBlob(1);                                              rowNumber += 1;                                                 byte[] bytes = data.getBytes(1, (int) data.length());                                                 /* Convert bytes to a hex dump of 4 digit hex groups.                                            * The first one seems to be missing the leading 00.                                             */                                              String hexString = "00" + getHex(bytes);                                                                                               /* if the length of the hex string isn't divisible by 4                                                  * then we can't group it. Just append a 00 at the end.                                                  * The last group is usually 00 anyway, making it 0000                                           */                                              if ((hexString.length() % 4) != 0) {                                                         hexString = hexString + "00";                                          }                                             StringBuilder result = new StringBuilder();                                            /* Now convert the hex dump into groupings of 4 and then                                                 * convert those hex groups into characters.                                             */                                              for (int i = 0; i < hexString.length() - 1; i += 4) {                                                    String c = hexString.substring(i, i + 4);                                                       int hexVal = Integer.parseInt(c, 16);                                                                                                          // skip nulls                                                    if(hexVal != 0) {                                                            result.append(String.valueOf(Character                                                                          .toChars(hexVal)));                                                     }                                             }                                             // print current row's contents                                          System.out.print(result.toString());                                    }                             } finally {                                        try {                                                rs.close();                                     } catch (Exception ignore) {                                       }                             }                     } finally {                                try {                                        stmt.close();                           } catch (Exception ignore) {                               }                     }             } finally {                        try {                                conn.close();                   } catch (Exception ignore) {                       }             }     }   }

it's a pleasure to meet you. I am USA Army personnel, i have an important thing to discus with you.
Please write me on my email (captkristen899@gmail.com)

RSS

PeopleSoft Jobs in US

Videos

  • Add Videos
  • View All

© 2017   Created by Maisam Agha.   Powered by

Badges  |  Report an Issue  |  Terms of Service