@Override public void setBlobAsBytes(PreparedStatement ps, int paramIndex, byte[] content) throws SQLException { Blob blob = ps.getConnection().createBlob(); blob.setBytes(1, content); ps.setBlob(paramIndex, blob); }
@Override public void setBlobAsBytes(PreparedStatement ps, int paramIndex, @Nullable byte[] content) throws SQLException { if (content != null) { Blob blob = ps.getConnection().createBlob(); blob.setBytes(1, content); this.temporaryBlobs.add(blob); ps.setBlob(paramIndex, blob); } else { ps.setBlob(paramIndex, (Blob) null); } if (logger.isDebugEnabled()) { logger.debug(content != null ? "Copied bytes into temporary BLOB with length " + content.length : "Set BLOB to null"); } }
/** * Convert an ndarray to a blob * * @param toConvert the complex ndarray to convert * @return the converted complex ndarray */ @Override public Blob convert(IComplexNDArray toConvert) throws IOException, SQLException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); Nd4j.writeComplex(toConvert, dos); byte[] bytes = bos.toByteArray(); Connection c = dataSource.getConnection(); Blob b = c.createBlob(); b.setBytes(1, bytes); return b; }
@Override public Blob createBlob(byte[] bytes) { try { final Blob blob = createBlob(); blob.setBytes( 1, bytes ); return blob; } catch ( SQLException e ) { throw new JDBCException( "Unable to set BLOB bytes after creation", e ); } }
/** * Convert an ndarray to a blob * * @param toConvert the ndarray to convert * @return the converted ndarray */ @Override public Blob convert(INDArray toConvert) throws SQLException { ByteBuffer byteBuffer = BinarySerde.toByteBuffer(toConvert); Buffer buffer = (Buffer) byteBuffer; buffer.rewind(); byte[] arr = new byte[byteBuffer.capacity()]; byteBuffer.get(arr); Connection c = dataSource.getConnection(); Blob b = c.createBlob(); b.setBytes(1, arr); return b; }
Object[] args = new Object[]{}; //arguments to be passed to the method. none in this case Blob blob = (Blob) c3p0Conn.rawConnectionOperation(m, C3P0ProxyConnection.RAW_CONNECTION, args); blob.setBytes(1, byteArray); ps.setBlob(index, blob); } catch (Exception ex) { blob.setBytes(1, byteArray); ps.setBlob(index, blob);
Object[] args = new Object[]{}; //arguments to be passed to the method. none in this case Blob blob = (Blob) c3p0Conn.rawConnectionOperation(m, C3P0ProxyConnection.RAW_CONNECTION, args); blob.setBytes(1, byteArray); ps.setBlob(index, blob); } catch (Exception ex) { blob.setBytes(1, byteArray); ps.setBlob(index, blob);
private void updateBlob(Connection connection, String findMessageByIdStatement, long sequence, byte[] data) throws SQLException, IOException { PreparedStatement s = null; ResultSet rs = null; try { s = connection.prepareStatement(statements.getFindMessageByIdStatement(), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); s.setLong(1, sequence); rs = s.executeQuery(); if (!rs.next()) { throw new IOException("Failed select blob for message: " + sequence + " in container."); } // Update the blob Blob blob = rs.getBlob(1); blob.truncate(0); blob.setBytes(1, data); rs.updateBlob(1, blob); rs.updateRow(); // Update the row with the updated blob } finally { close(rs); close(s); } }
/** * @throws Exception If failed. */ @Test public void testBlob() throws Exception { stmt = conn.prepareStatement("select * from TestObject where blobVal is not distinct from ?"); Blob blob = conn.createBlob(); blob.setBytes(1, new byte[] {1}); stmt.setBlob(1, blob); ResultSet rs = stmt.executeQuery(); int cnt = 0; while (rs.next()) { if (cnt == 0) assert rs.getInt("id") == 1; cnt++; } assertEquals(1, cnt); stmt.setNull(1, BINARY); rs = stmt.executeQuery(); cnt = 0; while (rs.next()) { if (cnt == 0) assert rs.getInt("id") == 2; cnt++; } assert cnt == 1; }
Blob blob; blob = conn.createBlob(); blob.setBytes(0, v.getBytes()); return blob; } else if (paramClass == Clob.class) {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); Blob b1 = conn.createBlob(); b1.setBytes(0, new byte[10]); PreparedStatement ps = conn.prepareStatement("update t1 set c1 = ?"); ps.setBlob(1, b1); ps.executeUpdate(); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select c1 from t1"); Blob b2 = rs.getBlob(1);
@Override public void setBlobAsBytes(PreparedStatement ps, int paramIndex, byte[] content) throws SQLException { Blob blob = ps.getConnection().createBlob(); blob.setBytes(1, content); ps.setBlob(paramIndex, blob); }
public static void updateBlobColumn(Connection con, String table, String blobColumn, byte[] inputBytes, String idColumn, Long id) throws SQLException { PreparedStatement pStmt = null; ResultSet rs = null; try { String sql = " SELECT " + blobColumn + " FROM " + table + " WHERE " + idColumn + " = ? " + " FOR UPDATE"; pStmt = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); pStmt.setLong(1, id); rs = pStmt.executeQuery(); if (rs.next()) { Blob blob = rs.getBlob(blobColumn); blob.truncate(0); blob.setBytes(1, inputBytes); rs.updateBlob(blobColumn, blob); rs.updateRow(); } } finally { if(rs != null) rs.close(); if(pStmt != null) pStmt.close(); } }
ps.setBytes(index, bytes); } else { blob.setBytes(1, bytes); ps.setBlob(index, blob);
@Test public void binaryExcludedFalse() throws SQLException { // given P6LogOptions.getActiveInstance().setExcludebinary(false); // when String update = "insert into img values (?, ?, ?)"; PreparedStatement prep = getPreparedStatement(update); prep.setInt(1, 1); prep.setBytes(2, "foo".getBytes(StandardCharsets.UTF_8)); if( // java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc4.Jdbc4Connection.createBlob() is not yet implemented. "PostgreSQL".equals(db) // // org.firebirdsql.jdbc.FBDriverNotCapableException: Not yet implemented. // at org.firebirdsql.jdbc.FBBlob.setBytes(FBBlob.java:472) || "Firebird".equals(db) // // java.lang.AbstractMethodError // at com.p6spy.engine.wrapper.ConnectionWrapper.createBlob(ConnectionWrapper.java:315) || "SQLite".equals(db)) { prep.setBytes(3, "foo".getBytes(StandardCharsets.UTF_8)); } else { Blob data = connection.createBlob(); data.setBytes(1, "foo".getBytes(StandardCharsets.UTF_8)); prep.setBlob(3, data); } prep.execute(); // then assertTrue(super.getLastLogEntry().contains("insert into img values (1, '666F6F',")); }
@Test public void binaryExcludedTrue() throws SQLException { // given P6LogOptions.getActiveInstance().setExcludebinary(true); // when String update = "insert into img values (?, ?, ?)"; PreparedStatement prep = getPreparedStatement(update); prep.setInt(1, 1); prep.setBytes(2, "foo".getBytes(StandardCharsets.UTF_8)); if( // java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc4.Jdbc4Connection.createBlob() is not yet implemented. "PostgreSQL".equals(db) // // org.firebirdsql.jdbc.FBDriverNotCapableException: Not yet implemented. // at org.firebirdsql.jdbc.FBBlob.setBytes(FBBlob.java:472) || "Firebird".equals(db) // // java.lang.AbstractMethodError // at com.p6spy.engine.wrapper.ConnectionWrapper.createBlob(ConnectionWrapper.java:315) || "SQLite".equals(db)) { prep.setBytes(3, "foo".getBytes(StandardCharsets.UTF_8)); } else { Blob data = connection.createBlob(); data.setBytes(1, "foo".getBytes(StandardCharsets.UTF_8)); prep.setBlob(3, data); } prep.execute(); // then assertTrue(super.getLastLogEntry().contains("insert into img values (1, '[binary]', ")); }
@Before public void setup() throws SQLException { P6LogOptions.getActiveInstance().setExcludebinary(true); String update = "insert into img values (?, ?, ?)"; PreparedStatement prep = getPreparedStatement(update); prep.setInt(1, 1000); prep.setBytes(2, "foo".getBytes(StandardCharsets.UTF_8)); if( // java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc4.Jdbc4Connection.createBlob() is not yet implemented. "PostgreSQL".equals(db) // // org.firebirdsql.jdbc.FBDriverNotCapableException: Not yet implemented. // at org.firebirdsql.jdbc.FBBlob.setBytes(FBBlob.java:472) || "Firebird".equals(db) // // java.lang.AbstractMethodError // at com.p6spy.engine.wrapper.ConnectionWrapper.createBlob(ConnectionWrapper.java:315) || "SQLite".equals(db)) { prep.setBytes(3, "foo".getBytes(StandardCharsets.UTF_8)); } else { Blob data = connection.createBlob(); data.setBytes(1, "foo".getBytes(StandardCharsets.UTF_8)); prep.setBlob(3, data); } prep.execute(); resultSet = executeQuery("select val from img where id=1000"); P6LogOptions.getActiveInstance().setExcludecategories("info,debug,result"); clearLogEntries(); clearLastLogStackTrace(); }
/** * Invoke the JDK 1.4 <code>setBytes</code> method on the given BLOB object. */ public void putBytes(Blob blob, byte[] data) throws SQLException { blob.setBytes(1L, data); }
@Override public Blob createTemporaryBlob(Connection connection, byte[] bytes) throws Exception { Blob blob = connection.createBlob(); blob.setBytes(1, bytes); return blob; }
@Override public Blob createTemporaryBlob(Connection connection, byte[] bytes) throws Exception { Blob blob = connection.createBlob(); blob.setBytes(1, bytes); return blob; }