Example usage for java.sql Blob getBinaryStream

List of usage examples for java.sql Blob getBinaryStream

Introduction

In this page you can find the example usage for java.sql Blob getBinaryStream.

Prototype

java.io.InputStream getBinaryStream() throws SQLException;

Source Link

Document

Retrieves the BLOB value designated by this Blob instance as a stream.

Usage

From source file:EmployeeShow.java

public static void main(String[] args) throws Exception {
    ImageIcon image;/* www . j a v a2s  .  c o m*/

    Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/c:\\employee");

    Statement s = con.createStatement();
    ResultSet rs = s.executeQuery("select photo from employee where name = 'Duke'");
    if (rs.next()) {
        Blob photo = rs.getBlob(1);
        ObjectInputStream ois = null;
        ois = new ObjectInputStream(photo.getBinaryStream());
        image = (ImageIcon) ois.readObject();
    }
    s.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");

    Statement stmt = conn.createStatement();

    createBlobClobTables(stmt);//from w  w w.  ja  va2s  .c o m

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobClob VALUES(40,?,?)");

    File file = new File("blob.txt");
    FileInputStream fis = new FileInputStream(file);
    pstmt.setBinaryStream(1, fis, (int) file.length());

    file = new File("clob.txt");
    fis = new FileInputStream(file);
    pstmt.setAsciiStream(2, fis, (int) file.length());
    fis.close();

    pstmt.execute();

    ResultSet rs = stmt.executeQuery("SELECT * FROM BlobClob WHERE id = 40");
    rs.next();

    java.sql.Blob blob = rs.getBlob(2);
    java.sql.Clob clob = rs.getClob(3);

    byte blobVal[] = new byte[(int) blob.length()];
    InputStream blobIs = blob.getBinaryStream();
    blobIs.read(blobVal);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(blobVal);
    blobIs.close();

    char clobVal[] = new char[(int) clob.length()];
    Reader r = clob.getCharacterStream();
    r.read(clobVal);
    StringWriter sw = new StringWriter();
    sw.write(clobVal);

    r.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");

    Statement stmt = conn.createStatement();

    createBlobClobTables(stmt);//  w  ww. j  a  v  a  2s .  c o m

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobClob VALUES(40,?,?)");

    File file = new File("blob.txt");
    FileInputStream fis = new FileInputStream(file);
    pstmt.setBinaryStream(1, fis, (int) file.length());

    file = new File("clob.txt");
    fis = new FileInputStream(file);
    pstmt.setAsciiStream(2, fis, (int) file.length());
    fis.close();

    pstmt.execute();

    ResultSet rs = stmt.executeQuery("SELECT * FROM BlobClob WHERE id = 40");
    rs.next();

    java.sql.Blob blob = rs.getBlob(2);
    java.sql.Clob clob = rs.getClob("myClobColumn");

    byte blobVal[] = new byte[(int) blob.length()];
    InputStream blobIs = blob.getBinaryStream();
    blobIs.read(blobVal);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(blobVal);
    blobIs.close();

    char clobVal[] = new char[(int) clob.length()];
    Reader r = clob.getCharacterStream();
    r.read(clobVal);
    StringWriter sw = new StringWriter();
    sw.write(clobVal);

    r.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//w w  w.j  a v a2s . c o m

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT col_blob FROM mysql_all_table");

    if (rs.next()) {
        Blob blob = rs.getBlob("col_blob");
        long blobLength = blob.length();

        int pos = 1; // position is 1-based
        int len = 10;
        byte[] bytes = blob.getBytes(pos, len);

        InputStream is = blob.getBinaryStream();
        int b = is.read();
    }
}

From source file:Main.java

public static boolean isImage(Blob blob) throws Exception {
    InputStream in = null;//from w w  w  . j av a  2  s.c o  m
    byte[] bytes = new byte[8];
    try {
        in = blob.getBinaryStream();

        in.read(bytes);
        return isImage(bytes);
    } catch (Exception ex) {
        throw new Exception(ex.toString());
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
                throw new Exception(ex.toString());
            }
        }
        bytes = null;
    }
}

From source file:com.webbfontaine.valuewebb.irms.impl.data.AttachedDocDataSource.java

