Example usage for java.sql ResultSet getCharacterStream

List of usage examples for java.sql ResultSet getCharacterStream

Introduction

In this page you can find the example usage for java.sql ResultSet getCharacterStream.

Prototype

java.io.Reader getCharacterStream(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a java.io.Reader object.

Usage

From source file:com.github.woonsan.jdbc.jcr.impl.JcrJdbcResultSetTest.java

@SuppressWarnings("deprecation")
@Test/*  ww w .  ja v a  2 s  .c  o  m*/
public void testResultSetWhenClosed() throws Exception {
    Statement statement = getConnection().createStatement();
    ResultSet rs = statement.executeQuery(SQL_EMPS);

    rs.close();

    try {
        rs.isBeforeFirst();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.isAfterLast();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.isFirst();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.isLast();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.beforeFirst();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.afterLast();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.first();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.last();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.next();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getRow();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getType();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getConcurrency();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.rowUpdated();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.rowDeleted();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.rowInserted();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getStatement();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.wasNull();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getString(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getString("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBoolean(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBoolean("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getByte(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getByte("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getShort(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getShort("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getInt(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getInt("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getLong(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getLong("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getFloat(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getFloat("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDouble(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDouble("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBigDecimal(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBigDecimal("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBytes(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBytes("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate(1, null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate("col1", null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime(1, null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime("col1", null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp(1, null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp("col1", null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getAsciiStream(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getAsciiStream("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getUnicodeStream(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getUnicodeStream("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBinaryStream(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBinaryStream("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getCharacterStream(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getCharacterStream("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getMetaData();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.setFetchDirection(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getFetchDirection();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.setFetchSize(100);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getFetchSize();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getHoldability();
        fail();
    } catch (SQLException ignore) {
    }

    statement.close();
}

From source file:nz.co.gregs.dbvolution.datatypes.DBByteArray.java

private byte[] getFromCharacterReader(ResultSet resultSet, String fullColumnName)
        throws SQLException, IOException {
    byte[] decodeBuffer = new byte[] {};
    Reader inputReader = null;/*from   w ww. jav  a2 s.  c o m*/
    try {
        inputReader = resultSet.getCharacterStream(fullColumnName);
    } catch (NullPointerException nullEx) {
        ;// NullPointerException is thrown by a SQLite-JDBC bug sometimes.
    }
    if (inputReader != null) {
        if (resultSet.wasNull()) {
            this.setToNull();
        } else {
            BufferedReader input = new BufferedReader(inputReader);
            List<byte[]> byteArrays = new ArrayList<byte[]>();

            int totalBytesRead = 0;
            try {
                char[] resultSetBytes;
                resultSetBytes = new char[100000];
                int bytesRead = input.read(resultSetBytes);
                while (bytesRead > 0) {
                    totalBytesRead += bytesRead;
                    byteArrays.add(String.valueOf(resultSetBytes).getBytes());
                    resultSetBytes = new char[100000];
                    bytesRead = input.read(resultSetBytes);
                }
            } catch (IOException ex) {
                Logger.getLogger(DBByteArray.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                input.close();
            }
            byte[] bytes = new byte[totalBytesRead];
            int bytesAdded = 0;
            for (byte[] someBytes : byteArrays) {
                System.arraycopy(someBytes, 0, bytes, bytesAdded,
                        Math.min(someBytes.length, bytes.length - bytesAdded));
                bytesAdded += someBytes.length;
            }
            decodeBuffer = Base64.decodeBase64(bytes);
            //            this.setValue(decodeBuffer);
        }
    }
    return decodeBuffer;
}

From source file:nz.co.gregs.dbvolution.datatypes.DBJavaObject.java

@SuppressWarnings("unchecked")
private O getFromCharacterReader(ResultSet resultSet, String fullColumnName) throws SQLException, IOException {
    O obj = null;/*ww w  .j  a  va2s. c o  m*/
    Reader inputReader = null;
    try {
        inputReader = resultSet.getCharacterStream(fullColumnName);
    } catch (NullPointerException nullEx) {
        ;// NullPointerException is thrown by a SQLite-JDBC bug sometimes.
    }
    if (inputReader != null) {
        if (resultSet.wasNull()) {
            this.setToNull();
        } else {
            BufferedReader input = new BufferedReader(inputReader);
            try {
                List<byte[]> byteArrays = new ArrayList<byte[]>();

                int totalBytesRead = 0;
                try {
                    char[] resultSetBytes;
                    resultSetBytes = new char[100000];
                    int bytesRead = input.read(resultSetBytes);
                    while (bytesRead > 0) {
                        totalBytesRead += bytesRead;
                        byteArrays.add(String.valueOf(resultSetBytes).getBytes());
                        resultSetBytes = new char[100000];
                        bytesRead = input.read(resultSetBytes);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(DBByteArray.class.getName()).log(Level.SEVERE, null, ex);
                }
                byte[] bytes = new byte[totalBytesRead];
                int bytesAdded = 0;
                for (byte[] someBytes : byteArrays) {
                    System.arraycopy(someBytes, 0, bytes, bytesAdded,
                            Math.min(someBytes.length, bytes.length - bytesAdded));
                    bytesAdded += someBytes.length;
                }
                byte[] decodeBuffer = Base64.decodeBase64(bytes);

                ObjectInputStream decodedInput = new ObjectInputStream(new ByteArrayInputStream(decodeBuffer));
                try {
                    //               this.setValue(decodedInput.readObject());
                    obj = (O) decodedInput.readObject();
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(DBJavaObject.class.getName()).log(Level.SEVERE, null, ex);
                }
            } finally {
                input.close();
            }
        }
    }
    return obj;
}

From source file:org.andromda.persistence.hibernate.usertypes.HibernateStringClobType.java

/**
 * @see org.hibernate.type.NullableType#get(java.sql.ResultSet,
 *      java.lang.String)/*  w  w w.ja  va 2 s  . com*/
 */
public Object get(ResultSet rs, String name) throws SQLException {

    Reader reader = rs.getCharacterStream(name);
    if (reader == null) {
        return null;
    }
    StringBuffer sb = new StringBuffer();
    try {
        char[] charbuf = new char[4096];
        for (int i = reader.read(charbuf); i > 0; i = reader.read(charbuf)) {
            sb.append(charbuf, 0, i);
        }
    } catch (IOException e) {
        throw new SQLException(e.getMessage());
    }
    return sb.toString();
}

From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java

/**
 * Convert the specified column of the SQL ResultSet to the proper
 * java type.// ww w .  j  av  a  2s.  c om
 */
public Reader getCharacterStream(ResultSet rs, int column) throws SQLException {
    return rs.getCharacterStream(column);
}

From source file:org.eclipse.ecr.core.storage.sql.jdbc.dialect.DialectOracle.java

@Override
@SuppressWarnings("boxing")
public Serializable getFromResultSet(ResultSet rs, int index, Column column) throws SQLException {
    switch (column.getJdbcType()) {
    case Types.VARCHAR:
        return getFromResultSetString(rs, index, column);
    case Types.CLOB:
        // Oracle cannot read CLOBs using rs.getString when the ResultSet is
        // a ScrollableResultSet (the standard OracleResultSetImpl works
        // fine).
        Reader r = rs.getCharacterStream(index);
        if (r == null) {
            return null;
        }/*from   w  ww.  j a v  a 2  s . c  om*/
        StringBuilder sb = new StringBuilder();
        try {
            int n;
            char[] buffer = new char[4096];
            while ((n = r.read(buffer)) != -1) {
                sb.append(new String(buffer, 0, n));
            }
        } catch (IOException e) {
            log.error("Cannot read CLOB", e);
        }
        return sb.toString();
    case Types.BIT:
        return rs.getBoolean(index);
    case Types.TINYINT:
    case Types.SMALLINT:
    case Types.INTEGER:
    case Types.BIGINT:
        return rs.getLong(index);
    case Types.DOUBLE:
        return rs.getDouble(index);
    case Types.TIMESTAMP:
        return getFromResultSetTimestamp(rs, index, column);
    }
    throw new SQLException("Unhandled JDBC type: " + column.getJdbcType());
}

From source file:org.geowebcache.storage.jdbc.jobstore.JDBCJobWrapper.java

private String readClob(ResultSet rs, String field) throws SQLException, IOException {
    StringBuilder sb = new StringBuilder();
    Reader reader = rs.getCharacterStream(field);

    while (true) {
        char[] buff = new char[1024];
        int len = reader.read(buff);
        if (len < 0) {
            break;
        } else {//from ww w. j a  v  a  2s  .  c  o  m
            sb.append(buff, 0, len);
        }
    }
    return sb.toString();
}

From source file:org.jbpm.db.hibernate.SybaseTextType.java

public Object get(ResultSet rs, String name) throws HibernateException, SQLException {
    // retrieve the value of the designated column in the current row of the
    // result set as a character reader
    Reader charReader = rs.getCharacterStream(name);

    // if the corresponding SQL value is NULL, the reader we got is NULL as well
    if (charReader == null || rs.wasNull())
        return null;

    // Fetch Reader content up to the end - and put characters in a StringBuffer
    StringBuffer sbuf = new StringBuffer();
    try {/*from   w w  w  .  jav a 2  s. co m*/
        char[] cbuf = new char[1024];
        for (int amountRead; (amountRead = charReader.read(cbuf)) != -1;) {
            sbuf.append(cbuf, 0, amountRead);
        }
    } catch (IOException ioe) {
        throw new HibernateException("IOException occurred reading text", ioe);
    } finally {
        try {
            charReader.close();
        } catch (IOException e) {
            throw new HibernateException("IOException occurred closing stream", e);
        }
    }

    // Return StringBuffer content as a large String
    return sbuf.toString();
}

From source file:org.kawanfw.sql.servlet.sql.ResultSetWriter.java

/**
 * the CLOB content is dumped in a server file that will be available for
 * the client the name of the file will be stored in the output stream ;
 * //from ww  w  . j  a v  a2s .  c  o  m
 * @param resultSet
 *            the result set in progress to send back to the client side
 * @param columnIndex
 *            the column index
 * 
 * @return the formated binary column
 * 
 * @throws SQLException
 */
private String formatClobColumn(ResultSet resultSet, int columnIndex) throws SQLException, IOException {
    String columnValueStr;

    FileNameFromBlobBuilder fileNameFromBlobBuilder = new FileNameFromBlobBuilder(sqlOrder, columnIndex, true);
    String fileName = fileNameFromBlobBuilder.getFileName();

    // Maybe null, we want to keep the info
    Reader reader = resultSet.getCharacterStream(columnIndex);
    BufferedReader br = new BufferedReader(reader);

    if (fileConfigurator == null) // Servlet Awake FILE not
    // configured.
    {
        columnValueStr = TransportConverter.KAWANFW_BYTES_STREAM_FILE
                + TransportConverter.KAWANFW_STREAM_FAILURE;
        return columnValueStr;
    }

    String hostFileName = null;

    hostFileName = HttpConfigurationUtil.addRootPath(fileConfigurator, username, fileName);

    debug("formatClobColumn:writer: " + hostFileName);

    if (reader == null) {
        Writer writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(hostFileName));
            debug("formatClobColumn.reader == null");
            writer.write(TransportConverter.KAWANFW_STREAM_NULL + CR_LF);
        } finally {
            IOUtils.closeQuietly(writer);
        }
    } else {

        writeClobFile(br, hostFileName);

    }

    // The column value is a file name with a tag for identification
    columnValueStr = TransportConverter.KAWANFW_BYTES_STREAM_FILE + fileName;
    return columnValueStr;
}

From source file:org.kawanfw.test.api.client.InsertAndUpdateClobTest.java

/**
 * Test that the blob was were correctly inserted
 * //from  w  w w  . jav a2 s.c  o  m
 * @param connection
 */
public void selectClobTest(Connection connection, String originalFileName, String shaHexa) throws Exception {

    int item_id;

    String sql = "select * from documentation where item_id >= ? ";

    PreparedStatement prepStatement = connection.prepareStatement(sql);

    int i = 1;
    prepStatement.setInt(i++, 1);

    long begin = System.currentTimeMillis();
    ResultSet rs = prepStatement.executeQuery();
    long end = System.currentTimeMillis();
    System.out.println("ResultSet rs = prepStatement.executeQuery() time: " + (end - begin));

    MessageDisplayer.display("");

    Reader reader = null;
    Writer writer = null;

    while (rs.next()) {

        item_id = rs.getInt("item_id");

        File originalFile = SqlTestParms.getFileFromUserHome(originalFileName);
        // String extension = "."
        // + StringUtils.substringAfterLast(originalFile.toString(),
        // ".");

        File file = InsertAndUpdateBlobTest.createTempFile(originalFile.toString());
        try {
            reader = rs.getCharacterStream("item_doc");

            if (reader != null) {
                writer = new BufferedWriter(new FileWriter(file));
                IOUtils.copy(reader, writer);

            } else {
                MessageDisplayer.display("item_doc column is null!");
            }

        } finally {
            IOUtils.closeQuietly(reader);
            IOUtils.closeQuietly(writer);
        }

        i = 1;
        item_id = rs.getInt(i++);

        MessageDisplayer.display("");
        MessageDisplayer.display("item_id     : " + item_id);

        // Compute the hash of the file
        Sha1Util sha1 = new Sha1Util();
        String shaHexaNew = sha1.getHexFileHash(file);

        Assert.assertEquals("Comparing original " + SqlTestParms.TEXT_FILE_1 + " new " + file + " hash values ",
                shaHexa, shaHexaNew);

        file.delete();

        MessageDisplayer.display("");
        MessageDisplayer.display("Ok, SHA-1 value of read file " + file + " is same as inserted file "
                + SqlTestParms.getFileFromUserHome(originalFileName));

    }

    prepStatement.close();
    rs.close();

    MessageDisplayer.display("Select done!");

}