private void addPattern(String sentId, Map<Integer, Set<E>> patterns, PreparedStatement pstmt) throws SQLException, IOException { if(pstmt != null){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(patterns); byte[] patsAsBytes = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(patsAsBytes); pstmt.setBinaryStream(2, bais, patsAsBytes.length); pstmt.setObject(1, sentId); //pstmt.setInt(2, tokenId); } }
PreparedStatement pstmt = conn.prepareStatement("update blob_table set blob = ? where id = ?"); File blob = new File("/path/to/picture.png"); FileInputStream in = new FileInputStream(blob); // the cast to int is necessary because with JDBC 4 there is // also a version of this method with a (int, long) // but that is not implemented by Oracle pstmt.setBinaryStream(1, in, (int)blob.length()); pstmt.setInt(2, 42); // set the PK value pstmt.executeUpdate(); conn.commit(); pstmt.close();
public void insert(DbSession dbSession, String taskUuid, InputStream data) { long now = system.now(); Connection connection = dbSession.getConnection(); try (PreparedStatement stmt = connection.prepareStatement( "INSERT INTO ce_task_input (task_uuid, created_at, updated_at, input_data) VALUES (?, ?, ?, ?)")) { stmt.setString(1, taskUuid); stmt.setLong(2, now); stmt.setLong(3, now); stmt.setBinaryStream(4, data); stmt.executeUpdate(); connection.commit(); } catch (SQLException e) { throw new IllegalStateException("Fail to insert data of CE task " + taskUuid, e); } }
@Override public ClientMetadata update(ClientMetadata resource, String zoneId) { logger.debug("Updating metadata for client: " + resource.getClientId()); updateClientNameIfNotEmpty(resource, IdentityZoneHolder.get().getId()); int updated = template.update(CLIENT_METADATA_UPDATE, ps -> { int pos = 1; ps.setBoolean(pos++, resource.isShowOnHomePage()); URL appLaunchUrl = resource.getAppLaunchUrl(); ps.setString(pos++, appLaunchUrl == null ? null : appLaunchUrl.toString()); String appIcon = resource.getAppIcon(); if (appIcon != null) { byte[] decodedAppIcon = Base64Utils.decode(appIcon.getBytes()); ps.setBinaryStream(pos++, new ByteArrayInputStream(decodedAppIcon), decodedAppIcon.length); } else { ps.setBinaryStream(pos++, new ByteArrayInputStream(new byte[]{}), 0); } ps.setString(pos++, resource.getClientId()); ps.setString(pos++, zoneId); }); ClientMetadata resultingClientMetadata = retrieve(resource.getClientId(), zoneId); if (updated > 1) { throw new IncorrectResultSizeDataAccessException(1); } return resultingClientMetadata; }
byte[] data = ...; PreparedStatement ps = connection.prepareStatement(...); ByteArrayInputStream stream = new ByteArrayInputStream(data); ps.setBinaryStream(1, stream); ps.executeUpdate();
/** {@inheritDoc} */ @Override public void putAllToStore(Map<?, ?> data) { Connection conn = null; PreparedStatement stmt = null; try { conn = connection(); stmt = conn.prepareStatement(H2CacheStore.MERGE); for (Map.Entry<?, ?> e : data.entrySet()) { stmt.setBinaryStream(1, new ByteArrayInputStream(H2CacheStore.serialize(e.getKey()))); stmt.setBinaryStream(2, new ByteArrayInputStream(H2CacheStore.serialize(e.getValue()))); stmt.addBatch(); } stmt.executeBatch(); } catch (SQLException e) { throw new IgniteException(e); } finally { U.closeQuiet(stmt); U.closeQuiet(conn); } }
public void insertBlob(String filedesc, File file) { Connection con = DriverManager.getConnection(url, username, password); InputStream input = new FileInputStream(file); PreparedStatement pstmt = con.prepareStatement( "insert into schema.table values(?,?)"); pstmt.setString(1, filedesc); pstmt.setBinaryStream(2, input); pstmt.execute(); }
//.. PreparedStatement ps = con.prepareStatement("INSERT INTO data_table VALUES(?, ?, ?)"); //... ps.setInt(1,id); ps.setString(2, file.getName()); ps.setBinaryStream(3, fs,fs.available()); int i = ps.executeUpdate(); //...
File file = new File("/tmp/photo.png"); fis = new FileInputStream(file); ps = conn.prepareStatement(INSERT_PICTURE); ps.setBinaryStream(1, fis, (int) file.length()); ps.executeUpdate(); conn.commit(); } finally {
@Override public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { if ( value == null ) { st.setNull( index, TYPES[0] ); st.setNull( index + 1, TYPES[1] ); } else { st.setString( index, value.getClass().getName() ); st.setBinaryStream( index + 1, convertObjectToInputStream( value ) ); } }
protected PreparedStatement prepareStatement(Map<String, Object> attributes, Statement sth, String query ) throws SQLException { // recycle prepared query - should not differ ! if (xquery != null & preparedSth != null && xquery.equals(query)) return preparedSth; // if differ close last prepared query closePreparedSth(); // checkout new if (attributes != null && attributes.containsKey( RETURN_BINARY_KEY + "0")) { PreparedStatement psth = dbCon.getConnection().prepareStatement(query); xquery = query; for (int nr = 0; attributes.containsKey(RETURN_BINARY_KEY + nr); nr++) { psth.setBinaryStream(nr+1, (InputStream) attributes.get(RETURN_BINARY_KEY + nr)); attributes.remove(RETURN_BINARY_KEY + nr); } return psth; } return null; }
/** * @throws IllegalArgumentException if {@code scannerContextLines} is empty or fully read. */ public void insert(DbSession dbSession, String taskUuid, CloseableIterator<String> scannerContextLines) { checkArgument(scannerContextLines.hasNext(), "Scanner context can not be empty"); long now = system.now(); Connection connection = dbSession.getConnection(); try (PreparedStatement stmt = connection.prepareStatement( "INSERT INTO ce_scanner_context (task_uuid, created_at, updated_at, context_data) VALUES (?, ?, ?, ?)"); InputStream inputStream = new LogsIteratorInputStream(scannerContextLines, UTF_8)) { stmt.setString(1, taskUuid); stmt.setLong(2, now); stmt.setLong(3, now); stmt.setBinaryStream(4, inputStream); stmt.executeUpdate(); connection.commit(); } catch (SQLException | IOException e) { throw new IllegalStateException("Fail to insert scanner context for task " + taskUuid, e); } }
@Test public void testSetBlobAsBinaryStream() throws SQLException, IOException { InputStream bis = new ByteArrayInputStream("testContent".getBytes()); lobCreator.setBlobAsBinaryStream(ps, 1, bis, 11); verify(ps).setBinaryStream(1, bis, 11); }
stmt.setBinaryStream(parameterIndex, new ByteArrayInputStream(bValue), bValue.length); case Types.LONGNVARCHAR: case Types.LONGVARCHAR: stmt.setString(parameterIndex, parameterValue); break; case Types.CLOB:
CallableStatement statement = con.prepareCall(query); ByteArrayInputStream baisForIndex2 = new ByteArrayInputStream(immAsBytes); ByteArrayInputStream baisForIndex3 = new ByteArrayInputStream(immAsBytes); statement.setDate(1, sqlDate); statement.setBinaryStream(2, baisForIndex2, immAsBytes.length); statement.setBinaryStream(3, baisForIndex3, immAsBytes.length); statement.executeUpdate();
/** * Selects from H2 and deserialize from bytes the value pointed by key. * * @param conn {@link Connection} to use. * @param key Key to look for. * @return Stored object or null if the key is missing from DB. * @throws SQLException If failed. */ static Object getFromDb(Connection conn, Object key) throws SQLException { PreparedStatement stmt = null; ResultSet rs = null; try { stmt = conn.prepareStatement("select v from CACHE where k = ?"); stmt.setBinaryStream(1, new ByteArrayInputStream(H2CacheStore.serialize(key))); rs = stmt.executeQuery(); return rs.next() ? H2CacheStore.deserialize(rs.getBytes(1)) : null; } finally { U.closeQuiet(rs); U.closeQuiet(stmt); } }
static void updateBinaryResourceForCategory(Connection connection, String apiID, ResourceCategory category, InputStream resourceValue, String updatedBy) throws SQLException { final String query = "UPDATE AM_API_RESOURCES SET RESOURCE_BINARY_VALUE = ?, UPDATED_BY = ?, " + "LAST_UPDATED_TIME = ? WHERE API_ID = ? AND RESOURCE_CATEGORY_ID = ?"; try (PreparedStatement statement = connection.prepareStatement(query)) { statement.setBinaryStream(1, resourceValue); statement.setString(2, updatedBy); statement.setTimestamp(3, Timestamp.valueOf(LocalDateTime.now())); statement.setString(4, apiID); statement.setInt(5, ResourceCategoryDAO.getResourceCategoryID(connection, category)); statement.execute(); } }
File file = new File("myimage.gif"); FileInputStream fis = new FileInputStream(file); PreparedStatement ps = conn.prepareStatement("INSERT INTO images VALUES (?, ?)"); ps.setString(1, file.getName()); ps.setBinaryStream(2, fis, (int)file.length()); ps.executeUpdate(); ps.close(); fis.close();
public void saveFileToDatabase(File file) { InputStream inputStream = new FileInputStream(file); Connection conn = ...; PreparedStatement pS = conn.prepareStatement(...); ... pS.setBinaryStream(index, inputStream, (int) file.length()); ... pS.executeUpdate(); }
PreparedStatement pst = connection.prepareStatement(query); pst.setString(1, "harom"); pst.setBinaryStream(2, ois); PreparedStatement pst = connection.prepareStatement(query); pst.setString(1, "harom"); pst.setBinaryStream(2, ois); pst.execute();