creating a random blob in jdbc and writing it to oracle

微笑、不失礼 提交于 2020-01-07 04:41:35

问题


I want to create many blobs in java [in memory] and write it to Oracle table.

What I want is the blob[bits inside it] to be random or sudo random so that oracle would not be able to do a lot of pre-optimization while storing the blob to the table.

Something like

for(1..1000000)
{
blob = createRandomBlob(sizeOfBlob);
sqlText ="INSERT INTO test_blob (id, blob) VALUES(i, blob)";
stmt.executeUpdate(sqlText);
}

Can someone point what JAVA APIs I can use to create such blob[in memory rather than on disk] and write it to db ?

Thanks


回答1:


I think this should do the trick

  byte[] data = new byte[10000];
    fRandom.nextBytes(data);
    try
    {
      SerialBlob fSerialBlob = new SerialBlob(data);
    } catch (SerialException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SQLException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

Dont know if there is a faster/efficient way of doing it.




回答2:


I can only answer for the "store it in the DB part", not sure about the "create a random blob array)

byte[] someBlob = createBlobBytes();
PreparedStatement stmt = connection.prepareStatement("INSERT INTO test_blob (id, blob) VALUES(?, ?)";
ByteArrayInputStream in = new ByteArrayInputStream(someBlob);
stmt.setInt(1, id);
stmt.setBinaryStream(2, in, someBlob.length);
stmt.executeUpdate();

This should work fine with the 10.x drivers



来源:https://stackoverflow.com/questions/4338889/creating-a-random-blob-in-jdbc-and-writing-it-to-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!