Package org.jhighfun.util.sql

Source Code of org.jhighfun.util.sql.ResultSetRow

package org.jhighfun.util.sql;

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

import org.jhighfun.util.stream.io.InputStreamChain;
import org.jhighfun.util.stream.io.ReaderChain;

public final class ResultSetRow {

  private final ResultSet resultSet;

  public ResultSetRow(ResultSet resultSet) {
    this.resultSet = resultSet;
  }

  public <T> T getColumnValueByName(String columnName, Class<T> type) {
    try {
      return (T) this.resultSet.getObject(columnName);
    } catch (SQLException e) {
      e.printStackTrace();
      throw new RuntimeException("Exception while accessing result row set invalid column name", e);
    }
  }

  public byte[] getBlob(String columnName) {
    try {
      Blob blob = this.resultSet.getBlob(columnName);
      return new InputStreamChain(blob.getBinaryStream()).readBytes();
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException("Exception while reading Blob from database.", e);
    }
  }

  public char[] getClob(String columnName) {
    try {
      Clob clob = this.resultSet.getClob(columnName);
      return new ReaderChain(clob.getCharacterStream()).read();
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException("Exception while reading Clob from database.", e);
    }
  }
}
TOP

Related Classes of org.jhighfun.util.sql.ResultSetRow

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.