Java Code Examples for java.sql.ResultSet.getBlob()

The following are Jave code examples for showing how to use getBlob() of the java.sql.ResultSet class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: wherehowsX   File: AzDbCommunicator.java   View Source Code Vote up 8 votes
public String getExecLog(long execId, String jobName)
  throws SQLException, IOException {
  System.out.println("start");
  String cmd = "select log from execution_logs where exec_id = " + execId +
    " and name = '" + jobName + "'and attempt = 0 order by start_byte;";
  System.out.println(cmd);
  Statement statement = conn.createStatement();
  ResultSet rs = statement.executeQuery(cmd);
  StringBuilder sb = new StringBuilder();
  while (rs.next()) {
    Blob logBlob = rs.getBlob("log");
    GZIPInputStream gzip = new GZIPInputStream(logBlob.getBinaryStream());
    sb.append(IOUtils.toString(gzip, "UTF-8"));
  }
  statement.close();
  System.out.println("stop");
  return sb.toString();
}
 
Example 2
Project: asura   File: WebLogicDelegate.java   View Source Code Vote up 7 votes
/**
 * <p>
 * This method should be overridden by any delegate subclasses that need
 * special handling for BLOBs. The default implementation uses standard
 * JDBC <code>java.sql.Blob</code> operations.
 * </p>
 * 
 * @param rs
 *          the result set, already queued to the correct row
 * @param colName
 *          the column name for the BLOB
 * @return the deserialized Object from the ResultSet BLOB
 * @throws ClassNotFoundException
 *           if a class found during deserialization cannot be found
 * @throws IOException
 *           if deserialization causes an error
 */
protected Object getObjectFromBlob(ResultSet rs, String colName)
    throws ClassNotFoundException, IOException, SQLException {
    
    Object obj = null;

    Blob blobLocator = rs.getBlob(colName);
    InputStream binaryInput = null;
    try {
        if (null != blobLocator && blobLocator.length() > 0) {
            binaryInput = blobLocator.getBinaryStream();
        }
    } catch (Exception ignore) {
    }

    if (null != binaryInput) {
        ObjectInputStream in = new ObjectInputStream(binaryInput);
        try {
            obj = in.readObject();
        } finally {
            in.close();
        }
    }

    return obj;
}
 
Example 3
Project: asura   File: StdJDBCDelegate.java   View Source Code Vote up 7 votes
/**
 * <p>
 * This method should be overridden by any delegate subclasses that need
 * special handling for BLOBs. The default implementation uses standard
 * JDBC <code>java.sql.Blob</code> operations.
 * </p>
 * 
 * @param rs
 *          the result set, already queued to the correct row
 * @param colName
 *          the column name for the BLOB
 * @return the deserialized Object from the ResultSet BLOB
 * @throws ClassNotFoundException
 *           if a class found during deserialization cannot be found
 * @throws IOException
 *           if deserialization causes an error
 */
protected Object getObjectFromBlob(ResultSet rs, String colName)
    throws ClassNotFoundException, IOException, SQLException {
    Object obj = null;

    Blob blobLocator = rs.getBlob(colName);
    if (blobLocator != null && blobLocator.length() != 0) {
        InputStream binaryInput = blobLocator.getBinaryStream();

        if (null != binaryInput) {
            if (binaryInput instanceof ByteArrayInputStream
                && ((ByteArrayInputStream) binaryInput).available() == 0 ) {
                //do nothing
            } else {
                ObjectInputStream in = new ObjectInputStream(binaryInput);
                try {
                    obj = in.readObject();
                } finally {
                    in.close();
                }
            }
        }

    }
    return obj;
}
 
Example 4
Project: lams   File: WebLogicDelegate.java   View Source Code Vote up 7 votes
@Override
protected Object getJobDataFromBlob(ResultSet rs, String colName)
    throws ClassNotFoundException, IOException, SQLException {
    
    if (canUseProperties()) {
        Blob blobLocator = rs.getBlob(colName);
        InputStream binaryInput = null;
        try {
            if (null != blobLocator && blobLocator.length() > 0) {
                binaryInput = blobLocator.getBinaryStream();
            }
        } catch (Exception ignore) {
        }
        return binaryInput;
    }

    return getObjectFromBlob(rs, colName);
}
 
