Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create BLOB object?

Tags:

java

jdbc

blob

  1. How to create BLOB object in Java?
  2. How to set the BLOB value from DB?
  3. How to set the BLOB value in DB?

I have create the BLOB object like this:

byte [] fileId = b.toByteArray();
Blob blob = new SerialBlob(fileId);

But it gives me an error.

like image 508
vijayk Avatar asked Jun 26 '13 05:06

vijayk


People also ask

How do I create a blob js file?

To create File object from Blob with JavaScript, we can use the File constructor`. const file = new File([blob], "filename"); to create a File object from a blob by putting it in an array.

How do I create a blob URL?

You can use URL. createObjectURL() to create Blob url. The URL. createObjectURL() static method creates a DOMString containing a URL representing the object given in the parameter.

What is Blob object in JavaScript?

A blob object is simply a collection of bytes that contains data stored in a file. A blob may appear to be a reference to the actual file, but it is not. A blob has the same size and MIME as a simple file. The blob data is stored in the user's memory, and it depends on the browser functions and the blob's size.

What is an example of blob?

BLOB (Binary Large Object) Examples While Binary Large objects can be structured data, or semi-structured data like XML files, most often they are unstructured data. Common BLOB examples are: Video (MP4, MOV) Audio (MP3)


1 Answers

  1. to create BLOB use Connection.createBlob

  2. to write BLOB to DB use PreparedStatement.setBlob

  3. to read BLOB from DB use ResultSet.getBlob

Assuming you have table t1 with BLOB column b1:

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Blob b1 = conn.createBlob();
b1.setBytes(1, new byte[10]); // first position is 1. Otherwise you get: Value of offset/position/start should be in the range [1, len] where len is length of Large Object[LOB]

PreparedStatement ps = conn.prepareStatement("update t1 set c1 = ?");
ps.setBlob(1, b1);
ps.executeUpdate();

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select c1 from t1");
Blob b2 = rs.getBlob(1);
like image 117
Evgeniy Dorofeev Avatar answered Oct 27 '22 11:10

Evgeniy Dorofeev