下面列出了java.sql.SQLData#java.sql.Blob 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public LocatedBlocks getLocatedBlocks(String src, long start, long length) throws IOException {
try {
try (PreparedStatement statement = connection.prepareStatement("call SYSCS_UTIL.SYSCS_HDFS_OPERATION(?, ?)")) {
statement.setString(1, src);
statement.setString(2, "blocks");
try (ResultSet rs = statement.executeQuery()) {
if (!rs.next()) {
throw new IOException("No results for getFileStatus");
}
Blob blob = rs.getBlob(1);
byte[] bytes = blob.getBytes(1, (int) blob.length());
HdfsProtos.LocatedBlocksProto lbp = HdfsProtos.LocatedBlocksProto.parseFrom(bytes);
// TODO return PBHelper.convert(lbp);
return null;
}
}
} catch (SQLException e) {
throw new IOException(e);
}
}
protected void insertToGFXDTable(Connection conn, int[] eid, String[] emp_name,
int[] deptid, Date[] since, String[] addr, Blob[] picture, String[] ssn,
int size, List<SQLException> exceptions, boolean isPut) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(isPut ? put : insert);
int tid = getMyTid();
int count = -1;
for (int i=0 ; i<size ; i++) {
try {
count = insertToTable(stmt, eid[i], emp_name[i], deptid[i], since[i], addr[i],
picture[i], ssn[i], tid, isPut);
if (count != ((Integer)verifyRowCount.get(tid+"_insert"+i)).intValue()) {
Log.getLogWriter().info("Gfxd insert has different row count from that of derby " +
"derby inserted " + ((Integer)verifyRowCount.get(tid+"_insert"+i)).intValue() +
" but gfxd inserted " + count);
}
} catch (SQLException se) {
SQLHelper.handleGFGFXDException(se, exceptions);
}
}
}
private static void checkBlob(ResultSet rs, int index, byte[] expected, int key) {
try {
byte[] actual = rs.getBytes(index);
Assert.assertArrayEquals(expected, actual);
if (expected != null) {
DataInputStream dataInputStream = new DataInputStream(rs.getBinaryStream(index));
byte[] actualFromStream = new byte[actual.length];
dataInputStream.readFully(actualFromStream);
Assert.assertArrayEquals("error at key " + key, expected, actualFromStream);
Blob blob = rs.getBlob(index);
assertEquals("error at key " + key, blob.length(), actual.length);
DataInputStream dataInputStream2 = new DataInputStream(blob.getBinaryStream());
byte[] actualFromStream2 = new byte[actual.length];
dataInputStream2.readFully(actualFromStream2);
Assert.assertArrayEquals("error at key " + key, expected, actualFromStream2);
}
byte[] object = (byte[]) rs.getObject(index);
Assert.assertArrayEquals((byte[]) expected, object);
} catch (SQLException | IOException e) {
throw new RuntimeException(e);
}
}
private List<byte[]> getRollbackInfo(String xid, long branchId, Connection conn, String resourceId) throws SQLException {
ResultSet rs = null;
PreparedStatement selectPST = null;
selectPST = conn.prepareStatement(SELECT_UNDO_LOG_SQL);
selectPST.setLong(1, branchId);
selectPST.setString(2, xid);
rs = selectPST.executeQuery();
List<byte[]> result = new ArrayList<>();
try {
while (rs.next()) {
Blob b = rs.getBlob("rollback_info");
byte[] rollbackInfo = BlobUtils.blob2Bytes(b);
result.add(rollbackInfo);
}
} finally {
rs.close();
}
return result;
}
@Override
public void setBlob(int parameterIndex, Blob x) {
if (x == null) {
setNull(parameterIndex);
} else {
try {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
bytesOut.write('\'');
StringUtils.escapeblockFast(x.getBytes(1, (int) x.length()), bytesOut, (int) x.length(),
this.session.getServerSession().useAnsiQuotedIdentifiers());
bytesOut.write('\'');
setValue(parameterIndex, bytesOut.toByteArray(), MysqlType.BLOB);
} catch (Throwable t) {
throw ExceptionFactory.createException(t.getMessage(), t);
}
}
}
static final Object setObject(int targetType, Blob source) {
switch (targetType) {
case ClientTypes.BLOB:
return source;
default:
throw new IllegalArgumentException("SQLState.LANG_DATA_TYPE_SET_MISMATCH java.sql.Blob" + ClientTypes.getTypeString(targetType));
}
}
@Override
public void setPreparedValue(PreparedStatement pStm, int col, Object o, int type )
throws SQLException
{
if( type == Types.BLOB
|| type == Types.LONGVARBINARY
|| type == Types.VARBINARY
|| type == Types.BINARY )
{
if( o != null )
{
o = outputValue( o );
// System.out.println( "BLOB class: " + o.getClass() + " Type: " + type + " Col: " + col );
if( o instanceof byte[] )
{
pStm.setBytes( col + 1, ( byte[] )o );
}
else
{
Blob b = ( Blob )o;
pStm.setBytes( col + 1, b.getBytes( 1, ( int )b.length() ) );
}
}
else
{
pStm.setBytes( col + 1, null );
}
}
else
{
super.setPreparedValue( pStm, col, o, type );
}
}
@Override
public Blob getBlob(int columnIndex) throws SQLException {
if (this.lobsInExtFile) {
return super.getBlob(columnIndex);
} else {
// data is in the main export file, stored in hex format.
String hexData = getCurrentRow()[columnIndex - 1];
byte[] data = null;
if (hexData != null) {
data = fromHexString(hexData, 0, hexData.length());
// fromHexString() returns null if the hex string
// is invalid one. It is invalid if the data string
// length is not multiple of 2 or the data string
// contains non-hex characters.
if (data != null) {
this.wasNull = false;
return new ImportBlob(data);
} else {
throw new SQLException(
"An invalid hexadecimal string '" + hexData
+ "' detected in the import file at line "
+ getCurrentLineNumber() + " column "
+ columnIndex, "XIE0N", 20000);
}
} else {
this.wasNull = true;
return null;
}
}
}
@Override
public void setBlobAsBytes(PreparedStatement ps, int paramIndex, @Nullable byte[] content)
throws SQLException {
if (streamAsLob) {
if (content != null) {
ps.setBlob(paramIndex, new ByteArrayInputStream(content), content.length);
}
else {
ps.setBlob(paramIndex, (Blob) null);
}
}
else if (wrapAsLob) {
if (content != null) {
ps.setBlob(paramIndex, new PassThroughBlob(content));
}
else {
ps.setBlob(paramIndex, (Blob) null);
}
}
else {
ps.setBytes(paramIndex, content);
}
if (logger.isDebugEnabled()) {
logger.debug(content != null ? "Set bytes for BLOB with length " + content.length :
"Set BLOB to null");
}
}
private InputStream getInputStream(String resPath, ResultSet rs) throws SQLException, IOException {
if (rs == null) {
return null;
}
Blob blob = rs.getBlob(META_TABLE_CONTENT);
if (blob == null || blob.length() == 0) {
return openPushdown(resPath); // empty bytes is pushdown indicator
} else {
return blob.getBinaryStream();
}
}
/**
* Retrieve a JDBC column value from a ResultSet, using the most appropriate
* value type. The returned value should be a detached value object, not having
* any ties to the active ResultSet: in particular, it should not be a Blob or
* Clob object but rather a byte array or String representation, respectively.
* <p>Uses the {@code getObject(index)} method, but includes additional "hacks"
* to get around Oracle 10g returning a non-standard object for its TIMESTAMP
* datatype and a {@code java.sql.Date} for DATE columns leaving out the
* time portion: These columns will explicitly be extracted as standard
* {@code java.sql.Timestamp} object.
* @param rs is the ResultSet holding the data
* @param index is the column index
* @return the value object
* @throws SQLException if thrown by the JDBC API
* @see java.sql.Blob
* @see java.sql.Clob
* @see java.sql.Timestamp
*/
@Nullable
public static Object getResultSetValue(ResultSet rs, int index) throws SQLException {
Object obj = rs.getObject(index);
String className = null;
if (obj != null) {
className = obj.getClass().getName();
}
if (obj instanceof Blob) {
Blob blob = (Blob) obj;
obj = blob.getBytes(1, (int) blob.length());
}
else if (obj instanceof Clob) {
Clob clob = (Clob) obj;
obj = clob.getSubString(1, (int) clob.length());
}
else if ("oracle.sql.TIMESTAMP".equals(className) || "oracle.sql.TIMESTAMPTZ".equals(className)) {
obj = rs.getTimestamp(index);
}
else if (className != null && className.startsWith("oracle.sql.DATE")) {
String metaDataClassName = rs.getMetaData().getColumnClassName(index);
if ("java.sql.Timestamp".equals(metaDataClassName) || "oracle.sql.TIMESTAMP".equals(metaDataClassName)) {
obj = rs.getTimestamp(index);
}
else {
obj = rs.getDate(index);
}
}
else if (obj instanceof java.sql.Date) {
if ("java.sql.Timestamp".equals(rs.getMetaData().getColumnClassName(index))) {
obj = rs.getTimestamp(index);
}
}
return obj;
}
private static ConcurrentHashMap<Class, Integer> buildJavaClassToJdbcTypeCodeMappings() {
final ConcurrentHashMap<Class, Integer> workMap = new ConcurrentHashMap<>();
// these mappings are the ones outlined specifically in the spec
workMap.put( String.class, Types.VARCHAR );
workMap.put( BigDecimal.class, Types.NUMERIC );
workMap.put( BigInteger.class, Types.NUMERIC );
workMap.put( Boolean.class, Types.BIT );
workMap.put( Short.class, Types.SMALLINT );
workMap.put( Integer.class, Types.INTEGER );
workMap.put( Long.class, Types.BIGINT );
workMap.put( Float.class, Types.REAL );
workMap.put( Double.class, Types.DOUBLE );
workMap.put( byte[].class, Types.LONGVARBINARY );
workMap.put( java.sql.Date.class, Types.DATE );
workMap.put( Time.class, Types.TIME );
workMap.put( Timestamp.class, Types.TIMESTAMP );
workMap.put( Blob.class, Types.BLOB );
workMap.put( Clob.class, Types.CLOB );
workMap.put( Array.class, Types.ARRAY );
workMap.put( Struct.class, Types.STRUCT );
workMap.put( Ref.class, Types.REF );
workMap.put( Class.class, Types.JAVA_OBJECT );
workMap.put( RowId.class, Types.ROWID );
workMap.put( SQLXML.class, Types.SQLXML );
// additional "common sense" registrations
workMap.put( Character.class, Types.CHAR );
workMap.put( char[].class, Types.VARCHAR );
workMap.put( Character[].class, Types.VARCHAR );
workMap.put( Byte[].class, Types.LONGVARBINARY );
workMap.put( java.util.Date.class, Types.TIMESTAMP );
workMap.put( Calendar.class, Types.TIMESTAMP );
return workMap;
}
public <T> T getObject( int parameterIndex, Class<T> type )
throws SQLException
{
// checkForClosedStatement() should be called by all of the
// more specific methods to which we forward this call
if ( type == null )
{
throw mismatchException( "NULL", parameterIndex );
}
Object retval;
if ( String.class.equals( type ) ) { retval = getString( parameterIndex ); }
else if ( BigDecimal.class.equals( type ) ) { retval = getBigDecimal( parameterIndex ); }
else if ( Boolean.class.equals( type ) ) { retval = Boolean.valueOf( getBoolean(parameterIndex ) ); }
else if ( Byte.class.equals( type ) ) { retval = Byte.valueOf( getByte( parameterIndex ) ); }
else if ( Short.class.equals( type ) ) { retval = Short.valueOf( getShort( parameterIndex ) ); }
else if ( Integer.class.equals( type ) ) { retval = Integer.valueOf( getInt( parameterIndex ) ); }
else if ( Long.class.equals( type ) ) { retval = Long.valueOf( getLong( parameterIndex ) ); }
else if ( Float.class.equals( type ) ) { retval = Float.valueOf( getFloat( parameterIndex ) ); }
else if ( Double.class.equals( type ) ) { retval = Double.valueOf( getDouble( parameterIndex ) ); }
else if ( Date.class.equals( type ) ) { retval = getDate( parameterIndex ); }
else if ( Time.class.equals( type ) ) { retval = getTime( parameterIndex ); }
else if ( Timestamp.class.equals( type ) ) { retval = getTimestamp( parameterIndex ); }
else if ( Blob.class.equals( type ) ) { retval = getBlob( parameterIndex ); }
else if ( Clob.class.equals( type ) ) { retval = getClob( parameterIndex ); }
else if ( type.isArray() && type.getComponentType().equals( byte.class ) ) { retval = getBytes( parameterIndex ); }
else { retval = getObject( parameterIndex ); }
if ( wasNull() ) { retval = null; }
if ( (retval == null) || (type.isInstance( retval )) ) { return type.cast( retval ); }
throw mismatchException( type.getName(), parameterIndex );
}
@Override
public Blob createBlob() throws SQLException {
try {
return wrapped.createBlob();
}
catch (SQLException x) {
this.reusable = !pool.checkConnectionFailure(x);
throw x;
}
}
@Override
public Blob getBlob(final String colName) throws SQLException {
try {
return resultSet.getBlob(colName);
} catch (final SQLException e) {
handleException(e);
return null;
}
}
@Override
public void release(Blob blob) {
if ( blobs == null ) {
log.debug( "Request to release Blob, but appears no Blobs have ever been registered" );
return;
}
blobs.remove( blob );
}
public void setBlob(int parameterIndex, Blob x) throws SQLException {
delegate.setBlob(parameterIndex, x);
}
public void updateBlob(String columnName, Blob x) throws SQLException {
this.targetResultSet.updateBlob(columnName, x);
}
@Override
public void setBlob(int i, Blob x) throws SQLException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void updateBlob(String columnLabel, Blob x) {
throw new UnsupportedOperationException();
}
@Override
public void updateBlob(int columnIndex, Blob x) throws SQLException {}
@Override
public Blob createBlob() throws SQLException {
return null;
}
public Blob getBlob(String parameterName) throws SQLException {
return getPhysCs().getBlob(parameterName);
}
@Override
public Blob createBlob() throws SQLException {
return null;
}
/**
* Delegates to {@link ResultSet#getObject(String, Class)} without throwing a {@link AbstractMethodError}.
*
* @param <T>
* See {@link ResultSet#getObject(String, Class)}
* @param resultSet
* See {@link ResultSet#getObject(String, Class)}
* @param columnLabel
* See {@link ResultSet#getObject(String, Class)}
* @param type
* See {@link ResultSet#getObject(String, Class)}
* @return See {@link ResultSet#getObject(String, Class)}
* @throws SQLException
* See {@link ResultSet#getObject(String, Class)}
* @see ResultSet#getObject(int, Class)
*/
@SuppressWarnings("unchecked")
public static <T> T getObject(final ResultSet resultSet, final String columnLabel, final Class<T> type)
throws SQLException {
try {
return resultSet.getObject(columnLabel, type);
} catch (final AbstractMethodError e) {
// Numbers
if (type == Integer.class) {
return (T) Integer.valueOf(resultSet.getInt(columnLabel));
}
if (type == Long.class) {
return (T) Long.valueOf(resultSet.getLong(columnLabel));
}
if (type == Double.class) {
return (T) Double.valueOf(resultSet.getDouble(columnLabel));
}
if (type == Float.class) {
return (T) Float.valueOf(resultSet.getFloat(columnLabel));
}
if (type == Short.class) {
return (T) Short.valueOf(resultSet.getShort(columnLabel));
}
if (type == BigDecimal.class) {
return (T) resultSet.getBigDecimal(columnLabel);
}
if (type == Byte.class) {
return (T) Byte.valueOf(resultSet.getByte(columnLabel));
}
// Dates
if (type == Date.class) {
return (T) resultSet.getDate(columnLabel);
}
if (type == Time.class) {
return (T) resultSet.getTime(columnLabel);
}
if (type == Timestamp.class) {
return (T) resultSet.getTimestamp(columnLabel);
}
// Streams
if (type == InputStream.class) {
return (T) resultSet.getBinaryStream(columnLabel);
}
if (type == Reader.class) {
return (T) resultSet.getCharacterStream(columnLabel);
}
// Other
if (type == Object.class) {
return (T) resultSet.getObject(columnLabel);
}
if (type == Boolean.class) {
return (T) Boolean.valueOf(resultSet.getBoolean(columnLabel));
}
if (type == Array.class) {
return (T) resultSet.getArray(columnLabel);
}
if (type == Blob.class) {
return (T) resultSet.getBlob(columnLabel);
}
if (type == Clob.class) {
return (T) resultSet.getClob(columnLabel);
}
if (type == Ref.class) {
return (T) resultSet.getRef(columnLabel);
}
if (type == RowId.class) {
return (T) resultSet.getRowId(columnLabel);
}
if (type == SQLXML.class) {
return (T) resultSet.getSQLXML(columnLabel);
}
if (type == URL.class) {
return (T) resultSet.getURL(columnLabel);
}
throw new SQLFeatureNotSupportedException(
String.format("resultSet=%s, columnLabel=%s, type=%s", resultSet, columnLabel, type));
}
}
public static EdmProperty mockEdmProperty(final String entityName, final String propertyName) throws EdmException {
EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
if (propertyName.equals(JPATypeMock.PROPERTY_NAME_MINT) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_ENUM) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_MSTRING) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_MDATETIME) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_MBLOB) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_CLOB) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_MCARRAY) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_MC) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_MCHAR) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_MCHARARRAY) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_XMLADAPTER) ||
propertyName.equals(JPATypeMock.JPATypeEmbeddableMock.PROPERTY_NAME_MSHORT) ||
propertyName.equals(JPATypeMock.JPATypeEmbeddableMock2.PROPERTY_NAME_MFLOAT) ||
propertyName.equals(JPATypeMock.JPATypeEmbeddableMock2.PROPERTY_NAME_MUUID) ||
propertyName.equals(JPARelatedTypeMock.PROPERTY_NAME_MLONG) ||
propertyName.equals(JPARelatedTypeMock.PROPERTY_NAME_MBYTE) ||
propertyName.equals(JPARelatedTypeMock.PROPERTY_NAME_MDOUBLE) ||
propertyName.equals(JPARelatedTypeMock.PROPERTY_NAME_MBYTEARRAY) ||
propertyName.equals(JPATypeMock.JPATypeEmbeddableMock.PROPERTY_NAME_MDATE) ||
propertyName.equals(JPATypeMock.JPATypeEmbeddableMock.PROPERTY_NAME_MDATE1) ||
propertyName.equals(JPATypeMock.JPATypeEmbeddableMock.PROPERTY_NAME_MTIME) ||
propertyName.equals(JPATypeMock.JPATypeEmbeddableMock.PROPERTY_NAME_MTIMESTAMP)) {
EdmSimpleType edmType = EasyMock.createMock(EdmSimpleType.class);
EasyMock.expect(edmProperty.getType()).andReturn(edmType).anyTimes();
EasyMock.expect(edmProperty.getFacets()).andReturn(null).anyTimes();
EasyMock.expect(edmType.getKind()).andReturn(EdmTypeKind.SIMPLE).anyTimes();
if (propertyName.equals(JPATypeMock.PROPERTY_NAME_MSTRING) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_ENUM) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_MCARRAY) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_MC) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_MCHAR) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_MCHARARRAY)) {
EasyMock.<Class<?>> expect(edmType.getDefaultType()).andReturn(String.class).anyTimes();
} else if (propertyName.equals(JPATypeMock.PROPERTY_NAME_MBLOB)) {
EasyMock.<Class<?>> expect(edmType.getDefaultType()).andReturn(Blob.class).anyTimes();
} else if (propertyName.equals(JPATypeMock.PROPERTY_NAME_XMLADAPTER)) {
EasyMock.<Class<?>> expect(edmType.getDefaultType())
.andReturn(String.class).anyTimes();
} else {
EasyMock.<Class<?>> expect(edmType.getDefaultType()).andReturn(Integer.class).anyTimes();
}
EasyMock.expect(edmType.isCompatible(EasyMock.isA(EdmSimpleType.class))).andReturn(true).anyTimes();
EasyMock.replay(edmType);
EasyMock.expect(edmProperty.getName()).andReturn(propertyName).anyTimes();
EasyMock.expect(edmProperty.getMapping()).andReturn((EdmMapping) mockEdmMapping(entityName, propertyName, null))
.anyTimes();
} else if (propertyName.equals(JPATypeMock.JPATypeEmbeddableMock.PROPERTY_NAME_MEMBEDDABLE) ||
propertyName.equals(JPATypeMock.PROPERTY_NAME_MCOMPLEXTYPE)) {
EdmComplexType complexType = mockComplexType(propertyName);
EasyMock.expect(edmProperty.getType()).andReturn(complexType).anyTimes();
EasyMock.expect(edmProperty.getName()).andReturn(propertyName).anyTimes();
EasyMock.expect(edmProperty.getMapping()).andReturn((EdmMapping) mockEdmMapping(null, propertyName, null))
.anyTimes();
EasyMock.expect(edmProperty.getFacets()).andReturn(null).anyTimes();
}
EasyMock.replay(edmProperty);
return edmProperty;
}
@Override
public void setBlob(int i, Blob x) throws SQLException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void setBlob(int i, Blob x) throws SQLException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void updateBlob(String columnLabel, Blob x) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public Blob getBlob(String arg0) throws SQLException {
throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
}