Example 5
Project: lams   File: StdJDBCDelegate.java   View Source Code Vote up 7 votes
/**
 * <p>
 * This method should be overridden by any delegate subclasses that need
 * special handling for BLOBs. The default implementation uses standard
 * JDBC <code>java.sql.Blob</code> operations.
 * </p>
 * 
 * @param rs
 *          the result set, already queued to the correct row
 * @param colName
 *          the column name for the BLOB
 * @return the deserialized Object from the ResultSet BLOB
 * @throws ClassNotFoundException
 *           if a class found during deserialization cannot be found
 * @throws IOException
 *           if deserialization causes an error
 */
protected Object getObjectFromBlob(ResultSet rs, String colName)
    throws ClassNotFoundException, IOException, SQLException {
    Object obj = null;

    Blob blobLocator = rs.getBlob(colName);
    if (blobLocator != null && blobLocator.length() != 0) {
        InputStream binaryInput = blobLocator.getBinaryStream();

        if (null != binaryInput) {
            if (binaryInput instanceof ByteArrayInputStream
                && ((ByteArrayInputStream) binaryInput).available() == 0 ) {
                //do nothing
            } else {
                ObjectInputStream in = new ObjectInputStream(binaryInput);
                try {
                    obj = in.readObject();
                } finally {
                    in.close();
                }
            }
        }

    }
    return obj;
}
 
Example 6
Project: UtilsMaven   File: DBUtils.java   View Source Code Vote up 6 votes
/**
 * 用于查询记录中大数据类型值,若有多条记录符合要求则返回第一条记录的大数据
 *
 * @param sql 查询SQL语句,查询字段的类型必须为Blob类型
 * @param arg 传入的占位符的参数
 * @return 返回查询的大数据,封装在Map中,若没有符合条件的记录则返回空Map
 */
