Search in sources :

Example 26 with Blob

use of java.sql.Blob in project mssql-jdbc by Microsoft.

the class SQLServerResultSet method getBlob.

public Blob getBlob(String colName) throws SQLServerException {
    loggerExternal.entering(getClassNameLogging(), "getBlob", colName);
    checkClosed();
    Blob value = (Blob) getValue(findColumn(colName), JDBCType.BLOB);
    loggerExternal.exiting(getClassNameLogging(), "getBlob", value);
    activeBlob = value;
    return value;
}
Also used : Blob(java.sql.Blob)

Example 27 with Blob

use of java.sql.Blob in project mssql-jdbc by Microsoft.

the class SQLServerResultSet method getBlob.

public Blob getBlob(int i) throws SQLServerException {
    loggerExternal.entering(getClassNameLogging(), "getBlob", i);
    checkClosed();
    Blob value = (Blob) getValue(i, JDBCType.BLOB);
    loggerExternal.exiting(getClassNameLogging(), "getBlob", value);
    activeBlob = value;
    return value;
}
Also used : Blob(java.sql.Blob)

Example 28 with Blob

use of java.sql.Blob in project mssql-jdbc by Microsoft.

the class SQLServerCallableStatement method getBlob.

public Blob getBlob(int index) throws SQLServerException {
    loggerExternal.entering(getClassNameLogging(), "getBlob", index);
    checkClosed();
    Blob value = (Blob) getValue(index, JDBCType.BLOB);
    loggerExternal.exiting(getClassNameLogging(), "getBlob", value);
    return value;
}
Also used : Blob(java.sql.Blob)

Example 29 with Blob

use of java.sql.Blob in project mssql-jdbc by Microsoft.

the class lobsTest method readBlobStreamAfterClosingRS.

@Test
@DisplayName("readBlobStreamAfterClosingRS")
public void readBlobStreamAfterClosingRS() throws Exception {
    String[] types = { "varbinary(max)" };
    // create empty table
    table = createTable(table, types, false);
    int size = 10000;
    byte[] data = new byte[size];
    ThreadLocalRandom.current().nextBytes(data);
    Blob blob = null;
    InputStream stream = null;
    PreparedStatement ps = conn.prepareStatement("INSERT INTO " + table.getEscapedTableName() + "  VALUES(?)");
    blob = conn.createBlob();
    blob.setBytes(1, data);
    ps.setBlob(1, blob);
    ps.executeUpdate();
    byte[] chunk = new byte[size];
    ResultSet rs = stmt.executeQuery("select * from " + table.getEscapedTableName());
    rs.next();
    blob = rs.getBlob(1);
    stream = blob.getBinaryStream();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int read = 0;
    while ((read = stream.read(chunk)) > 0) buffer.write(chunk, 0, read);
    assertEquals(chunk.length, size);
    rs.close();
    stream = blob.getBinaryStream();
    buffer = new ByteArrayOutputStream();
    read = 0;
    while ((read = stream.read(chunk)) > 0) buffer.write(chunk, 0, read);
    assertEquals(chunk.length, size);
    if (null != blob)
        blob.free();
    dropTables(table);
}
Also used : Blob(java.sql.Blob) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet) PreparedStatement(java.sql.PreparedStatement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test) DynamicTest(org.junit.jupiter.api.DynamicTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 30 with Blob

use of java.sql.Blob in project mssql-jdbc by Microsoft.

the class lobsTest method readMultipleBlobStreamsThenCloseRS.

@Test
@DisplayName("readMultipleBlobStreamsThenCloseRS")
public void readMultipleBlobStreamsThenCloseRS() throws Exception {
    String[] types = { "varbinary(max)" };
    table = createTable(table, types, false);
    int size = 10000;
    byte[] data = new byte[size];
    Blob[] blobs = { null, null, null, null, null };
    InputStream stream = null;
    for (// create 5 blobs
    int i = 0; // create 5 blobs
    i < 5; // create 5 blobs
    i++) {
        PreparedStatement ps = conn.prepareStatement("INSERT INTO " + table.getEscapedTableName() + "  VALUES(?)");
        blobs[i] = conn.createBlob();
        ThreadLocalRandom.current().nextBytes(data);
        blobs[i].setBytes(1, data);
        ps.setBlob(1, blobs[i]);
        ps.executeUpdate();
    }
    byte[] chunk = new byte[size];
    ResultSet rs = stmt.executeQuery("select * from " + table.getEscapedTableName());
    for (int i = 0; i < 5; i++) {
        rs.next();
        blobs[i] = rs.getBlob(1);
        stream = blobs[i].getBinaryStream();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int read = 0;
        while ((read = stream.read(chunk)) > 0) buffer.write(chunk, 0, read);
        assertEquals(chunk.length, size);
    }
    rs.close();
    for (int i = 0; i < 5; i++) {
        stream = blobs[i].getBinaryStream();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int read = 0;
        while ((read = stream.read(chunk)) > 0) buffer.write(chunk, 0, read);
        assertEquals(chunk.length, size);
    }
}
Also used : Blob(java.sql.Blob) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet) PreparedStatement(java.sql.PreparedStatement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test) DynamicTest(org.junit.jupiter.api.DynamicTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

Blob (java.sql.Blob)337 ResultSet (java.sql.ResultSet)130 SQLException (java.sql.SQLException)109 PreparedStatement (java.sql.PreparedStatement)106 InputStream (java.io.InputStream)97 Clob (java.sql.Clob)81 ByteArrayInputStream (java.io.ByteArrayInputStream)59 IOException (java.io.IOException)54 Statement (java.sql.Statement)52 Connection (java.sql.Connection)36 Test (org.junit.Test)34 BigDecimal (java.math.BigDecimal)25 Timestamp (java.sql.Timestamp)25 SQLXML (java.sql.SQLXML)22 OutputStream (java.io.OutputStream)21 NClob (java.sql.NClob)21 Reader (java.io.Reader)19 Date (java.sql.Date)18 ArrayList (java.util.ArrayList)17 List (java.util.List)17