Convert String to blob java - java

i have a from which accept image from user. i sent this form to the next jsp page to insert data in database. i take database field format blog. i do write get parameter line, now i want to convert it to blob type. how can i do this ?
String pic=request.getParameter("pic");
i did not want to use prepare statement to insert data in database

You can do it by getting byte[] content from String
byte[] byteData = yourString.getBytes("UTF-8");//Better to specify encoding
Blob blobData = dbConnection.createBlob();
blobData.setBytes(1, byteData);
Moreover, you can directly store String to BLOB column.

Related

Convert google/any image to blob(binary large object)

as per my requirement i want to store image in blob format in db Cassandara in a column type blob.
table: image_store
---------
image_id<int>|image_blob<blob>
I have google image in my filesystem. This will be a utility class which will be just used to convert image in blob.
Can I print blob value in text file or console sysout?
Is there any online website as well which can convert my file to blob and provide in text format so that i can just update in table.
Thanks in advance

How to access byte array from String which is made up of bytes array?

I want to save data in byte code format to my MySql db and retrieve it.
Example
String s = "String To Byte Array";
I am converting it to byte code using base64 library, so it becomes -
"U3RyaW5nIFRvIEJ5dGUgQXJyYXk="
Now I am saving this value in my MySql database in column with data type longtext
After fetching this value from DB I want to convert it in to original format.
You can decode the base64 back to string. This might help you.
https://examples.javacodegeeks.com/core-java/apache/commons/codec/decode-base64/
Once you obtained the encoded string from the database, you can obtain an instance of class Decoder and decode the string in this way:
Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(encoded.getBytes());
String decoded = new String(bytes);

Extracting the hex value after after converting a given byte array into blob obj in Java

I have a requirement to insert blob value into one of the tables(DB type - Oracle). I am using Java to convert my image file into a byte array first and then to blob object by using javax.sql.rowset.serial.SerialBlob.
However, when I create a blob object using
Blob blob = new SerialBlob(byteArray);
The resulting blob object is of the form javax.sql.rowset.serial.SerialBlob#'some-hex-value'. When I use the blob object in my insert query, Oracle gives me: Invalid hex number error.
But when I try to insert using only the hex part and removing 'javax.sql.rowset.serial.SerialBlob' part from my blob object, the insert query works fine and the record gets inserted.
So is there any way to extract the hex value from the blob object that I am converting my image file into?
Thanks!

CLOB to binarystream conversion

How to convert Clob data to Binarystream ?
I can able to find it in 2 steps as of now, 1--> CLOB to String 2---> String to BinaryStream.
I am calling a SQL package which has 1 i/p and 2 o/p and assigning the CLOB output to some variable XYZ, sample is shown below...
Clob XYZ=null;
CallableStatement CalSmt = null;
InputStream ABC = null;
try
{
CalSmt = conn.prepareCall("{call package(?,?,?)}");
CalSmt.registerOutParameter(1,OracleTypes.CLOB);
CalSmt.registerOutParameter(2,OracleTypes.INTEGER);
CalSmt.setString(3,"315141");
CalSmt.execute();
XYZ = CalSmt.getClob(1);
ABC= **XYZ.getBinaryStream();** <---- this is showing me a error as 'Method "getBinaryStream" not found'
}
Here XYZ is holding the CLOB data which needs to be converted to Binary Stream and Saved into ABC for further references. Kindly help me out by providing a single method for this plz. Thanks in Advance.
Yes, there's no getBinaryStream method on Clob, because it doesn't make sense for there to be one. A clob is character data, not binary data. It makes no more sense to ask a clob for a binary stream than it does to ask a blob for a character stream.
Your approach of converting to a string seems reasonable to me, to be honest, if you really need this conversion. It would be better if you could just avoid it though - if you're storing binary data, use a blob and InputStream everywhere. If you're storing character data, use a clob and Reader everywhere.

Not able to store large String in Clob

Not able to store large size String in CLOB
String which contains xml data . the size of String is greater than 19000.
I am able to store small string but getting null value in database for large string.
Please suggest the solution for the same.
I am using following code:
Clob myClob = Hibernate.createClob(updateStr);
Reader reader = myClob.getCharacterStream();
myServiceRequest.setRequestData(Hibernate.createClob(reader, (int)myClob.length()));
updateStr is a String which contains xml data and its size is very large. that i am not able to insert in DB.
RequestData is column(type CLOB) in database where i am inserting String .

Categories

Resources