Example usage for java.sql PreparedStatement setBlob

List of usage examples for java.sql PreparedStatement setBlob

Introduction

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

Prototype

void setBlob(int parameterIndex, InputStream inputStream) throws SQLException;

Source Link

Document

Sets the designated parameter to a InputStream object.

Usage

From source file:de.unidue.inf.is.ezdl.dlservices.repository.store.repositories.DBRepository.java

@Override
protected void putAsIs(String oid, StoredDocument document) {
    Connection con = null;/*from   www .  ja v a 2 s .  com*/
    PreparedStatement st = null;
    PreparedStatement st2 = null;

    byte[] encoded = encode(document);
    ByteArrayInputStream is = new ByteArrayInputStream(encoded);

    String databaseIdForOid = databaseIdForOid(oid);

    try {
        con = provider.connection();

        st = con.prepareStatement(EXISTS);
        st.setString(1, databaseIdForOid);
        ResultSet rs = st.executeQuery();
        if (rs.next()) {
            String s = rs.getString(1);
            if (Boolean.valueOf(s)) {
                st2 = con.prepareStatement(UPDATE);
                st2.setBlob(1, is);
                st2.setString(2, databaseIdForOid);
                st2.execute();
            } else {
                getLogger().error("Error while checking if row exists");
            }
        } else {
            st2 = con.prepareStatement(PUT);
            st2.setString(1, databaseIdForOid);
            st2.setBlob(2, is);
            st2.execute();
        }
        con.commit();
    } catch (SQLException e) {
        rollback(con);
        getLogger().error("Error putting " + databaseIdForOid, e);
    } finally {
        ClosingUtils.close(st, st2);
        ClosingUtils.close(con);
    }
}

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

    Blob blob = connection.createBlob();

    InputStream in = null;//from w  ww.ja v a2  s  .  c o  m
    OutputStream out = null;

    try {
        in = new BufferedInputStream(new FileInputStream(blobFile));
        out = blob.setBinaryStream(1);
        IOUtils.copy(in, out);

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

        prepStatement.executeUpdate();

        // Close and free are important to delete temp files
        prepStatement.close();
        blob.free();
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }

}

From source file:org.xenei.bloomgraph.bloom.sql.MySQLCommands.java

@Override
public PreparedStatement tripleSearch(final Connection connection, final int pageId,
        final PageSearchItem candidate) throws SQLException, IOException {
    String sql = null;//from  w  ww . j  av a  2s.  co m
    PreparedStatement stmt = null;
    try {
        if (candidate.getSerializable().containsWild()) {
            sql = String.format(
                    "SELECT data, idx FROM Page_%s WHERE hamming>=? AND log>=? AND bloommatch( ?, bloom)",
                    pageId);
            stmt = connection.prepareStatement(sql);
            stmt.setInt(1, candidate.getTripleFilter().getHammingWeight());
            stmt.setDouble(2, candidate.getTripleFilter().getApproximateLog(APPROX_LOG_DEPTH));
            stmt.setBlob(3, DBIO.asInputStream(candidate.getTripleFilter().getByteBuffer()));
        } else {
            sql = String.format("SELECT data, idx FROM Page_%s WHERE hash=?", pageId);
            stmt = connection.prepareStatement(sql);
            stmt.setInt(1, candidate.getTriple().hashCode());
        }
        LOG.debug("Checking {} with {}", candidate.getTriple(), stmt);
        return stmt;
    } catch (final SQLException e) {
        DbUtils.closeQuietly(stmt);
        throw e;
    } catch (final IOException e) {
        DbUtils.closeQuietly(stmt);
        throw e;
    }
}

From source file:org.wso2.carbon.identity.certificateauthority.dao.CertificateDAO.java

/**
 * adds a public certificate to the database
 *
 * @param serial   serial number of the certificate
 * @param tenantID id of the tenant tenant who issued the certificate
 * @return/*from  w  w w .  ja  v  a2  s  . c  om*/
 */
