Java SQL Blob getStringValue(Blob blob)

Here you can find the source of getStringValue(Blob blob)

Description

Return a string representation of an object

License

Open Source License

Parameter

Parameter Description
blob a parameter

Exception

Parameter Description
SQLException an exception

Declaration

public static String getStringValue(Blob blob) throws SQLException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;

public class Main {
    /**//from w w w . j av  a 2  s .  co  m
     * Return a string representation of an object
     * @param o
     * @return
     * @throws SQLException
     */
    public static String getStringValue(Object o) throws SQLException {
        String s = null;
        if (o != null) {
            if (o instanceof Clob) {
                Clob clob = (Clob) o;
                s = clob.getSubString(1, (int) clob.length());
            } else if (o instanceof String) {
                s = (String) o;
            } else {
                s = o.toString();
            }
        }
        return s;
    }

    /**
     * Return a string representation of an object
     * @param blob
     * @return
     * @throws SQLException
     */
    public static String getStringValue(Blob blob) throws SQLException {
        String s = null;
        InputStream is = null;
        if (blob != null) {
            is = blob.getBinaryStream();
        }
        if (is != null) {
            BufferedInputStream bis = new BufferedInputStream(is, 4096);
            StringBuffer sb = new StringBuffer();
            int ch = 0;
            try {
                while ((ch = bis.read()) != -1) {
                    try {
                        sb.append((char) ch);
                    } catch (Exception e) {
                        sb.append(' ');
                    }
                }
            } catch (IOException e) {
                throw new SQLException(e.getMessage());
            }
            s = sb.toString();
        }
        return s;
    }
}

Related

  1. getBlobType(byte[] image)
  2. getDataFromBlob(Blob b)
  3. getDataFromBlob(Blob b)
  4. getLongBlobTypeString(Connection conn)
  5. getString(Blob blob)
  6. isBlob(Object object)
  7. readBlob(Blob blob)
  8. readBytes(Blob blob, byte[] defaultValue)
  9. safeFree(Blob blob)