public static Map<String, byte[]> getBigData(String sql, Object... arg) {
    Map<String, byte[]> bigDataMap = new HashMap<String, byte[]>();
    Connection connection = JDBCUtils.getConnection();
    PreparedStatement ps = null;
    ResultSet result = null;

    // 填充占位符
    try {
        ps = connection.prepareStatement(sql);
        for (int i = 0; i < arg.length; i++) {
            ps.setObject(i + 1, arg[i]);
        }

        // 执行SQL语句
        result = ps.executeQuery();
        // 获取字段名
        List<String> columnList = DBUtils.getColumnLabels(result);
        // 遍历结果集
        while (result.next()) {
            // 遍历字段名获取相应大数据值
            for (String column : columnList) {
                Blob data = result.getBlob(column);
                byte[] datas = data.getBytes(1, (int) data.length());
                bigDataMap.put(column, datas);
            }
            break;
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        JDBCUtils.release(result, ps, connection);
    }

    return bigDataMap;
}
 
Example 7
Project: lams   File: OracleLobHandler.java   View Source Code Vote up 6 votes
@Override
public byte[] getBlobAsBytes(ResultSet rs, int columnIndex) throws SQLException {
	logger.debug("Returning Oracle BLOB as bytes");
	Blob blob = rs.getBlob(columnIndex);
	initializeResourcesBeforeRead(rs.getStatement().getConnection(), blob);
	byte[] retVal = (blob != null ? blob.getBytes(1, (int) blob.length()) : null);
	releaseResourcesAfterRead(rs.getStatement().getConnection(), blob);
	return retVal;
}
 
Example 8
Project: lams   File: OracleLobHandler.java   View Source Code Vote up 6 votes
@Override
public InputStream getBlobAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException {
	logger.debug("Returning Oracle BLOB as binary stream");
	Blob blob = rs.getBlob(columnIndex);
	initializeResourcesBeforeRead(rs.getStatement().getConnection(), blob);
	InputStream retVal = (blob != null ? blob.getBinaryStream() : null);
	releaseResourcesAfterRead(rs.getStatement().getConnection(), blob);
	return retVal;
}
 
Example 9
Project: tangyuan2   File: BlobTypeHandler.java   View Source Code Vote up 6 votes
@Override
public byte[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
	Blob blob = rs.getBlob(columnIndex);
	byte[] returnValue = null;
	if (null != blob) {
		returnValue = blob.getBytes(1, (int) blob.length());
	}
	return returnValue;
}
 
Example 10
Project: lams   File: CacheDelegate.java   View Source Code Vote up 6 votes
/**
 * {@inheritDoc}
 * <p>
 * Caché requires {@code java.sql.Blob} instances to be explicitly freed.
 */
@Override
protected Object getObjectFromBlob(ResultSet rs, String colName) throws ClassNotFoundException, IOException, SQLException {
    Blob blob = rs.getBlob(colName);
    if (blob == null) {
        return null;
    } else {
        try {
            if (blob.length() == 0) {
                return null;
            } else {
                InputStream binaryInput = blob.getBinaryStream();
                if (binaryInput == null) {
                    return null;
                } else if (binaryInput instanceof ByteArrayInputStream && ((ByteArrayInputStream) binaryInput).available() == 0 ) {
                    return null;
                } else {
                    ObjectInputStream in = new ObjectInputStream(binaryInput);
                    try {
                        return in.readObject();
                    } finally {
                        in.close();
                    }
                }
            }
        } finally {
            blob.free();
        }
    }
}
 
Example 11
Project: lams   File: OracleDelegate.java   View Source Code Vote up 6 votes
@SuppressWarnings("deprecation")
protected Blob writeDataToBlob(ResultSet rs, int column, byte[] data) throws SQLException {

    Blob blob = rs.getBlob(column); // get blob

    if (blob == null) { 
        throw new SQLException("Driver's Blob representation is null!");
    }
    
    if (blob instanceof oracle.sql.BLOB) { // is it an oracle blob?
        ((oracle.sql.BLOB) blob).putBytes(1, data);
        ((oracle.sql.BLOB) blob).trim(data.length);
        return blob;
    } else {
        throw new SQLException(
                "Driver's Blob representation is of an unsupported type: "
                        + blob.getClass().getName());
    }
}
 
Example 12
Project: lams   File: ByteArrayBlobType.java   View Source Code Vote up 6 votes
protected Object get(ResultSet rs, String name) throws SQLException {
	Blob blob = rs.getBlob( name );
	if ( rs.wasNull() ) return null;
	int length = (int) blob.length();
	byte[] primaryResult = blob.getBytes( 1, length );
	return wrap( primaryResult );
}
 
Example 13
Project: aliyun-maxcompute-data-collectors   File: LargeObjectLoader.java   View Source Code Vote up 5 votes
/**
 * Actually read a BlobRef instance from the ResultSet and materialize
 * the data either inline or to a file.
 *
 * @param colNum the column of the ResultSet's current row to read.
 * @param r the ResultSet to read from.
 * @return a BlobRef encapsulating the data in this field.
 * @throws IOException if an error occurs writing to the FileSystem.
 * @throws SQLException if an error occurs reading from the database.
 */
public com.cloudera.sqoop.lib.BlobRef readBlobRef(int colNum, ResultSet r)
    throws IOException, InterruptedException, SQLException {

  long maxInlineLobLen = conf.getLong(
      MAX_INLINE_LOB_LEN_KEY,
      DEFAULT_MAX_LOB_LENGTH);

  Blob b = r.getBlob(colNum);
  if (null == b) {
    return null;
  } else if (b.length() > maxInlineLobLen) {
    // Deserialize very large BLOBs into separate files.
    long len = b.length();
    LobFile.Writer lobWriter = getBlobWriter();

    long recordOffset = lobWriter.tell();
    InputStream is = null;
    OutputStream os = lobWriter.writeBlobRecord(len);
    try {
      is = b.getBinaryStream();
      copyAll(is, os);
    } finally {
      if (null != os) {
        os.close();
      }

      if (null != is) {
        is.close();
      }

      // Mark the record as finished.
      lobWriter.finishRecord();
    }

    return new com.cloudera.sqoop.lib.BlobRef(
        getRelativePath(curBlobWriter), recordOffset, len);
  } else {
    // This is a 1-based array.
    return new com.cloudera.sqoop.lib.BlobRef(
        b.getBytes(1, (int) b.length()));
  }
}
 
Example 14
Project: Lagerta   File: BlobValueTransformer.java   View Source Code Vote up 5 votes
@Override
public Object get(ResultSet resultSet, int index) throws SQLException {
    Blob blob = resultSet.getBlob(index);
    if (blob != null) {
        ByteBuffer wrap = ByteBuffer.wrap(blob.getBytes(0, (int) blob.length()));
        return serializer.deserialize(wrap);
    }
    return null;
}
 
Example 15
Project: calcite-avatica   File: AvaticaResultSetConversionsTest.java   View Source Code Vote up 5 votes
public void testGetClob(ResultSet resultSet) throws SQLException {
  try {
    resultSet.getBlob(ordinal);
    fail("Was expecting to throw SQLDataException");
  } catch (Exception e) {
    assertThat(e, isA((Class) SQLDataException.class)); // success
  }
}
 
Example 16
Project: lams   File: DefaultLobHandler.java   View Source Code Vote up 5 votes
@Override
public InputStream getBlobAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException {
	logger.debug("Returning BLOB as binary stream");
	if (this.wrapAsLob) {
		Blob blob = rs.getBlob(columnIndex);
		return blob.getBinaryStream();
	}
	else {
		return rs.getBinaryStream(columnIndex);
	}
}
 
Example 17
Project: lams   File: CacheDelegate.java   View Source Code Vote up 5 votes
/**
 * {@inheritDoc}
 * <p>
 * Caché requires {@code java.sql.Blob} instances to be explicitly freed.
 */
@Override
protected Object getJobDataFromBlob(ResultSet rs, String colName) throws ClassNotFoundException, IOException, SQLException {
    if (canUseProperties()) {
        Blob blob = rs.getBlob(colName);
        if (blob == null) {
            return null;
        } else {
            return new BlobFreeingStream(blob, blob.getBinaryStream());
        }
    } else {
        return getObjectFromBlob(rs, colName);
    }
}
 
Example 18
Project: calcite-avatica   File: AvaticaResultSetConversionsTest.java   View Source Code Vote up 5 votes
public void testGetBlob(ResultSet resultSet) throws SQLException {
  try {
    resultSet.getBlob(ordinal);
    fail("Was expecting to throw SQLDataException");
  } catch (Exception e) {
    assertThat(e, isA((Class) SQLDataException.class)); // success
  }
}
 
Example 19
Project: MineDonate   File: Entities.java   View Source Code Vote up 4 votes
@Override
public void loadCategoryFromObject ( Object o ) {
	
	ResultSet rs = ( ResultSet ) o ;
	
    try {
    	
        while (rs.next()) {
        	
            final EntityInfo info = new EntityInfo(shopId, catId, rs.getInt("id"), rs.getInt("rating"), rs.getInt("cost"), rs.getBlob("data"), rs.getString("name"), rs.getInt("lim"));
          
            this.addMerch(info);
            
        }
        
    } catch (SQLException e) {
    	
        e.printStackTrace();
        
    }
    
    MineDonate . logInfo ( "Loaded " + m_Merch . size() + " merch in " + toString ( ) ) ;

}
 
Example 20
Project: MineDonate   File: ItemNBlocks.java   View Source Code Vote up 3 votes
@Override
public void loadCategoryFromObject ( Object o ) {
	
	ResultSet rs = ( ResultSet ) o ;
	
    try {
    	
        while (rs.next()) {
        	
            final ItemInfo info = new ItemInfo(shopId, catId, rs.getInt("id"), rs.getInt("rating"),
                    rs.getInt("cost"),
                    rs.getString("name"),
                    rs.getInt("lim"),
                    rs.getBlob("stack_data"));
            
            this.addMerch(info);
            
        }
        
    } catch (SQLException e) {
    	
        e.printStackTrace();
        
    }
    
    MineDonate . logInfo ( "Loaded " + m_Merch . size() + " merch in " + toString ( ) ) ;
    
}