public void addCertificate(String serial, X509Certificate certificate, int tenantID, String username,
        String userStoreDomain) throws CaException {
    Connection connection = null;
    Date requestDate = new Date();
    String sql = null;
    PreparedStatement prepStmt = null;
    try {
        Date expiryDate = certificate.getNotAfter();

        log.debug("adding public certificate file to database");
        connection = JDBCPersistenceManager.getInstance().getDBConnection();
        sql = "INSERT INTO CA_CERTIFICATE_STORE (SERIAL_NO,PUBLIC_CERTIFICATE,STATUS,ISSUED_DATE,EXPIRY_DATE,TENANT_ID,USER_NAME,UM_DOMAIN_NAME) VALUES (?,?,?,?,?,?,?,?) ";
        prepStmt = connection.prepareStatement(sql);
        prepStmt.setString(1, serial);
        prepStmt.setBlob(2, new ByteArrayInputStream(certificate.getEncoded()));
        prepStmt.setString(3, CertificateStatus.ACTIVE.toString());
        prepStmt.setTimestamp(4, new Timestamp(requestDate.getTime()));
        prepStmt.setTimestamp(5, new Timestamp(expiryDate.getTime()));
        prepStmt.setInt(6, tenantID);
        prepStmt.setString(7, username);
        prepStmt.setString(8, userStoreDomain);
        prepStmt.execute();
        connection.commit();
    } catch (IdentityException e) {
        String errorMsg = "Error when getting an Identity Persistence Store instance.";
        log.error(errorMsg, e);
        throw new CaException(errorMsg, e);
    } catch (SQLException e) {
        log.error("Error when executing the SQL : " + sql);
        log.error(e.getMessage(), e);
    } catch (CertificateEncodingException e) {
        log.error("Error encoding certificate");
    } finally {
        IdentityDatabaseUtil.closeAllConnections(connection, null, prepStmt);
    }
}

From source file:org.quartz.impl.jdbcjobstore.oracle.OracleDelegate.java

public int updateCalendar(Connection conn, String calendarName, Calendar calendar)
        throws IOException, SQLException {
    ByteArrayOutputStream baos = serializeObject(calendar);

    PreparedStatement ps = null;/* www.  jav a  2 s.  co m*/
    PreparedStatement ps2 = null;
    ResultSet rs = null;

    try {
        ps = conn.prepareStatement(rtp(SELECT_ORACLE_CALENDAR_BLOB));
        ps.setString(1, calendarName);

        rs = ps.executeQuery();

        if (rs.next()) {
            Blob dbBlob = writeDataToBlob(rs, 1, baos.toByteArray());
            ps2 = conn.prepareStatement(rtp(UPDATE_ORACLE_CALENDAR_BLOB));

            ps2.setBlob(1, dbBlob);
            ps2.setString(2, calendarName);

            return ps2.executeUpdate();
        }

        return 0;

    } finally {
        closeResultSet(rs);
        closeStatement(ps);
        closeStatement(ps2);
    }
}

From source file:org.ojbc.adapters.rapbackdatastore.dao.RapbackDAOImpl.java

@Override
public Integer saveCivilInitialRapSheet(final CivilInitialRapSheet civilInitialRapSheet) {
    log.debug("Inserting row into CIVIL_INITIAL_RAP_SHEET table : " + civilInitialRapSheet.toString());

    final String CIVIL_INITIAL_RAP_SHEET_INSERT = "insert into CIVIL_INITIAL_RAP_SHEET "
            + "(CIVIL_INITIAL_RESULT_ID, RAP_SHEET) " + "values (?, ?)";

    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(CIVIL_INITIAL_RAP_SHEET_INSERT,
                    new String[] { "CIVIL_INITIAL_RESULT_ID", "RAP_SHEET" });
            ps.setInt(1, civilInitialRapSheet.getCivilIntitialResultId());
            ps.setBlob(2, new SerialBlob(ZipUtils.zip(civilInitialRapSheet.getRapSheet())));
            return ps;
        }/*from  w ww. j a  v a 2 s .  c om*/
    }, keyHolder);

    return keyHolder.getKey().intValue();
}

From source file:org.ojbc.adapters.rapbackdatastore.dao.RapbackDAOImpl.java

@Override
public Integer saveCivilInitialResults(final CivilInitialResults civilInitialResults) {
    log.debug("Inserting row into CIVIL_INITIAL_RESULTS table : " + civilInitialResults.toString());

    final String CIVIL_INITIAL_RESULTS_INSERT = "insert into CIVIL_INITIAL_RESULTS "
            + "(TRANSACTION_NUMBER, SEARCH_RESULT_FILE, " + " RESULTS_SENDER_ID) " + "values (?, ?, ?)";

    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(CIVIL_INITIAL_RESULTS_INSERT,
                    new String[] { "TRANSACTION_NUMBER", "MATCH_NO_MATCH", "RESULTS_SENDER_ID" });
            ps.setString(1, civilInitialResults.getTransactionNumber());
            ps.setBlob(2, new SerialBlob(ZipUtils.zip(civilInitialResults.getSearchResultFile())));
            ps.setInt(3, civilInitialResults.getResultsSender().ordinal() + 1);
            return ps;
        }//from w ww.j  a va 2 s.c o m
    }, keyHolder);

    return keyHolder.getKey().intValue();
}

