Search in sources :

Example 36 with Blob

use of java.sql.Blob in project h2database by h2database.

the class TestLob method testConcurrentCreate.

private void testConcurrentCreate() throws Exception {
    deleteDb("lob");
    final JdbcConnection conn1 = (JdbcConnection) getConnection("lob");
    final JdbcConnection conn2 = (JdbcConnection) getConnection("lob");
    conn1.setAutoCommit(false);
    conn2.setAutoCommit(false);
    final byte[] buffer = new byte[10000];
    Task task1 = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                Blob b = conn1.createBlob();
                OutputStream out = b.setBinaryStream(1);
                out.write(buffer);
                out.close();
            }
        }
    };
    Task task2 = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                Blob b = conn2.createBlob();
                OutputStream out = b.setBinaryStream(1);
                out.write(buffer);
                out.close();
            }
        }
    };
    task1.execute();
    task2.execute();
    Thread.sleep(1000);
    task1.get();
    task2.get();
    conn1.close();
    conn2.close();
}
Also used : Task(org.h2.util.Task) Blob(java.sql.Blob) OutputStream(java.io.OutputStream) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 37 with Blob

use of java.sql.Blob in project h2database by h2database.

the class TestLob method testLob.

private void testLob(boolean clob) throws Exception {
    deleteDb("lob");
    Connection conn = reconnect(null);
    conn = reconnect(conn);
    Statement stat = conn.createStatement();
    stat.execute("DROP TABLE IF EXISTS TEST");
    PreparedStatement prep;
    ResultSet rs;
    long time;
    stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, VALUE " + (clob ? "CLOB" : "BLOB") + ")");
    int len = getSize(1, 1000);
    if (config.networked && config.big) {
        len = 100;
    }
    time = System.nanoTime();
    prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?)");
    for (int i = 0; i < len; i += i + i + 1) {
        prep.setInt(1, i);
        int size = i * i;
        if (clob) {
            prep.setCharacterStream(2, getRandomReader(size, i), 0);
        } else {
            prep.setBinaryStream(2, getRandomStream(size, i), 0);
        }
        prep.execute();
    }
    trace("insert=" + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - time));
    traceMemory();
    conn = reconnect(conn);
    time = System.nanoTime();
    prep = conn.prepareStatement("SELECT ID, VALUE FROM TEST");
    rs = prep.executeQuery();
    while (rs.next()) {
        int id = rs.getInt("ID");
        int size = id * id;
        if (clob) {
            Reader rt = rs.getCharacterStream(2);
            assertEqualReaders(getRandomReader(size, id), rt, -1);
            Object obj = rs.getObject(2);
            if (obj instanceof Clob) {
                obj = ((Clob) obj).getCharacterStream();
            }
            assertEqualReaders(getRandomReader(size, id), (Reader) obj, -1);
        } else {
            InputStream in = rs.getBinaryStream(2);
            assertEqualStreams(getRandomStream(size, id), in, -1);
            Object obj = rs.getObject(2);
            if (obj instanceof Blob) {
                obj = ((Blob) obj).getBinaryStream();
            }
            assertEqualStreams(getRandomStream(size, id), (InputStream) obj, -1);
        }
    }
    trace("select=" + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - time));
    traceMemory();
    conn = reconnect(conn);
    time = System.nanoTime();
    prep = conn.prepareStatement("DELETE FROM TEST WHERE ID=?");
    for (int i = 0; i < len; i++) {
        prep.setInt(1, i);
        prep.executeUpdate();
    }
    trace("delete=" + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - time));
    traceMemory();
    conn = reconnect(conn);
    conn.setAutoCommit(false);
    prep = conn.prepareStatement("INSERT INTO TEST VALUES(1, ?)");
    if (clob) {
        prep.setCharacterStream(1, getRandomReader(0, 0), 0);
    } else {
        prep.setBinaryStream(1, getRandomStream(0, 0), 0);
    }
    prep.execute();
    conn.rollback();
    prep.execute();
    conn.commit();
    conn.createStatement().execute("DELETE FROM TEST WHERE ID=1");
    conn.rollback();
    conn.createStatement().execute("DELETE FROM TEST WHERE ID=1");
    conn.commit();
    conn.createStatement().execute("DROP TABLE TEST");
    conn.close();
}
Also used : Blob(java.sql.Blob) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) StringReader(java.io.StringReader) PreparedStatement(java.sql.PreparedStatement) Clob(java.sql.Clob) Savepoint(java.sql.Savepoint)

Example 38 with Blob

use of java.sql.Blob in project jforum2 by rafaelsteil.

the class OracleUtils method writeBlobUTF16BinaryStream.

/**
 * The query should look like:
 *
 * SELECT blob_field from any_table WHERE id = ? FOR UPDATE
 *
 * BUT KEEP IN MIND:
 *
 * When you insert record in previous step, it should go with empty_blob() like:
 *
 * INSERT INTO jforum_posts_text ( post_text ) VALUES (EMPTY_BLOB())
 *
 * @param query String
 * @param idForQuery int
 * @param value String
 * @throws SQLException
 */
