'How to convert Json object to BLOB in java

I need to store HTTP REST API (POST, GET, PATCH, etc.) requests and responses into a database entry (Column as BLOB), so that we can audit the requests and responses later.

As part of the incoming HTTP POST request, the DTO object is coming as request body. I can extract the JSON object as a request body.

How can I convert that JSON object to BLOB in Java?



Solution 1:[1]

Try this

String str = json.toString();
PreparedStatement ps1 = conn.prepareStatement("update table set blob=? where id=1");
Blob blob = conn.createBlob();
blob.setBytes(1, str.getBytes());
ps1.setBlob(1, blob);
ps1.executeUpdate();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thangamudy Gurusamy