Example usage for java.sql PreparedStatement setBinaryStream

List of usage examples for java.sql PreparedStatement setBinaryStream

Introduction

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

Prototype

void setBinaryStream(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.apache.sandesha2.storage.jdbc.PersistentStorageManager.java

synchronized public void storeMessageContext(String key, MessageContext msgContext)
        throws SandeshaStorageException {
    if (log.isDebugEnabled())
        log.debug("Enter storeMessageContext for key " + key + " context " + msgContext);
    storageMap.put(key, msgContext);/*from  w  w  w .j a  v  a 2 s  . com*/
    try {
        PreparedStatement pstmt = getDbConnection()
                .prepareStatement("insert into wsrm_msgctx(ctx_key,ctx)values(?,?)");
        pstmt.setString(1, key);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        msgContext.writeExternal(oos);
        oos.close();
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        pstmt.setBinaryStream(2, bais, bais.available());
        pstmt.execute();
        pstmt.close();
    } catch (Exception ex) {
        throw new SandeshaStorageException(ex);
    }
}

From source file:org.codehaus.wadi.core.store.DatabaseStore.java

public void insert(Motable motable) throws Exception {
    Connection conn = null;//from  w w w . j a va2s . co  m
    PreparedStatement ps = null;
    try {
        conn = getConnection();
        ps = conn.prepareStatement(insertMotableSQL);
        int i = 1;
        Object id = motable.getId();
        ps.setString(i++, transformIdToString(id));
        ps.setLong(i++, motable.getCreationTime());
        ps.setLong(i++, motable.getLastAccessedTime());
        ps.setInt(i++, motable.getMaxInactiveInterval());
        byte[] body = motable.getBodyAsByteArray();
        ps.setBinaryStream(i++, new ByteArrayInputStream(body), body.length);
        ps.executeUpdate();
        if (log.isTraceEnabled()) {
            log.trace("Motable [" + id + "] has been inserted");
        }
    } catch (SQLException e) {
        log.warn("See nested", e);
        throw e;
    } finally {
        closeStatement(ps);
        closeConnection(conn);
    }
}

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

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

    String sql = "update orderlog set " + "   jpeg_image  = ? "
            + "     where  customer_id >= ? and item_id >= ?";

    prepStatement = connection.prepareStatement(sql);

    InputStream in = null;/*  w w  w .j  a v  a  2 s.  c  o m*/

    try {
        in = new BufferedInputStream(new FileInputStream(blobFile));

        int i = 1;
        prepStatement.setBinaryStream(i++, in, (int) blobFile.length());
        prepStatement.setInt(i++, 1);
        prepStatement.setInt(i++, 1);

        prepStatement.executeUpdate();
        prepStatement.close(); // important to delete temp files
    } finally {
        IOUtils.closeQuietly(in);
    }

}

From source file:edu.clemson.cs.nestbed.server.adaptation.sql.ProgramMessageSymbolSqlAdapter.java

public ProgramMessageSymbol addProgramMessageSymbol(int programID, String name, byte[] bytecode)
        throws AdaptationException {
    ProgramMessageSymbol programMessageSymbol = null;
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    Statement statement = null;/*w  w  w . j  a v a  2  s . c o m*/
    ResultSet resultSet = null;
    InputStream stream = new ByteArrayInputStream(bytecode);
    try {
        String query = "INSERT INTO ProgramMessageSymbols(programID, name, " + "bytecode) VALUES ( ?, ?, ? )";

        connection = DriverManager.getConnection(CONN_STR);
        preparedStatement = connection.prepareStatement(query);

        preparedStatement.setInt(1, programID);
        preparedStatement.setString(2, name);
        preparedStatement.setBinaryStream(3, stream, bytecode.length);

        log.info("INSERT INTO ProgramMessageSymbols(programID, name, " + "bytecode) VALUES (" + programID
                + ", '" + name + "', " + "<bytecode>)");
        preparedStatement.executeUpdate();

        statement = connection.createStatement();
        query = "SELECT * FROM ProgramMessageSymbols WHERE " + "programID =  " + programID + " AND "
                + "name      = '" + name + "'";
        resultSet = statement.executeQuery(query);

        if (!resultSet.next()) {
            connection.rollback();
            String msg = "Attempt to add program message symbol failed.";
            log.error(msg);
            ;
            throw new AdaptationException(msg);
        }

        programMessageSymbol = getProgramMessageSymbol(resultSet);
        connection.commit();
    } catch (SQLException ex) {
        try {
            connection.rollback();
        } catch (Exception e) {
        }

        String msg = "SQLException in addProgramMessageSymbol";
        log.error(msg, ex);
        throw new AdaptationException(msg, ex);
    } finally {
        try {
            resultSet.close();
        } catch (Exception ex) {
        }
        try {
            preparedStatement.close();
        } catch (Exception ex) {
        }
        try {
            statement.close();
        } catch (Exception ex) {
        }
        try {
            connection.close();
        } catch (Exception ex) {
        }
    }

    return programMessageSymbol;
}

From source file:com.npstrandberg.simplemq.MessageQueueImp.java

