Java Code Examples for java.sql.Blob.setBytes()

The following are Jave code examples for showing how to use setBytes() of the java.sql.Blob class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: ProyectoPacientes   File: BlobRegressionTest.java   View Source Code Vote up 6 votes
/**
 * @throws Exception
 */
public void testBug2670() throws Exception {
    byte[] blobData = new byte[32];

    for (int i = 0; i < blobData.length; i++) {
        blobData[i] = 1;
    }

    createTable("testBug2670", "(blobField LONGBLOB)");

    PreparedStatement pStmt = this.conn.prepareStatement("INSERT INTO testBug2670 (blobField) VALUES (?)");
    pStmt.setBytes(1, blobData);
    pStmt.executeUpdate();

    this.rs = this.stmt.executeQuery("SELECT blobField FROM testBug2670");
    this.rs.next();

    Blob blob = this.rs.getBlob(1);

    //
    // Test mid-point insertion
    //
    blob.setBytes(4, new byte[] { 2, 2, 2, 2 });

    byte[] newBlobData = blob.getBytes(1L, (int) blob.length());

    assertTrue("Blob changed length", blob.length() == blobData.length);

    assertTrue("New data inserted wrongly", ((newBlobData[3] == 2) && (newBlobData[4] == 2) && (newBlobData[5] == 2) && (newBlobData[6] == 2)));

    //
    // Test end-point insertion
    //
    blob.setBytes(32, new byte[] { 2, 2, 2, 2 });

    assertTrue("Blob length should be 3 larger", blob.length() == (blobData.length + 3));
}
 
Example 2
Project: BibliotecaPS   File: BlobRegressionTest.java   View Source Code Vote up 6 votes
/**
 * @throws Exception
 */
public void testBug2670() throws Exception {
    byte[] blobData = new byte[32];

    for (int i = 0; i < blobData.length; i++) {
        blobData[i] = 1;
    }

    createTable("testBug2670", "(blobField LONGBLOB)");

    PreparedStatement pStmt = this.conn.prepareStatement("INSERT INTO testBug2670 (blobField) VALUES (?)");
    pStmt.setBytes(1, blobData);
    pStmt.executeUpdate();

    this.rs = this.stmt.executeQuery("SELECT blobField FROM testBug2670");
    this.rs.next();

    Blob blob = this.rs.getBlob(1);

    //
    // Test mid-point insertion
    //
    blob.setBytes(4, new byte[] { 2, 2, 2, 2 });

    byte[] newBlobData = blob.getBytes(1L, (int) blob.length());

    assertTrue("Blob changed length", blob.length() == blobData.length);

    assertTrue("New data inserted wrongly", ((newBlobData[3] == 2) && (newBlobData[4] == 2) && (newBlobData[5] == 2) && (newBlobData[6] == 2)));

    //
    // Test end-point insertion
    //
    blob.setBytes(32, new byte[] { 2, 2, 2, 2 });

    assertTrue("Blob length should be 3 larger", blob.length() == (blobData.length + 3));
}
 
Example 3
Project: mongodb-rdbms-sync   File: SqlLiteralFactory.java   View Source Code Vote up 6 votes
public static Literal getLobLiteral(Object inputValue, String dataType, Connection connection) throws SQLException{
	Literal literal = null;
	if(connection==null){
		literal = new NullLiteral();
	}else{
		if(SqlColumnType.BLOB.equalsIgnoreCase(dataType)){
			literal= new BlobLiteral();
			Blob blob = connection.createBlob();
			Binary objArray = (Binary) inputValue;
			blob.setBytes(1, objArray.getData());
			literal.setLiteralValue(blob);
		}else if(SqlColumnType.CLOB.equalsIgnoreCase(dataType)){
			literal = new ClobLiteral();
			Clob clob = connection.createClob();
			clob.setString(1, String.valueOf(inputValue));
			literal.setLiteralValue(clob);
		}
	}
	return literal;
}
 
Example 4
Project: the-vigilantes   File: BlobRegressionTest.java   View Source Code Vote up 5 votes
/**
 * @throws Exception
 */
