Example usage for java.sql PreparedStatement setClob

List of usage examples for java.sql PreparedStatement setClob

Introduction

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

Prototype

void setClob(int parameterIndex, Reader reader) throws SQLException;

Source Link

Document

Sets the designated parameter to a Reader object.

Usage

From source file:DemoPreparedStatementSetClob.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    String newID = "0002";

    ResultSet rs = null;/*  w  ww. jav  a 2 s .  c  om*/
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        // begin transaction
        conn.setAutoCommit(false);
        String query1 = "select clob_column from clob_table where id = ?";
        pstmt = conn.prepareStatement(query1);
        pstmt.setString(1, id);
        rs = pstmt.executeQuery();
        rs.next();
        java.sql.Clob clob = (java.sql.Clob) rs.getObject(1);
        String query = "insert into clob_table(id, clob_column) values(?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, newID);
        pstmt.setClob(2, clob);

        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
        conn.commit();
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}

From source file:DemoPreparedStatementSetClob.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    String newID = "0002";

    ResultSet rs = null;//from   w  w  w  .  j  a v a2 s . c  om
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        // begin transaction
        conn.setAutoCommit(false);
        String query1 = "select clob_column from clob_table where id = ?";
        pstmt = conn.prepareStatement(query1);
        pstmt.setString(1, id);
        rs = pstmt.executeQuery();
        rs.next();
        java.sql.Clob clob = (java.sql.Clob) rs.getObject(1);
        String query = "insert into clob_table(id, clob_column) values(?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, newID);
        pstmt.setClob(2, clob);

        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
        // end transaction
        conn.commit();
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}

From source file:org.springframework.jdbc.support.lob.TemporaryLobCreator.java

@Override
public void setClobAsString(PreparedStatement ps, int paramIndex, @Nullable String content)
        throws SQLException {

    if (content != null) {
        Clob clob = ps.getConnection().createClob();
        clob.setString(1, content);// ww  w . ja v  a2 s .  c o  m
        this.temporaryClobs.add(clob);
        ps.setClob(paramIndex, clob);
    } else {
        ps.setClob(paramIndex, (Clob) null);
    }

    if (logger.isDebugEnabled()) {
        logger.debug(content != null ? "Copied string into temporary CLOB with length " + content.length()
                : "Set CLOB to null");
    }
}

From source file:org.springframework.jdbc.support.lob.TemporaryLobCreator.java

@Override
public void setClobAsAsciiStream(PreparedStatement ps, int paramIndex, @Nullable InputStream asciiStream,
        int contentLength) throws SQLException {

    if (asciiStream != null) {
        Clob clob = ps.getConnection().createClob();
        try {/*from  w  w  w  .j av  a  2 s.  co m*/
            FileCopyUtils.copy(asciiStream, clob.setAsciiStream(1));
        } catch (IOException ex) {
            throw new DataAccessResourceFailureException("Could not copy into LOB stream", ex);
        }
        this.temporaryClobs.add(clob);
        ps.setClob(paramIndex, clob);
    } else {
        ps.setClob(paramIndex, (Clob) null);
    }

    if (logger.isDebugEnabled()) {
        logger.debug(
                asciiStream != null ? "Copied ASCII stream into temporary CLOB with length " + contentLength
                        : "Set CLOB to null");
    }
}

From source file:org.springframework.jdbc.support.lob.TemporaryLobCreator.java

@Override
public void setClobAsCharacterStream(PreparedStatement ps, int paramIndex, @Nullable Reader characterStream,
        int contentLength) throws SQLException {

    if (characterStream != null) {
        Clob clob = ps.getConnection().createClob();
        try {//  ww w .  ja  v a 2  s . co m
            FileCopyUtils.copy(characterStream, clob.setCharacterStream(1));
        } catch (IOException ex) {
            throw new DataAccessResourceFailureException("Could not copy into LOB stream", ex);
        }
        this.temporaryClobs.add(clob);
        ps.setClob(paramIndex, clob);
    } else {
        ps.setClob(paramIndex, (Clob) null);
    }

    if (logger.isDebugEnabled()) {
        logger.debug(characterStream != null
                ? "Copied character stream into temporary CLOB with length " + contentLength
                : "Set CLOB to null");
    }
}

From source file:data.DefaultExchanger.java

