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 .
Related
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!
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.
I am trying to insert all the data at once using the following:
userModel.setServiceData(gson.toJson(sortedResponse).getBytes());
This is giving Out of Memory for the huge data.
So I am trying to add data in chunks to the postgres database using something like:
byte[] chunkedMessage = {};
for (Iterator<Message> iterator = sortedResponse.iterator(); iterator.hasNext();) {
Message message = (Message) iterator.next();
chunkedMessage = gson.toJson(message).getBytes();
userModel.setServiceData(chunkedMessage);
}
Obviously, this will end up storing the last chunk but not all chunks collectively.
I am using hibernate so thinking if there is any easy way with hibernate query or may be as easy as something in the above code.
Do not use arrays (byte[]), as it needs to allocate the memory for the complete data you want to insert into the database and switch to streams. Change the type of userModel.serviceData from byte[] to java.sql.Blob, which allows to create a new Blob instance from an InputStream or Reader:
Blob blob = Hibernate.getLobCreator(session).createBlob(stream, size);
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.
I am having CLOB column in Oracle Data Base , I want to insert String .
It works if I use setCharacterStream, but how to insert String by setBytes am getting exception.
Please help me.
String s = "Hello How are you Data for CLOB column";
ps.setCharacterStream(1, new StringReader(s), s.length());
ps.setByte(1,Byte.parseByte(s));
Exception Trace :
java.lang.NumberFormatException: For input string: "Hello How are you Data for CLOB column"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Byte.parseByte(Byte.java:151)
at java.lang.Byte.parseByte(Byte.java:108)
at colb.test.InertClob.main(InertClob.java:24)
Here are two good examples (with sample code, for select and insert):
Handling CLOBS made easy:
http://rocksolutions.wordpress.com/2010/06/07/handling-clobs-made-easy-with-oracle-jdbc-10g/
Adding large object type to databaase
http://docs.oracle.com/javase/tutorial/jdbc/basics/blob.html
please refer to the Java API DOC
Parses the string argument as a signed decimal byte. The characters in the string must all be decimal digits,
You should:
Turn the String to a byte array by calling s.getBytes() for example or any other.
Call setBytes method, not setByte
When retrieving from the database, don't forget how you got the bytes, in order to restore the String properly.