From source file:org.ojbc.adapters.rapbackdatastore.dao.RapbackDAOImpl.java

@Override
public Integer saveCriminalInitialResults(final CriminalInitialResults criminalInitialResults) {
    log.debug("Inserting row into CRIMINAL_INITIAL_RESULTS table : " + criminalInitialResults.toString());

    final String CRIMINAL_INITIAL_RESULTS_INSERT = "insert into CRIMINAL_INITIAL_RESULTS "
            + "(TRANSACTION_NUMBER, SEARCH_RESULT_FILE, RESULTS_SENDER_ID) " + "values (?, ?, ?)";

    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(CRIMINAL_INITIAL_RESULTS_INSERT,
                    new String[] { "TRANSACTION_NUMBER", "SEARCH_RESULT_FILE", "RESULTS_SENDER_ID" });
            ps.setString(1, criminalInitialResults.getTransactionNumber());
            ps.setBlob(2, new SerialBlob(ZipUtils.zip(criminalInitialResults.getSearchResultFile())));
            ps.setInt(3, criminalInitialResults.getResultsSender().ordinal() + 1);
            return ps;
        }/*from  w w w  .j a va 2 s  .  co m*/
    }, keyHolder);

    return keyHolder.getKey().intValue();
}

From source file:org.quartz.impl.jdbcjobstore.oracle.OracleDelegate.java

public int updateJobData(Connection conn, JobDetail job) throws IOException, SQLException {

    ByteArrayOutputStream baos = serializeJobData(job.getJobDataMap());
    byte[] data = baos.toByteArray();

    PreparedStatement ps = null;/*from w w  w  .  j  a v a  2s  .co m*/
    PreparedStatement ps2 = null;
    ResultSet rs = null;

    try {
        ps = conn.prepareStatement(rtp(SELECT_ORACLE_JOB_DETAIL_BLOB));
        ps.setString(1, job.getName());
        ps.setString(2, job.getGroup());

        rs = ps.executeQuery();

        int res = 0;

        if (rs.next()) {
            Blob dbBlob = writeDataToBlob(rs, 1, data);
            ps2 = conn.prepareStatement(rtp(UPDATE_ORACLE_JOB_DETAIL_BLOB));

            ps2.setBlob(1, dbBlob);
            ps2.setString(2, job.getName());
            ps2.setString(3, job.getGroup());

            res = ps2.executeUpdate();
        }

        return res;
    } finally {
        closeResultSet(rs);
        closeStatement(ps);
        closeStatement(ps2);
    }
}

From source file:org.ojbc.adapters.rapbackdatastore.dao.RapbackDAOImpl.java

@Override
public Integer saveCivilFingerPrints(final CivilFingerPrints civilFingerPrints) {
    log.debug("Inserting row into CIVIL_FINGER_PRINTS table : " + civilFingerPrints.toString());

    final String CIVIL_FINGER_PRINTS_INSERT = "insert into CIVIL_FINGER_PRINTS "
            + "(TRANSACTION_NUMBER, FINGER_PRINTS_FILE, FINGER_PRINTS_TYPE_ID) " + "values (?, ?, ?)";

    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(CIVIL_FINGER_PRINTS_INSERT,
                    new String[] { "TRANSACTION_NUMBER", "FINGER_PRINTS_FILE", "FINGER_PRINTS_TYPE" });
            ps.setString(1, civilFingerPrints.getTransactionNumber());

            if (civilFingerPrints.getFingerPrintsFile() != null) {
                ps.setBlob(2, new SerialBlob(ZipUtils.zip(civilFingerPrints.getFingerPrintsFile())));
            }//from  w  ww.j av  a  2  s .  co  m
            ps.setInt(3, civilFingerPrints.getFingerPrintsType().ordinal() + 1);
            return ps;
        }
    }, keyHolder);

    return keyHolder.getKey().intValue();
}