public boolean send(MessageInput messageInput) {
    if (messageInput == null) {
        throw new NullPointerException("The messageInput cannot be 'null'");
    }//w w w  .j a  va 2s  .  co m

    byte[] b = null;
    if (messageInput.getObject() != null) {
        b = Utils.serialize(messageInput.getObject());

        // Check if the byte array  can fit in the 'object' column.
        // the 'object' column is a BIGINT and has the same max size as Integer.MAX_VALUE
        if (b.length > Integer.MAX_VALUE) {
            throw new IllegalArgumentException(
                    "The Object is to large, it can only be " + Integer.MAX_VALUE + " bytes.");
        }
    }

    try {
        PreparedStatement ps;
        if (b != null) {
            ps = conn.prepareStatement("INSERT INTO message (body, time, read, object) VALUES(?, ?, ?, ?)");
            ps.setBinaryStream(4, new ByteArrayInputStream(b), b.length);
        } else {
            ps = conn.prepareStatement("INSERT INTO message (body, time, read) VALUES(?, ?, ?)");
        }
        ps.setString(1, messageInput.getBody());
        ps.setLong(2, System.nanoTime());
        ps.setBoolean(3, false);
        ps.executeUpdate();
        ps.close();

        return true;
    } catch (SQLException e) {
        logger.error(e);
    }

    return false;
}

From source file:org.apache.sandesha2.storage.jdbc.PersistentStorageManager.java

synchronized public void updateMessageContext(String key, MessageContext msgContext)
        throws SandeshaStorageException {
    if (log.isDebugEnabled())
        log.debug("updateMessageContext key : " + key);
    storageMap.put(key, msgContext);/* w  w w .  j ava2  s. c  o m*/
    PreparedStatement pstmt = null;
    try {
        pstmt = getDbConnection()
                .prepareStatement("update wsrm_msgctx set ctx=?" + "where ctx_key='" + key + "'");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        msgContext.writeExternal(oos);
        oos.close();
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        pstmt.setBinaryStream(1, bais, bais.available());
        pstmt.executeQuery();
        pstmt.close();
    } catch (Exception ex) {
        throw new SandeshaStorageException("Exception in updateMessageContext", ex);
    }
}

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

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

    String sql = "update orderlog_2 set " + "   jpeg_image  = ? "
            + "     where  customer_id >= ? and item_id >= ?";

    prepStatement = connection.prepareStatement(sql);

    InputStream in = null;/*from w  ww.ja  v a  2 s  .  c  o  m*/

    try {
        in = new BufferedInputStream(new FileInputStream(blobFile));

        int i = 1;
        prepStatement.setBinaryStream(i++, in, (int) blobFile.length());
        prepStatement.setInt(i++, 1);
        prepStatement.setInt(i++, 1);

        prepStatement.executeUpdate();
        prepStatement.close(); // important to delete temp files
    } finally {
        IOUtils.closeQuietly(in);
    }

}

From source file:org.apache.ddlutils.platform.sybase.SybasePlatform.java

/**
  * {@inheritDoc}//from ww  w  .ja  v  a  2s.c o  m
  */
protected void setStatementParameterValue(PreparedStatement statement, int sqlIndex, int typeCode, Object value)
        throws SQLException {
    if ((typeCode == Types.BLOB) || (typeCode == Types.LONGVARBINARY)) {
        // jConnect doesn't like the BLOB type, but works without problems with LONGVARBINARY
        // even when using the Blob class
        if (value instanceof byte[]) {
            byte[] data = (byte[]) value;

            statement.setBinaryStream(sqlIndex, new ByteArrayInputStream(data), data.length);
        } else {
            // Sybase doesn't like the BLOB type, but works without problems with LONGVARBINARY
            // even when using the Blob class
            super.setStatementParameterValue(statement, sqlIndex, Types.LONGVARBINARY, value);
        }
    } else if (typeCode == Types.CLOB) {
        // Same for CLOB and LONGVARCHAR
        super.setStatementParameterValue(statement, sqlIndex, Types.LONGVARCHAR, value);
    } else {
        super.setStatementParameterValue(statement, sqlIndex, typeCode, value);
    }
}

From source file:capture.MySQLDatabase.java

public void storeFile(String urlid, String fileName) {
    Connection con = this.getConnection();
    Statement stmt;//  w  w w .ja  v a2s .co m
    ResultSet rs;

    try {
        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file);
        PreparedStatement ps = con
                .prepareStatement("INSERT INTO file(url_id, operation_id,  filename, content) VALUES (" + urlid
                        + ", " + getCurrentOperation() + ", ?, ?)");
        ps.setString(1, fileName.substring(4));
        ps.setBinaryStream(2, fis, (int) file.length());
        ps.executeUpdate();
        ps.close();
        fis.close();
        con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.wso2.carbon.identity.application.authentication.framework.store.SessionDataStore.java

private void setBlobObject(PreparedStatement prepStmt, Object value, int index)
        throws SQLException, IOException {
    if (value != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(value);// www  .  j a  va  2  s.co m
        oos.flush();
        oos.close();
        InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
        prepStmt.setBinaryStream(index, inputStream, inputStream.available());
    } else {
        prepStmt.setBinaryStream(index, null, 0);
    }
}