protected void setClob(PreparedStatement ps, short index, JsonNode node, String column) throws SQLException {
    String value = node.get(column).textValue();
    if (value == null) {
        ps.setNull(index, Types.CLOB);
    } else {// www. j  a  va 2s.  c  om
        Clob clob = ps.getConnection().createClob();
        clob.setString(1, value);
        ps.setClob(index, clob);
    }
}

From source file:org.apache.eagle.alert.metadata.impl.JdbcMetadataHandler.java

private OpResult executeUpdate(Connection connection, String query, String key, String value)
        throws SQLException {
    OpResult result = new OpResult();
    PreparedStatement statement = null;
    try {//from   w ww  .j a v  a 2  s. co  m
        statement = connection.prepareStatement(query);
        Clob clob = connection.createClob();
        clob.setString(1, value);
        statement.setClob(1, clob);
        statement.setString(2, key);
        int status = statement.executeUpdate();
        LOG.info("update {} with query={}", status, query);
    } finally {
        if (statement != null) {
            statement.close();
        }
    }
    return result;
}

From source file:com.amazon.carbonado.repo.jdbc.OracleSupportStrategy.java

/**
 * @return original clob if too large and post-insert update is required, null otherwise
 *//*from w  w w .j ava2 s  .c  o m*/
@Override
com.amazon.carbonado.lob.Clob setClobValue(PreparedStatement ps, int column, com.amazon.carbonado.lob.Clob clob)
        throws PersistException {
    try {
        long length = clob.getLength();
        if (length > CLOB_CHUNK_LIMIT || ((long) ((int) length)) != length) {
            if (mCLOB_empty_lob == null) {
                return super.setClobValue(ps, column, clob);
            }

            try {
                ps.setClob(column, (java.sql.Clob) mCLOB_empty_lob.invoke(null));
                return clob;
            } catch (InvocationTargetException e) {
                throw mRepo.toPersistException(e.getCause());
            } catch (Exception e) {
                throw mRepo.toPersistException(e);
            }
        }

        if (clob instanceof OracleClob) {
            ps.setClob(column, ((OracleClob) clob).getInternalClobForPersist());
            return null;
        }

        ps.setCharacterStream(column, clob.openReader(), (int) length);
        return null;
    } catch (SQLException e) {
        throw mRepo.toPersistException(e);
    } catch (FetchException e) {
        throw e.toPersistException();
    }
}

From source file:org.kawanfw.test.api.client.InsertAndUpdateClobTestNew.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);

    Clob clob = connection.createClob();

    Writer writer = null;//ww  w.j  av a 2  s  . co  m
    Reader reader = null;

    try {
        reader = new BufferedReader(new FileReader(file));
        writer = clob.setCharacterStream(1);

        IOUtils.copy(reader, writer);

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

        prepStatement.executeUpdate();
        // Close and free are important to delete temp files
        prepStatement.close();
        clob.free();
    } finally {
        IOUtils.closeQuietly(reader);
        IOUtils.closeQuietly(writer);
    }

}

From source file:com.oracle.tutorial.jdbc.ClobSample.java

public void addRowToCoffeeDescriptions(String coffeeName, String fileName) throws SQLException {
    PreparedStatement pstmt = null;
    try {//from   ww  w. jav a  2s  . c om
        Clob myClob = this.con.createClob();

        Writer clobWriter = myClob.setCharacterStream(1);
        String str = this.readFile(fileName, clobWriter);
        System.out.println("Wrote the following: " + clobWriter.toString());
        if (this.settings.dbms.equals("mysql")) {
            System.out.println("MySQL, setting String in Clob object with setString method");
            myClob.setString(1, str);
        }
        System.out.println("Length of Clob: " + myClob.length());
        String sql = "INSERT INTO COFFEE_DESCRIPTIONS VALUES(?,?)";
        pstmt = this.con.prepareStatement(sql);
        pstmt.setString(1, coffeeName);
        pstmt.setClob(2, myClob);
        pstmt.executeUpdate();
    } catch (SQLException sqlex) {
        JDBCTutorialUtilities.printSQLException(sqlex);
    } catch (Exception ex) {
        System.out.println("Unexpected exception: " + ex.toString());
    } finally {
        if (pstmt != null) {
            pstmt.close();
        }
    }
}