Example usage for java.sql PreparedStatement setAsciiStream

List of usage examples for java.sql PreparedStatement setAsciiStream

Introduction

In this page you can find the example usage for java.sql PreparedStatement setAsciiStream.

Prototype

void setAsciiStream(int parameterIndex, java.io.InputStream x, long length) throws SQLException;

Source Link

Document

Sets the designated parameter to the given input stream, which will have the specified number of bytes.

Usage

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

/**
 * Sets the ASCII stream using the underlying Clob file uploaded by the
 * client side/*from   w  w  w  .  j  a v  a2s  .c o  m*/
 * 
 * @param preparedStatement
 *            The Prepared Statement to execute
 * @param parameterIndex
 *            the parameter index
 * @param paramValue
 *            the parameter value (the file name)
 * @throws SQLException
 */
private void setAsciiStream(PreparedStatement preparedStatement, int parameterIndex, String paramValue)
        throws SQLException, IOException {

    // Extract the Clob file from the parameter
    File clobFile = getFileFromParameter(paramValue);

    InputStream inAsciiStream = null;
    long theLength = -1;

    if (callableStatementHolder.isHtmlEncodingOn()) {
        File clobFileHtmlDecoded = new File(clobFile + HTML_DECODED);
        blobsOrClobs.add(clobFileHtmlDecoded);

        BufferedReader br = null;
        Writer writer = null;

        try {
            br = new BufferedReader(new FileReader(clobFile));
            writer = new BufferedWriter(new FileWriter(clobFileHtmlDecoded));
            String line = null;
            while ((line = br.readLine()) != null) {
                line = HtmlConverter.fromHtml(line);
                writer.write(line + CR_LF);
            }

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

            if (!KeepTempFilePolicyParms.KEEP_TEMP_FILE && !DEBUG) {
                clobFile.delete();
            }

        }

        inAsciiStream = new BufferedInputStream(new FileInputStream(clobFileHtmlDecoded));
        theLength = clobFileHtmlDecoded.length();

    } else {
        blobsOrClobs.add(clobFile);
        inAsciiStream = new BufferedInputStream(new FileInputStream(clobFile));
        theLength = clobFile.length();

    }

    // We cast theLength, because the long version may not be implemented by
    // the driver
    preparedStatement.setAsciiStream(parameterIndex, inAsciiStream, (int) theLength);
    this.inList.add(inAsciiStream);
}

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

/**
 * Sets the ASCII stream using the underlying Clob file uploaded by the
 * client side// ww w .j  av a2  s.c o m
 * 
 * @param preparedStatement
 *            The Prepared Statement to execute
 * @param parameterIndex
 *            the parameter index
 * @param paramValue
 *            the parameter value (the file name)
 * @throws SQLException
 */
private void setAsciiStream(PreparedStatement preparedStatement, int parameterIndex, String paramValue)
        throws SQLException, IOException {

    // Extract the Clob file from the parameter
    File clobFile = getFileFromParameter(paramValue);

    InputStream inAsciiStream = null;
    long theLength = -1;

    if (statementHolder.isHtmlEncodingOn()) {
        File clobFileHtmlDecoded = new File(clobFile + HTML_DECODED);
        blobsOrClobs.add(clobFileHtmlDecoded);

        BufferedReader br = null;
        Writer writer = null;

        try {
            br = new BufferedReader(new FileReader(clobFile));
            writer = new BufferedWriter(new FileWriter(clobFileHtmlDecoded));
            String line = null;
            while ((line = br.readLine()) != null) {
                line = HtmlConverter.fromHtml(line);
                writer.write(line + CR_LF);
            }

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

            if (!KeepTempFilePolicyParms.KEEP_TEMP_FILE && !DEBUG) {
                clobFile.delete();
            }

        }

        inAsciiStream = new BufferedInputStream(new FileInputStream(clobFileHtmlDecoded));
        theLength = clobFileHtmlDecoded.length();

    } else {
        blobsOrClobs.add(clobFile);
        inAsciiStream = new BufferedInputStream(new FileInputStream(clobFile));
        theLength = clobFile.length();

    }

    // We cast theLength, because the long version may not be implemented by
    // the driver
    preparedStatement.setAsciiStream(parameterIndex, inAsciiStream, (int) theLength);
    this.inList.add(inAsciiStream);
}

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

/**
 * Insert a CLOB/*from w w  w  .jav  a 2s . com*/
 * 
 * @throws Exception
 *             it any Exception occurs
 */
public void insertLoopPrepStatement(Connection connection, int numberToInsert, File file) throws Exception {
    // We can now use our Remote JDBC Connection as a regular Connection!
    connection.setAutoCommit(false);

    // We will do all our remote insert in a SQL Transaction
    try {
        String sql = "insert into documentation values ( ?, ? )";

        // Create a new Prepared Statement
        PreparedStatement prepStatement = null;

        MessageDisplayer.display("");
        MessageDisplayer.display("Inserting " + numberToInsert + " documentation...");

        for (int customerId = 1; customerId < numberToInsert + 1; customerId++) {
            int i = 1;

            // We will insert a Blob (the image of the product).
            // The transfer will be done in streaming both on the client
            // and on the Servlet Server: we can upload/download very big
            // files.

            InputStream in = new BufferedInputStream(new FileInputStream(file));

            prepStatement = connection.prepareStatement(sql);

            prepStatement.setInt(i++, customerId);
            prepStatement.setAsciiStream(i++, in, (int) file.length());

            prepStatement.executeUpdate();
            prepStatement.close();
        }

        // We do either everything in a single transaction or nothing
        connection.commit(); // Commit is propagated on Server
        MessageDisplayer.display("Remote Commit Done on AceQL Server!");
    } catch (Exception e) {
        connection.rollback();
        throw e;
    } finally {
        connection.setAutoCommit(true);
    }

}

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

public void updateclob(Connection connection, File file) throws Exception {
    PreparedStatement prepStatement = null;

    String sql = "update documentation set " + " item_doc  = ? " + "     where  item_id >= ?";

    prepStatement = connection.prepareStatement(sql);

    InputStream in = new BufferedInputStream(new FileInputStream(file));

    int i = 1;//from ww  w.  ja va2s . c om
    prepStatement.setAsciiStream(i++, in, (int) file.length());
    prepStatement.setInt(i++, 1);

    // System.err.println("file: " + FileUtils.readFileToString(file));

    prepStatement.executeUpdate();
    prepStatement.close(); // important to delete temp files

}