private static InputStream stream(Blob data) throws SQLException {
    if (data != null) {
        LOGGER.debug("Attached doc LOB data length: {}", data.length());
        return data.getBinaryStream();
    }/*ww w  .j  av a 2 s.  c  om*/

    return null;
}

From source file:com.plexobject.testplayer.dao.jdbc.GenericDaoJdbc.java

protected static Object toObject(Blob blob) {
    try {/*from   w ww .  j a v  a2  s .c  o m*/
        if (blob == null)
            return null;
        InputStream in = blob.getBinaryStream();
        if (in == null)
            return null;
        int c;
        ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
        while ((c = in.read()) != -1) {
            out.write(c);
        }
        byte[] b = out.toByteArray();
        if (b.length == 0)
            return null;
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(b));
        Object object = ois.readObject();
        ois.close();
        return object;
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new DaoException("Failed to deserialize", e);
    }
}

From source file:br.com.manish.ahy.kernel.util.JPAUtil.java

public static byte[] blobToBytes(Blob blob) {
    byte[] byteData = null;
    try {/* w w  w .j  a va 2s. c  om*/
        InputStream is = blob.getBinaryStream();
        ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
        byte[] bytes = new byte[512];

        int readBytes;
        while ((readBytes = is.read(bytes)) > 0) {
            os.write(bytes, 0, readBytes);
        }

        byteData = os.toByteArray();

        is.close();
        os.close();
    } catch (Exception e) {
        throw new OopsException(e, "Error reading file data.");
    }
    return byteData;
}

From source file:com.jaspersoft.jasperserver.core.util.StreamUtil.java

public static byte[] readData(Blob blob) {
    if (blob == null) {
        return null;
    }//from w ww.j av  a  2s. com

    try {
        return readData(blob.getBinaryStream());
    } catch (SQLException e) {
        log.error("Error while reading blob data", e);
        throw new JSExceptionWrapper(e);
    }
}

From source file:com.hangum.tadpole.rdb.core.editors.main.utils.TableToDataUtils.java

/**
 *  ? ??  ./*from  www .j  a v a 2  s. com*/
 * 
 * @param columnObject
 * @param intType
 * @param name
 * @return
 */
public static TableColumnDAO getTableData(Object columnObject, Integer intType, String name) {
    TableColumnDAO columnDao = new TableColumnDAO();

    if (intType == null)
        intType = java.sql.Types.VARCHAR;
    String strType = RDBTypeToJavaTypeUtils.getRDBType(intType);

    columnDao.setName(name);
    columnDao.setType(strType);

    if (columnObject != null) {
        //  ? ??  clob?? ? ?.
        if (columnObject instanceof java.sql.Clob) {
            Clob cl = (Clob) columnObject;

            StringBuffer clobContent = new StringBuffer();
            String readBuffer = new String();

            // ? ? clob ? ? ? ? .
            BufferedReader bufferedReader;
            try {
                bufferedReader = new java.io.BufferedReader(cl.getCharacterStream());
                while ((readBuffer = bufferedReader.readLine()) != null) {
                    clobContent.append(readBuffer);
                }

                columnDao.setCol_value(clobContent.toString());
            } catch (Exception e) {
                logger.error("Clob column echeck", e); //$NON-NLS-1$
            }
        } else if (columnObject instanceof java.sql.Blob) {
            try {
                Blob blob = (Blob) columnObject;
                columnDao.setCol_value(blob.getBinaryStream());

            } catch (Exception e) {
                logger.error("Blob column echeck", e); //$NON-NLS-1$
            }

        } else if (columnObject instanceof byte[]) {
            byte[] b = (byte[]) columnObject;
            StringBuffer str = new StringBuffer();
            try {
                for (byte buf : b) {
                    str.append(buf);
                }
                str.append("\n\nHex : " + new BigInteger(str.toString(), 2).toString(16)); //$NON-NLS-1$

                columnDao.setCol_value(str.toString());
            } catch (Exception e) {
                logger.error("Clob column echeck", e); //$NON-NLS-1$
            }
        } else {
            columnDao.setCol_value(columnObject.toString());
        }
    } else {
        columnDao.setCol_value(GetPreferenceGeneral.getResultNull());
    }

    return columnDao;
}