Example usage for java.sql Clob free

List of usage examples for java.sql Clob free

Introduction

In this page you can find the example usage for java.sql Clob free.

Prototype

void free() throws SQLException;

Source Link

Document

This method releases the resources that the Clob object holds.

Usage

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

/**
 * Test that the blob was were correctly inserted
 * /* ww w  .j  a  v a 2 s .  c  om*/
 * @param connection
 */
public void selectClobTestAlternateSyntax(Connection connection, String originalFileName, String shaHexa)
        throws Exception {

    int item_id;
    Clob clob;

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

    PreparedStatement prepStatement = connection.prepareStatement(sql);

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

    ResultSet rs = prepStatement.executeQuery();

    MessageDisplayer.display("");

    Reader reader = null;
    Writer writer = null;

    while (rs.next()) {

        item_id = rs.getInt("item_id");

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

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

        File file = InsertAndUpdateBlobTest.createTempFile(originalFile.toString());

        try {
            reader = clob.getCharacterStream();

            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);

            try {
                clob.free();
            } catch (Throwable e) {
                MessageDisplayer.display("clob.free() not done: " + e.toString());
            }
        }

        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(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!");

}

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

/**
 * Test that the blob was were correctly inserted
 * /*from w ww.  j a v a2s .  com*/
 * @param connection
 */
public void selectClobTestAlternateSyntax(Connection connection, String originalFileName, String shaHexa)
        throws Exception {

    int item_id;
    Clob clob;

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

    PreparedStatement prepStatement = connection.prepareStatement(sql);

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

    ResultSet rs = prepStatement.executeQuery();

    MessageDisplayer.display("");

    InputStream in = null;
    Writer writer = null;

    while (rs.next()) {

        item_id = rs.getInt("item_id");

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

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

        File file = InsertAndUpdateBlobTest.createTempFile(originalFile.toString());

        try {
            in = clob.getAsciiStream();

            if (in != null) {
                Reader reader = new InputStreamReader(in);
                writer = new BufferedWriter(new FileWriter(file));
                IOUtils.copy(reader, writer);
            } else {
                MessageDisplayer.display("item_doc column is null!");
            }

        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(writer);
            try {
                clob.free();
            } catch (Throwable e) {
                MessageDisplayer.display("clob.free() not done: " + e.toString());
            }
        }

        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(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!");

}