public static void writeBlobUTF16BinaryStream(String query, int idForQuery, String value) throws Exception {
    PreparedStatement p = null;
    ResultSet rs = null;
    OutputStream blobWriter = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(query);
        p.setInt(1, idForQuery);
        rs = p.executeQuery();
        rs.next();
        Blob text = rs.getBlob(1);
        if (text instanceof BLOB) {
            blobWriter = ((BLOB) text).getBinaryOutputStream();
        } else {
            blobWriter = text.setBinaryStream(0);
        }
        blobWriter.write(value.getBytes("UTF-16"));
        blobWriter.close();
    } catch (IOException e) {
        throw new DatabaseException(e);
    } finally {
        if (blobWriter != null) {
            blobWriter.close();
        }
        DbUtils.close(rs, p);
    }
}
Also used : Blob(java.sql.Blob) BLOB(oracle.sql.BLOB) OutputStream(java.io.OutputStream) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 39 with Blob

use of java.sql.Blob in project ballerina by ballerina-lang.

the class TableIterator method generateNext.

@Override
public BStruct generateNext() {
    BStruct bStruct = new BStruct(type);
    int longRegIndex = -1;
    int doubleRegIndex = -1;
    int stringRegIndex = -1;
    int booleanRegIndex = -1;
    int refRegIndex = -1;
    int blobRegIndex = -1;
    int index = 0;
    try {
        BStructType.StructField[] structFields = type.getStructFields();
        for (BStructType.StructField sf : structFields) {
            BType type = sf.getFieldType();
            ++index;
            switch(type.getTag()) {
                case TypeTags.INT_TAG:
                    long iValue = rs.getInt(index);
                    bStruct.setIntField(++longRegIndex, iValue);
                    break;
                case TypeTags.STRING_TAG:
                    String sValue = rs.getString(index);
                    bStruct.setStringField(++stringRegIndex, sValue);
                    break;
                case TypeTags.FLOAT_TAG:
                    double dalue = rs.getDouble(index);
                    bStruct.setFloatField(++doubleRegIndex, dalue);
                    break;
                case TypeTags.BOOLEAN_TAG:
                    boolean boolValue = rs.getBoolean(index);
                    bStruct.setBooleanField(++booleanRegIndex, boolValue ? 1 : 0);
                    break;
                case TypeTags.JSON_TAG:
                    String jsonValue = rs.getString(index);
                    bStruct.setRefField(++refRegIndex, new BJSON(jsonValue));
                    break;
                case TypeTags.XML_TAG:
                    String xmlValue = rs.getString(index);
                    bStruct.setRefField(++refRegIndex, new BXMLItem(xmlValue));
                    break;
                case TypeTags.BLOB_TAG:
                    Blob blobValue = rs.getBlob(index);
                    bStruct.setBlobField(++blobRegIndex, blobValue.getBytes(1L, (int) blobValue.length()));
                    break;
                case TypeTags.ARRAY_TAG:
                    Array arrayValue = rs.getArray(index);
                    bStruct.setRefField(++refRegIndex, getDataArray(arrayValue));
                    break;
            }
        }
    } catch (SQLException e) {
        throw new BallerinaException("error in generating next row of data :" + e.getMessage());
    }
    return bStruct;
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BStruct(org.ballerinalang.model.values.BStruct) Blob(java.sql.Blob) SQLException(java.sql.SQLException) BJSON(org.ballerinalang.model.values.BJSON) BStructType(org.ballerinalang.model.types.BStructType) BFloatArray(org.ballerinalang.model.values.BFloatArray) BStringArray(org.ballerinalang.model.values.BStringArray) Array(java.sql.Array) BIntArray(org.ballerinalang.model.values.BIntArray) BNewArray(org.ballerinalang.model.values.BNewArray) BBooleanArray(org.ballerinalang.model.values.BBooleanArray) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 40 with Blob

use of java.sql.Blob in project Zong by Xenoage.

the class AudioAction method performTry.

@Override
public boolean performTry(Request request, Webserver server, HttpServletResponse response) throws SQLException, IOException {
    AudioRequest audioRequest = getAs(AudioRequest.class, request);
    Connection db = server.getDBConnection();
    // get ID of document
    PreparedStatement stmtID = stmt(db, "SELECT id FROM docs WHERE public_id = ?", audioRequest.id);
    ResultSet resID = stmtID.executeQuery();
    if (!resID.next()) {
        stmtID.close();
        return false;
    }
    int docID = resID.getInt(1);
    stmtID.close();
    // deliver audio data
    PreparedStatement stmtAudio = stmt(db, "SELECT audio FROM audio WHERE doc_id = ?" + " AND format = ?", docID, audioRequest.format);
    ResultSet resAudio = stmtAudio.executeQuery();
    if (!resAudio.next()) {
        stmtAudio.close();
        return false;
    }
    Blob blob = resAudio.getBlob(1);
    byte[] imageData = blob.getBytes(1, (int) blob.length());
    stmtAudio.close();
    // header
    response.setHeader("Content-Type", (audioRequest.format.equals("OGG") ? "application/ogg" : "audio/mpeg"));
    response.setHeader("Content-Disposition", "attachment; filename=\"" + audioRequest.id + (audioRequest.format.equals("OGG") ? ".ogg" : ".mp3") + "\"");
    // data
    response.getOutputStream().write(imageData);
    // update access time
    PreparedStatement stmtTime = stmt(db, "UPDATE docs SET last_access = ? WHERE id = ?", unixTime(), docID);
    stmtTime.executeUpdate();
    stmtTime.close();
    return true;
}
Also used : Blob(java.sql.Blob) AudioRequest(com.xenoage.zong.webserver.model.requests.AudioRequest) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

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