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();
}
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();
}
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);
}
}
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;
}
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;
}
Aggregations