java.sql.Clob.getCharacterStream()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(310)

本文整理了Java中java.sql.Clob.getCharacterStream()方法的一些代码示例,展示了Clob.getCharacterStream()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Clob.getCharacterStream()方法的具体详情如下:
包路径:java.sql.Clob
类名称:Clob
方法名:getCharacterStream

Clob.getCharacterStream介绍

[英]Gets the data of this Clob object in a java.io.Reader.
[中]获取java中此Clob对象的数据。木卫一。读者

代码示例

代码示例来源:origin: com.h2database/h2

private static Reader asReader(Object o) throws SQLException {
  if (o == null) {
    return null;
  } else if (o instanceof Clob) {
    return ((Clob) o).getCharacterStream();
  }
  return (Reader) o;
}

代码示例来源:origin: alibaba/druid

public Reader getClobAsCharacterStream(ResultSet rs, int columnIndex) throws SQLException {
  Clob clob = rs.getClob(columnIndex);
  if (clob == null) {
    return null;
  }
  return clob.getCharacterStream();
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Reader getClobAsCharacterStream(ResultSet rs, int columnIndex) throws SQLException {
  logger.debug("Returning CLOB as character stream");
  if (this.wrapAsLob) {
    Clob clob = rs.getClob(columnIndex);
    return clob.getCharacterStream();
  }
  else {
    return rs.getCharacterStream(columnIndex);
  }
}

代码示例来源:origin: lealone/Lealone

private static Reader asReader(Object o) throws SQLException {
  if (o == null) {
    return null;
  } else if (o instanceof Clob) {
    return ((Clob) o).getCharacterStream();
  }
  return (Reader) o;
}

代码示例来源:origin: nutzam/nutz

public void set(PreparedStatement stat, Object obj, int index) throws SQLException {
  if (null == obj) {
    stat.setNull(index, Types.CLOB);
  } else {
    Clob clob = (Clob) obj;
    Jdbcs.setCharacterStream(index, clob.getCharacterStream(), stat);
  }
}

代码示例来源:origin: jfinal/jfinal

public String handleClob(Clob clob) throws SQLException {
  if (clob == null)
    return null;
  
  Reader reader = null;
  try {
    reader = clob.getCharacterStream();
    if (reader == null)
      return null;
    char[] buffer = new char[(int)clob.length()];
    if (buffer.length == 0)
      return null;
    reader.read(buffer);
    return new String(buffer);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
  finally {
    if (reader != null)
      try {reader.close();} catch (IOException e) {throw new RuntimeException(e);}
  }
}

代码示例来源:origin: alibaba/druid

@Override
public Reader clob_getCharacterStream(ClobProxy clob, long pos, long length) throws SQLException {
  if (this.pos < filterSize) {
    return nextFilter().clob_getCharacterStream(this, clob, pos, length);
  }
  return clob.getRawClob().getCharacterStream(pos, length);
}

代码示例来源:origin: alibaba/druid

@Override
public java.io.Reader clob_getCharacterStream(ClobProxy clob) throws SQLException {
  if (this.pos < filterSize) {
    return nextFilter().clob_getCharacterStream(this, clob);
  }
  return clob.getRawClob().getCharacterStream();
}

代码示例来源:origin: nutzam/nutz

public Object get(ResultSet rs, int columnIndex) throws SQLException {
  Clob clob = rs.getClob(columnIndex);
  if (clob == null)
    return null;
  File f = this.createTempFile();
  Streams.writeAndClose(Streams.fileOutw(f), clob.getCharacterStream());
  return new SimpleClob(f);
}

代码示例来源:origin: nutzam/nutz

public Object get(ResultSet rs, String colName) throws SQLException {
  Clob clob = rs.getClob(colName);
  if (clob == null)
    return null;
  File f = this.createTempFile();
  Streams.writeAndClose(Streams.fileOutw(f), clob.getCharacterStream());
  return new SimpleClob(f);
}

代码示例来源:origin: looly/hutool

/**
 * Clob字段值转字符串
 * 
 * @param clob {@link Clob}
 * @return 字符串
 * @since 3.0.6
 */
public static String clobToStr(Clob clob) {
  Reader reader = null;
  try {
    reader = clob.getCharacterStream();
    return IoUtil.read(reader);
  } catch (SQLException e) {
    throw new DbRuntimeException(e);
  } finally {
    IoUtil.close(reader);
  }
}

代码示例来源:origin: looly/hutool

/**
 * Clob字段值转字符串
 * 
 * @param clob {@link Clob}
 * @return 字符串
 * @since 3.0.6
 */
public static String clobToStr(Clob clob) {
  Reader reader = null;
  try {
    reader = clob.getCharacterStream();
    return IoUtil.read(reader);
  } catch (SQLException e) {
    throw new DbRuntimeException(e);
  } finally {
    IoUtil.close(reader);
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@SuppressWarnings({ "unchecked" })
public <X> X unwrap(final Clob value, Class<X> type, WrapperOptions options) {
  if ( value == null ) {
    return null;
  }
  try {
    if ( CharacterStream.class.isAssignableFrom( type ) ) {
      if ( ClobImplementer.class.isInstance( value ) ) {
        // if the incoming Clob is a wrapper, just pass along its CharacterStream
        return (X) ( (ClobImplementer) value ).getUnderlyingStream();
      }
      else {
        // otherwise we need to build a CharacterStream...
        return (X) new CharacterStreamImpl( DataHelper.extractString( value.getCharacterStream() ) );
      }
    }
    else if (Clob.class.isAssignableFrom( type )) {
      final Clob clob =  WrappedClob.class.isInstance( value )
          ? ( (WrappedClob) value ).getWrappedClob()
          : value;
      return (X) clob;
    }
  }
  catch ( SQLException e ) {
    throw new HibernateException( "Unable to access clob stream", e );
  }
  
  throw unknownUnwrap( type );
}

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Extract the contents of the given Clob as a string.
 *
 * @param value The clob to to be extracted from
 *
 * @return The content as string
 */
public static String extractString(final Clob value) {
  try {
    final Reader characterStream = value.getCharacterStream();
    final long length = determineLengthForBufferSizing( value );
    return length > Integer.MAX_VALUE
        ? extractString( characterStream, Integer.MAX_VALUE )
        : extractString( characterStream, (int) length );
  }
  catch ( SQLException e ) {
    throw new HibernateException( "Unable to access lob stream", e );
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Override
public Clob mergeClob(Clob original, Clob target, SharedSessionContractImplementor session) {
  if ( original == null && target == null ) {
    return null;
  }
  try {
    final LobCreator lobCreator = session.getFactory().getServiceRegistry().getService( JdbcServices.class ).getLobCreator( session );
    return original == null
        ? lobCreator.createClob( "" )
        : lobCreator.createClob( original.getCharacterStream(), original.length() );
  }
  catch (SQLException e) {
    throw session.getFactory().getSQLExceptionHelper().convert( e, "unable to merge CLOB data" );
  }
}

代码示例来源:origin: com.h2database/h2

/**
 * Updates a column in the current or insert row.
 *
 * @param columnIndex (1,2,...)
 * @param x the value
 * @throws SQLException if the result set is closed or not updatable
 */
@Override
public void updateClob(int columnIndex, Clob x) throws SQLException {
  try {
    if (isDebugEnabled()) {
      debugCode("updateClob("+columnIndex+", x);");
    }
    checkClosed();
    Value v;
    if (x == null) {
      v = ValueNull.INSTANCE;
    } else {
      v = conn.createClob(x.getCharacterStream(), -1);
    }
    update(columnIndex, v);
  } catch (Exception e) {
    throw logAndConvert(e);
  }
}

代码示例来源:origin: com.h2database/h2

/**
 * Updates a column in the current or insert row.
 *
 * @param columnLabel the column label
 * @param x the value
 * @throws SQLException if the result set is closed or not updatable
 */
@Override
public void updateClob(String columnLabel, Clob x) throws SQLException {
  try {
    if (isDebugEnabled()) {
      debugCode("updateClob("+quote(columnLabel)+", x);");
    }
    checkClosed();
    Value v;
    if (x == null) {
      v = ValueNull.INSTANCE;
    } else {
      v = conn.createClob(x.getCharacterStream(), -1);
    }
    update(columnLabel, v);
  } catch (Exception e) {
    throw logAndConvert(e);
  }
}

代码示例来源:origin: com.h2database/h2

/**
 * Sets the value of a parameter as a Clob.
 *
 * @param parameterIndex the parameter index (1, 2, ...)
 * @param x the value
 * @throws SQLException if this object is closed
 */
@Override
public void setClob(int parameterIndex, Clob x) throws SQLException {
  try {
    if (isDebugEnabled()) {
      debugCode("setClob("+parameterIndex+", x);");
    }
    checkClosedForWrite();
    try {
      Value v;
      if (x == null) {
        v = ValueNull.INSTANCE;
      } else {
        v = conn.createClob(x.getCharacterStream(), -1);
      }
      setParameter(parameterIndex, v);
    } finally {
      afterWriting();
    }
  } catch (Exception e) {
    throw logAndConvert(e);
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testClob() {
  final int id = doInHibernate( this::sessionFactory, s -> {
    FileClob file = new FileClob();
    file.setClob( s.getLobHelper().createClob( "TEST CASE" ) );
    // merge transient entity
    file = (FileClob) s.merge( file );
    return file.getId();
  } );
  doInHibernate( this::sessionFactory, s -> {
    FileClob file = s.get( FileClob.class, id );
    assertFalse( Hibernate.isPropertyInitialized( file, "clob" ) );
    Clob clob = file.getClob();
    try {
      final char[] chars = new char[(int) file.getClob().length()];
      clob.getCharacterStream().read( chars );
      assertTrue( Arrays.equals( "TEST CASE".toCharArray(), chars ) );
    }
    catch (SQLException ex ) {
      fail( "could not determine Lob length" );
    }
    catch (IOException ex) {
      fail( "could not read Lob" );
    }
  });
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void test() {
  Integer productId = doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::basic-clob-persist-example[]
    String warranty = "My product warranty";
    final Product product = new Product();
    product.setId( 1 );
    product.setName( "Mobile phone" );
    product.setWarranty( ClobProxy.generateProxy( warranty ) );
    entityManager.persist( product );
    //end::basic-clob-persist-example[]
    return product.getId();
  } );
  doInJPA( this::entityManagerFactory, entityManager -> {
    try {
      //tag::basic-clob-find-example[]
      Product product = entityManager.find( Product.class, productId );
      try (Reader reader = product.getWarranty().getCharacterStream()) {
        assertEquals( "My product warranty", toString( reader ) );
      }
      //end::basic-clob-find-example[]
    }
    catch (Exception e) {
      fail( e.getMessage() );
    }
  } );
}

相关文章