public void testBug2670() throws Exception {
    byte[] blobData = new byte[32];

    for (int i = 0; i < blobData.length; i++) {
        blobData[i] = 1;
    }

    createTable("testBug2670", "(blobField LONGBLOB)");

    PreparedStatement pStmt = this.conn.prepareStatement("INSERT INTO testBug2670 (blobField) VALUES (?)");
    pStmt.setBytes(1, blobData);
    pStmt.executeUpdate();

    this.rs = this.stmt.executeQuery("SELECT blobField FROM testBug2670");
    this.rs.next();

    Blob blob = this.rs.getBlob(1);

    //
    // Test mid-point insertion
    //
    blob.setBytes(4, new byte[] { 2, 2, 2, 2 });

    byte[] newBlobData = blob.getBytes(1L, (int) blob.length());

    assertTrue("Blob changed length", blob.length() == blobData.length);

    assertTrue("New data inserted wrongly", ((newBlobData[3] == 2) && (newBlobData[4] == 2) && (newBlobData[5] == 2) && (newBlobData[6] == 2)));

    //
    // Test end-point insertion
    //
    blob.setBytes(32, new byte[] { 2, 2, 2, 2 });

    assertTrue("Blob length should be 3 larger", blob.length() == (blobData.length + 3));
}
 
Example 5
Project: OpenVertretung   File: BlobRegressionTest.java   View Source Code Vote up 5 votes
/**
 * @throws Exception
 */
public void testBug2670() throws Exception {
    byte[] blobData = new byte[32];

    for (int i = 0; i < blobData.length; i++) {
        blobData[i] = 1;
    }

    createTable("testBug2670", "(blobField LONGBLOB)");

    PreparedStatement pStmt = this.conn.prepareStatement("INSERT INTO testBug2670 (blobField) VALUES (?)");
    pStmt.setBytes(1, blobData);
    pStmt.executeUpdate();

    this.rs = this.stmt.executeQuery("SELECT blobField FROM testBug2670");
    this.rs.next();

    Blob blob = this.rs.getBlob(1);

    //
    // Test mid-point insertion
    //
    blob.setBytes(4, new byte[] { 2, 2, 2, 2 });

    byte[] newBlobData = blob.getBytes(1L, (int) blob.length());

    assertTrue("Blob changed length", blob.length() == blobData.length);

    assertTrue("New data inserted wrongly", ((newBlobData[3] == 2) && (newBlobData[4] == 2) && (newBlobData[5] == 2) && (newBlobData[6] == 2)));

    //
    // Test end-point insertion
    //
    blob.setBytes(32, new byte[] { 2, 2, 2, 2 });

    assertTrue("Blob length should be 3 larger", blob.length() == (blobData.length + 3));
}
 
Example 6
Project: lams   File: TemporaryLobCreator.java   View Source Code Vote up 5 votes
@Override
public void setBlobAsBytes(PreparedStatement ps, int paramIndex, byte[] content)
		throws SQLException {

	Blob blob = ps.getConnection().createBlob();
	blob.setBytes(1, content);

	this.temporaryBlobs.add(blob);
	ps.setBlob(paramIndex, blob);

	if (logger.isDebugEnabled()) {
		logger.debug(content != null ? "Copied bytes into temporary BLOB with length " + content.length :
				"Set BLOB to null");
	}
}
 
Example 7
Project: lams   File: ContextualLobCreator.java   View Source Code Vote up 5 votes
@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 );
	}
}
 
Example 8
Project: ipf-flow-manager   File: HibernateUtils.java   View Source Code Vote up 3 votes
/**
 * Writes a byte array to a {@link Blob}.
 * 
 * @param bytes
 *            byte array.
 * @param blob
 *            an initialized {@link Blob}.
 * @return the {@link Blob} instance passed as <code>blob</code> argument.
 * @throws SQLException
 */
public static Blob writeToBlob(byte[] bytes, Blob blob, Session session) throws SQLException {
    if (/*blob != null*/ false) { // not supported
        blob.setBytes(0, bytes);
        blob.truncate(bytes.length);
    } else {
        blob = Hibernate.getLobCreator(session).createBlob(bytes);
    }
    return blob;
}